mirror of
https://github.com/gonum/gonum.git
synced 2025-10-03 22:36:25 +08:00

* optimize: Remove Local function This change removes the Local function. In order to do so, this changes the previous LocalGlobal wrapper to LocalController to allow Local methods to be used as a Global optimizer. This adds methods to all of the Local methods in order to implement GlobalMethod, and changes the tests accordingly. The next commit will fix all of the names
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// Copyright ©2016 The Gonum Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package optimize_test
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"gonum.org/v1/gonum/optimize"
|
|
"gonum.org/v1/gonum/optimize/functions"
|
|
)
|
|
|
|
func ExampleMinimize() {
|
|
p := optimize.Problem{
|
|
Func: functions.ExtendedRosenbrock{}.Func,
|
|
Grad: functions.ExtendedRosenbrock{}.Grad,
|
|
}
|
|
|
|
x := []float64{1.3, 0.7, 0.8, 1.9, 1.2}
|
|
settings := optimize.DefaultSettings()
|
|
settings.Recorder = nil
|
|
settings.GradientThreshold = 1e-12
|
|
settings.FunctionConverge = nil
|
|
settings.InitX = x
|
|
|
|
result, err := optimize.Global(p, len(x), settings, &optimize.BFGS{})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err = result.Status.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("result.Status: %v\n", result.Status)
|
|
fmt.Printf("result.X: %v\n", result.X)
|
|
fmt.Printf("result.F: %v\n", result.F)
|
|
fmt.Printf("result.Stats.FuncEvaluations: %d\n", result.Stats.FuncEvaluations)
|
|
// Output:
|
|
// result.Status: GradientThreshold
|
|
// result.X: [1 1 1 1 1]
|
|
// result.F: 0
|
|
// result.Stats.FuncEvaluations: 35
|
|
}
|