|
| 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/gonum/optimize" |
| 12 | + "github.com/gonum/optimize/functions" |
| 13 | +) |
| 14 | + |
| 15 | +func ExampleLocal_BFGS() { |
| 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 | + settings := optimize.DefaultSettings() |
| 23 | + settings.Recorder = nil |
| 24 | + settings.GradientThreshold = 1e-12 |
| 25 | + settings.FunctionConverge = nil |
| 26 | + |
| 27 | + result, err := optimize.Local(p, x, settings, &optimize.BFGS{}) |
| 28 | + if err != nil { |
| 29 | + log.Fatal(err) |
| 30 | + } |
| 31 | + if err = result.Status.Err(); err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | + fmt.Printf("result.Status: %v\n", result.Status) |
| 35 | + fmt.Printf("result.X: %v\n", result.X) |
| 36 | + fmt.Printf("result.F: %v\n", result.F) |
| 37 | + fmt.Printf("result.Stats.FuncEvaluations: %d\n", result.Stats.FuncEvaluations) |
| 38 | + // Output: |
| 39 | + // result.Status: GradientThreshold |
| 40 | + // result.X: [1 1 1 1 1] |
| 41 | + // result.F: 0 |
| 42 | + // result.Stats.FuncEvaluations: 35 |
| 43 | +} |
0 commit comments