mirror of
https://github.com/gonum/gonum.git
synced 2025-10-05 07:06:54 +08:00

* optimize: remove Local implementation and replace with a call to Global This PR starts the process described in #482. It removes the existing Local implementation, replacing with a function that wraps Method to act as a GlobalMethod. This PR also adds a hack to fix an inconsistency with FunctionConverge between Global and Local (and a TODO to make it not a hack in the future)
48 lines
971 B
Go
48 lines
971 B
Go
// Copyright ©2015 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 "math"
|
|
|
|
// FunctionConverge tests for the convergence of function values. See comment
|
|
// in Settings.
|
|
type FunctionConverge struct {
|
|
Absolute float64
|
|
Relative float64
|
|
Iterations int
|
|
|
|
first bool
|
|
best float64
|
|
iter int
|
|
}
|
|
|
|
func (fc *FunctionConverge) Init(f float64) {
|
|
fc.first = true
|
|
fc.best = 0
|
|
fc.iter = 0
|
|
}
|
|
|
|
func (fc *FunctionConverge) FunctionConverged(f float64) Status {
|
|
if fc.first {
|
|
fc.best = f
|
|
fc.first = false
|
|
return NotTerminated
|
|
}
|
|
if fc.Iterations == 0 {
|
|
return NotTerminated
|
|
}
|
|
maxAbs := math.Max(math.Abs(f), math.Abs(fc.best))
|
|
if f < fc.best && fc.best-f > fc.Relative*maxAbs+fc.Absolute {
|
|
fc.best = f
|
|
fc.iter = 0
|
|
return NotTerminated
|
|
}
|
|
fc.iter++
|
|
if fc.iter < fc.Iterations {
|
|
return NotTerminated
|
|
}
|
|
return FunctionConvergence
|
|
}
|