forked from gonum/optimize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linesearch.go
167 lines (143 loc) · 5.53 KB
/
linesearch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright ©2014 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"
"github.com/gonum/floats"
)
// LinesearchMethod represents an abstract optimization method in which
// a function is optimized through successive line search optimizations.
// It consists of a NextDirectioner, which specifies the search direction
// of each linesearch, and a Linesearcher which performs a linesearch along
// the search direction.
type LinesearchMethod struct {
NextDirectioner NextDirectioner
Linesearcher Linesearcher
x []float64 // Starting point for the current iteration.
dir []float64 // Search direction for the current iteration.
first bool // Indicator of the first iteration.
evalType EvaluationType
iterType IterationType
}
func (ls *LinesearchMethod) Init(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
if loc.Gradient == nil {
panic("linesearch: gradient is nil")
}
dim := len(loc.X)
ls.x = resize(ls.x, dim)
ls.dir = resize(ls.dir, dim)
ls.first = true
return ls.initNextLinesearch(loc, xNext)
}
func (ls *LinesearchMethod) Iterate(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
if ls.iterType == SubIteration {
// We needed to evaluate invalid fields of Location. Now we have them
// and can announce MajorIteration.
copy(xNext, loc.X)
ls.evalType = NoEvaluation
ls.iterType = MajorIteration
return ls.evalType, ls.iterType, nil
}
if ls.iterType == MajorIteration {
// The linesearch previously signaled MajorIteration. Since we're here,
// it means that the previous location is not good enough to converge,
// so start the next linesearch.
return ls.initNextLinesearch(loc, xNext)
}
projGrad := floats.Dot(loc.Gradient, ls.dir)
if ls.Linesearcher.Finished(loc.F, projGrad) {
copy(xNext, loc.X)
// Check if the last evaluation evaluated all fields of Location.
ls.evalType = complementEval(loc, ls.evalType)
if ls.evalType == NoEvaluation {
// Location is complete and MajorIteration can be announced directly.
ls.iterType = MajorIteration
} else {
// Location is not complete, evaluate its invalid fields in SubIteration.
ls.iterType = SubIteration
}
return ls.evalType, ls.iterType, nil
}
// Line search not done, just iterate.
stepSize, evalType, err := ls.Linesearcher.Iterate(loc.F, projGrad)
if err != nil {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, err
}
floats.AddScaledTo(xNext, ls.x, stepSize, ls.dir)
// Compare the starting point for the current iteration with the next
// evaluation point to make sure that rounding errors do not prevent progress.
if floats.Equal(ls.x, xNext) {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, ErrNoProgress
}
ls.evalType = evalType
ls.iterType = MinorIteration
return ls.evalType, ls.iterType, nil
}
func (ls *LinesearchMethod) initNextLinesearch(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
copy(ls.x, loc.X)
var stepSize float64
if ls.first {
stepSize = ls.NextDirectioner.InitDirection(loc, ls.dir)
ls.first = false
} else {
stepSize = ls.NextDirectioner.NextDirection(loc, ls.dir)
}
projGrad := floats.Dot(loc.Gradient, ls.dir)
if projGrad >= 0 {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, ErrNonNegativeStepDirection
}
ls.evalType = ls.Linesearcher.Init(loc.F, projGrad, stepSize)
floats.AddScaledTo(xNext, ls.x, stepSize, ls.dir)
// Compare the starting point for the current iteration with the next
// evaluation point to make sure that rounding errors do not prevent progress.
if floats.Equal(ls.x, xNext) {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, ErrNoProgress
}
ls.iterType = MinorIteration
return ls.evalType, ls.iterType, nil
}
// ArmijoConditionMet returns true if the Armijo condition (aka sufficient
// decrease) has been met. Under normal conditions, the following should be
// true, though this is not enforced:
// - initGrad < 0
// - step > 0
// - 0 < funcConst < 1
func ArmijoConditionMet(currObj, initObj, initGrad, step, funcConst float64) bool {
return currObj <= initObj+funcConst*step*initGrad
}
// StrongWolfeConditionsMet returns true if the strong Wolfe conditions have been met.
// The strong Wolfe conditions ensure sufficient decrease in the function
// value, and sufficient decrease in the magnitude of the projected gradient.
// Under normal conditions, the following should be true, though this is not
// enforced:
// - initGrad < 0
// - step > 0
// - 0 <= funcConst < gradConst < 1
func StrongWolfeConditionsMet(currObj, currGrad, initObj, initGrad, step, funcConst, gradConst float64) bool {
if currObj > initObj+funcConst*step*initGrad {
return false
}
return math.Abs(currGrad) < gradConst*math.Abs(initGrad)
}
// WeakWolfeConditionsMet returns true if the weak Wolfe conditions have been met.
// The weak Wolfe conditions ensure sufficient decrease in the function value,
// and sufficient decrease in the value of the projected gradient. Under normal
// conditions, the following should be true, though this is not enforced:
// - initGrad < 0
// - step > 0
// - 0 <= funcConst < gradConst < 1
func WeakWolfeConditionsMet(currObj, currGrad, initObj, initGrad, step, funcConst, gradConst float64) bool {
if currObj > initObj+funcConst*step*initGrad {
return false
}
return currGrad >= gradConst*initGrad
}