mirror of
https://github.com/gonum/gonum.git
synced 2025-09-27 19:42:12 +08:00

* optimize: Refactor gradient convergence and remove DefaultSettings The current API design makes it easy to make a mistake in not using the DefaultSettings. This change makes the zero value of Settings do the 'right thing'. The remaining setting that is used by the DefaultSettings is to change the behavior of the GradientTolerance. This was necessary because gradient-based Local methods (BFGS, LBFGS, CG, etc.) typically _define_ convergence by the value of the gradient, while Global methods (CMAES, GuessAndCheck) are defined by _not_ converging when the gradient is small. The problem is to have two completely different default behaviors without knowing the Method. The solution is to treat a very small value of the gradient as a method-based convergence, in the same way that a small spread of data is a convergence of CMAES. Thus, the default behavior, from the perspective of Settings, is never to converge based on the gradient, but all of the Local methods will converge when a value close to the minimum is found. This default value is set to a very small value, such that users should not want a smaller value. A user can thus still set a (more reasonable) convergence value through settings. Fixes 677.
37 lines
858 B
Go
37 lines
858 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
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gonum.org/v1/gonum/mat"
|
|
"gonum.org/v1/gonum/optimize/functions"
|
|
"gonum.org/v1/gonum/stat/distmv"
|
|
)
|
|
|
|
func TestGuessAndCheck(t *testing.T) {
|
|
dim := 30
|
|
problem := Problem{
|
|
Func: functions.ExtendedRosenbrock{}.Func,
|
|
}
|
|
mu := make([]float64, dim)
|
|
sigma := mat.NewSymDense(dim, nil)
|
|
for i := 0; i < dim; i++ {
|
|
sigma.SetSym(i, i, 1)
|
|
}
|
|
d, ok := distmv.NewNormal(mu, sigma, nil)
|
|
if !ok {
|
|
panic("bad test")
|
|
}
|
|
initX := make([]float64, dim)
|
|
Minimize(problem, initX, nil, &GuessAndCheck{Rander: d})
|
|
|
|
settings := &Settings{}
|
|
settings.Concurrent = 5
|
|
settings.MajorIterations = 15
|
|
Minimize(problem, initX, settings, &GuessAndCheck{Rander: d})
|
|
}
|