github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/optimize/local_example_test.go (about)

     1  // Copyright ©2016 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package optimize_test
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  
    11  	"github.com/jingcheng-WU/gonum/optimize"
    12  	"github.com/jingcheng-WU/gonum/optimize/functions"
    13  )
    14  
    15  func ExampleMinimize() {
    16  	p := optimize.Problem{
    17  		Func: functions.ExtendedRosenbrock{}.Func,
    18  		Grad: functions.ExtendedRosenbrock{}.Grad,
    19  	}
    20  
    21  	x := []float64{1.3, 0.7, 0.8, 1.9, 1.2}
    22  	result, err := optimize.Minimize(p, x, nil, nil)
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  	if err = result.Status.Err(); err != nil {
    27  		log.Fatal(err)
    28  	}
    29  	fmt.Printf("result.Status: %v\n", result.Status)
    30  	fmt.Printf("result.X: %0.4g\n", result.X)
    31  	fmt.Printf("result.F: %0.4g\n", result.F)
    32  	fmt.Printf("result.Stats.FuncEvaluations: %d\n", result.Stats.FuncEvaluations)
    33  	// Output:
    34  	// result.Status: GradientThreshold
    35  	// result.X: [1 1 1 1 1]
    36  	// result.F: 4.98e-30
    37  	// result.Stats.FuncEvaluations: 31
    38  }