Improve parameters when dimension of NelderMead is one. In particular… (#543)

* Improve parameters when dimension of NelderMead is one. In particular the shrink parameter is set to 0, which causes the algorithm to fail to converge.

Fixes #542.

* Add test for 1-D NelderMead
This commit is contained in:
Brendan Tracey
2018-07-18 12:18:43 -06:00
committed by GitHub
parent 2704973b50
commit c06c645ce2
2 changed files with 37 additions and 0 deletions

View File

@@ -127,14 +127,23 @@ func (n *NelderMead) Init(loc *Location) (Operation, error) {
n.expansion = n.Expansion
if n.expansion == 0 {
n.expansion = 1 + 2/float64(dim)
if dim == 1 {
n.expansion = 2
}
}
n.contraction = n.Contraction
if n.contraction == 0 {
n.contraction = 0.75 - 1/(2*float64(dim))
if dim == 1 {
n.contraction = 0.5
}
}
n.shrink = n.Shrink
if n.shrink == 0 {
n.shrink = 1 - 1/float64(dim)
if dim == 1 {
n.shrink = 0.5
}
}
if n.InitialVertices != nil {

View File

@@ -1307,3 +1307,31 @@ func TestIssue76(t *testing.T) {
t.Error("Issue https://github.com/gonum/optimize/issues/76 not fixed")
}
}
func TestNelderMeadOneD(t *testing.T) {
p := Problem{
Func: func(x []float64) float64 { return x[0] * x[0] },
}
x := []float64{10}
m := &NelderMead{}
s := DefaultSettings()
result, err := Local(p, x, s, m)
if err != nil {
t.Errorf(err.Error())
}
if !floats.EqualApprox(result.X, []float64{0}, 1e-10) {
t.Errorf("Minimum not found")
}
if m.reflection != 1 {
t.Errorf("Wrong value of reflection")
}
if m.expansion != 2 {
t.Errorf("Wrong value of expansion")
}
if m.contraction != 0.5 {
t.Errorf("Wrong value of contraction")
}
if m.shrink != 0.5 {
t.Errorf("Wrong value of shrink")
}
}