mirror of
https://github.com/gonum/gonum.git
synced 2025-09-28 03:52:16 +08:00

* optimize: Change initialization, remove Needser, and update Problem function calls We need a better way to express the Hessian function call so that sparse Hessians can be provided. This change updates the Problem function definitions to allow an arbitrary Symmetric matrix. With this change, we need to change how Location is used, so that we do not allocate a SymDense. Once this location is changed, we no longer need Needser to allocate the appropriate memory, and can shift that to initialization, further simplifying the interfaces. A 'fake' Problem is passed to Method to continue to make it impossible for the Method to call the functions directly. Fixes #727, #593.
39 lines
975 B
Go
39 lines
975 B
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}
|
|
result, err := optimize.Minimize(p, x, nil, nil)
|
|
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: %0.4g\n", result.X)
|
|
fmt.Printf("result.F: %0.4g\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: 4.98e-30
|
|
// result.Stats.FuncEvaluations: 31
|
|
}
|