Skip to content

Commit

Permalink
Merge e958694 into ff99a98
Browse files Browse the repository at this point in the history
  • Loading branch information
btracey committed Jul 13, 2018
2 parents ff99a98 + e958694 commit 20b7822
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions optimize/neldermead.go
Expand Up @@ -108,14 +108,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 {
Expand Down
28 changes: 28 additions & 0 deletions optimize/unconstrained_test.go
Expand Up @@ -1304,3 +1304,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")
}
}

0 comments on commit 20b7822

Please sign in to comment.