This repository has been archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
morethuente.go
385 lines (341 loc) · 10.8 KB
/
morethuente.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// 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"
// MoreThuente is a Linesearcher that finds steps that satisfy both the
// sufficient decrease and curvature conditions (the strong Wolfe conditions).
//
// References:
// - More, J.J. and D.J. Thuente: Line Search Algorithms with Guaranteed Sufficient
// Decrease. ACM Transactions on Mathematical Software 20(3) (1994), 286-307
type MoreThuente struct {
// DecreaseFactor is the constant factor in the sufficient decrease
// (Armijo) condition.
// It must be in the interval [0, 1). The default value is 0.
DecreaseFactor float64
// CurvatureFactor is the constant factor in the Wolfe conditions. Smaller
// values result in a more exact line search.
// A set value must be in the interval (0, 1). If it is zero, it will be
// defaulted to 0.9.
CurvatureFactor float64
// StepTolerance sets the minimum acceptable width for the linesearch
// interval. If the relative interval length is less than this value,
// ErrLinesearcherFailure is returned.
// It must be non-negative. If it is zero, it will be defaulted to 1e-10.
StepTolerance float64
// MinimumStep is the minimum step that the linesearcher will take.
// It must be non-negative and less than MaximumStep. Defaults to no
// minimum (a value of 0).
MinimumStep float64
// MaximumStep is the maximum step that the linesearcher will take.
// It must be greater than MinimumStep. If it is zero, it will be defaulted
// to 1e20.
MaximumStep float64
bracketed bool // Indicates if a minimum has been bracketed.
fInit float64 // Function value at step = 0.
gInit float64 // Derivative value at step = 0.
// When stage is 1, the algorithm updates the interval given by x and y
// so that it contains a minimizer of the modified function
// psi(step) = f(step) - f(0) - DecreaseFactor * step * f'(0).
// When stage is 2, the interval is updated so that it contains a minimizer
// of f.
stage int
step float64 // Current step.
lower, upper float64 // Lower and upper bounds on the next step.
x float64 // Endpoint of the interval with a lower function value.
fx, gx float64 // Data at x.
y float64 // The other endpoint.
fy, gy float64 // Data at y.
width [2]float64 // Width of the interval at two previous iterations.
}
const (
mtMinGrowthFactor float64 = 1.1
mtMaxGrowthFactor float64 = 4
)
func (mt *MoreThuente) Init(f, g float64, step float64) Operation {
// Based on the original Fortran code that is available, for example, from
// http://ftp.mcs.anl.gov/pub/MINPACK-2/csrch/
// as part of
// MINPACK-2 Project. November 1993.
// Argonne National Laboratory and University of Minnesota.
// Brett M. Averick, Richard G. Carter, and Jorge J. Moré.
if g >= 0 {
panic("morethuente: initial derivative is non-negative")
}
if step <= 0 {
panic("morethuente: invalid initial step")
}
if mt.CurvatureFactor == 0 {
mt.CurvatureFactor = 0.9
}
if mt.StepTolerance == 0 {
mt.StepTolerance = 1e-10
}
if mt.MaximumStep == 0 {
mt.MaximumStep = 1e20
}
if mt.MinimumStep < 0 {
panic("morethuente: minimum step is negative")
}
if mt.MaximumStep <= mt.MinimumStep {
panic("morethuente: maximum step is not greater than minimum step")
}
if mt.DecreaseFactor < 0 || mt.DecreaseFactor >= 1 {
panic("morethuente: invalid decrease factor")
}
if mt.CurvatureFactor <= 0 || mt.CurvatureFactor >= 1 {
panic("morethuente: invalid curvature factor")
}
if mt.StepTolerance <= 0 {
panic("morethuente: step tolerance is not positive")
}
if step < mt.MinimumStep {
step = mt.MinimumStep
}
if step > mt.MaximumStep {
step = mt.MaximumStep
}
mt.bracketed = false
mt.stage = 1
mt.fInit = f
mt.gInit = g
mt.x, mt.fx, mt.gx = 0, f, g
mt.y, mt.fy, mt.gy = 0, f, g
mt.lower = 0
mt.upper = step + mtMaxGrowthFactor*step
mt.width[0] = mt.MaximumStep - mt.MinimumStep
mt.width[1] = 2 * mt.width[0]
mt.step = step
return FuncEvaluation | GradEvaluation
}
func (mt *MoreThuente) Iterate(f, g float64) (Operation, float64, error) {
if mt.stage == 0 {
panic("morethuente: Init has not been called")
}
gTest := mt.DecreaseFactor * mt.gInit
fTest := mt.fInit + mt.step*gTest
if mt.bracketed {
if mt.step <= mt.lower || mt.step >= mt.upper || mt.upper-mt.lower <= mt.StepTolerance*mt.upper {
// step contains the best step found (see below).
return NoOperation, mt.step, ErrLinesearcherFailure
}
}
if mt.step == mt.MaximumStep && f <= fTest && g <= gTest {
return NoOperation, mt.step, ErrLinesearcherBound
}
if mt.step == mt.MinimumStep && (f > fTest || g >= gTest) {
return NoOperation, mt.step, ErrLinesearcherFailure
}
// Test for convergence.
if f <= fTest && math.Abs(g) <= mt.CurvatureFactor*(-mt.gInit) {
mt.stage = 0
return MajorIteration, mt.step, nil
}
if mt.stage == 1 && f <= fTest && g >= 0 {
mt.stage = 2
}
if mt.stage == 1 && f <= mt.fx && f > fTest {
// Lower function value but the decrease is not sufficient .
// Compute values and derivatives of the modified function at step, x, y.
fm := f - mt.step*gTest
fxm := mt.fx - mt.x*gTest
fym := mt.fy - mt.y*gTest
gm := g - gTest
gxm := mt.gx - gTest
gym := mt.gy - gTest
// Update x, y and step.
mt.nextStep(fxm, gxm, fym, gym, fm, gm)
// Recover values and derivates of the non-modified function at x and y.
mt.fx = fxm + mt.x*gTest
mt.fy = fym + mt.y*gTest
mt.gx = gxm + gTest
mt.gy = gym + gTest
} else {
// Update x, y and step.
mt.nextStep(mt.fx, mt.gx, mt.fy, mt.gy, f, g)
}
if mt.bracketed {
// Monitor the length of the bracketing interval. If the interval has
// not been reduced sufficiently after two steps, use bisection to
// force its length to zero.
width := mt.y - mt.x
if math.Abs(width) >= 2.0/3*mt.width[1] {
mt.step = mt.x + 0.5*width
}
mt.width[0], mt.width[1] = math.Abs(width), mt.width[0]
}
if mt.bracketed {
mt.lower = math.Min(mt.x, mt.y)
mt.upper = math.Max(mt.x, mt.y)
} else {
mt.lower = mt.step + mtMinGrowthFactor*(mt.step-mt.x)
mt.upper = mt.step + mtMaxGrowthFactor*(mt.step-mt.x)
}
// Force the step to be in [MinimumStep, MaximumStep].
mt.step = math.Max(mt.MinimumStep, math.Min(mt.step, mt.MaximumStep))
if mt.bracketed {
if mt.step <= mt.lower || mt.step >= mt.upper || mt.upper-mt.lower <= mt.StepTolerance*mt.upper {
// If further progress is not possible, set step to the best step
// obtained during the search.
mt.step = mt.x
}
}
return FuncEvaluation | GradEvaluation, mt.step, nil
}
// nextStep computes the next safeguarded step and updates the interval that
// contains a step that satisfies the sufficient decrease and curvature
// conditions.
func (mt *MoreThuente) nextStep(fx, gx, fy, gy, f, g float64) {
x := mt.x
y := mt.y
step := mt.step
gNeg := g < 0
if gx < 0 {
gNeg = !gNeg
}
var next float64
var bracketed bool
switch {
case f > fx:
// A higher function value. The minimum is bracketed between x and step.
// We want the next step to be closer to x because the function value
// there is lower.
theta := 3*(fx-f)/(step-x) + gx + g
s := math.Max(math.Abs(gx), math.Abs(g))
s = math.Max(s, math.Abs(theta))
gamma := s * math.Sqrt((theta/s)*(theta/s)-(gx/s)*(g/s))
if step < x {
gamma *= -1
}
p := gamma - gx + theta
q := gamma - gx + gamma + g
r := p / q
stpc := x + r*(step-x)
stpq := x + gx/((fx-f)/(step-x)+gx)/2*(step-x)
if math.Abs(stpc-x) < math.Abs(stpq-x) {
// The cubic step is closer to x than the quadratic step.
// Take the cubic step.
next = stpc
} else {
// If f is much larger than fx, then the quadratic step may be too
// close to x. Therefore heuristically take the average of the
// cubic and quadratic steps.
next = stpc + (stpq-stpc)/2
}
bracketed = true
case gNeg:
// A lower function value and derivatives of opposite sign. The minimum
// is bracketed between x and step. If we choose a step that is far
// from step, the next iteration will also likely fall in this case.
theta := 3*(fx-f)/(step-x) + gx + g
s := math.Max(math.Abs(gx), math.Abs(g))
s = math.Max(s, math.Abs(theta))
gamma := s * math.Sqrt((theta/s)*(theta/s)-(gx/s)*(g/s))
if step > x {
gamma *= -1
}
p := gamma - g + theta
q := gamma - g + gamma + gx
r := p / q
stpc := step + r*(x-step)
stpq := step + g/(g-gx)*(x-step)
if math.Abs(stpc-step) > math.Abs(stpq-step) {
// The cubic step is farther from x than the quadratic step.
// Take the cubic step.
next = stpc
} else {
// Take the quadratic step.
next = stpq
}
bracketed = true
case math.Abs(g) < math.Abs(gx):
// A lower function value, derivatives of the same sign, and the
// magnitude of the derivative decreases. Extrapolate function values
// at x and step so that the next step lies between step and y.
theta := 3*(fx-f)/(step-x) + gx + g
s := math.Max(math.Abs(gx), math.Abs(g))
s = math.Max(s, math.Abs(theta))
gamma := s * math.Sqrt(math.Max(0, (theta/s)*(theta/s)-(gx/s)*(g/s)))
if step > x {
gamma *= -1
}
p := gamma - g + theta
q := gamma + gx - g + gamma
r := p / q
var stpc float64
switch {
case r < 0 && gamma != 0:
stpc = step + r*(x-step)
case step > x:
stpc = mt.upper
default:
stpc = mt.lower
}
stpq := step + g/(g-gx)*(x-step)
if mt.bracketed {
// We are extrapolating so be cautious and take the step that
// is closer to step.
if math.Abs(stpc-step) < math.Abs(stpq-step) {
next = stpc
} else {
next = stpq
}
// Modify next if it is close to or beyond y.
if step > x {
next = math.Min(step+2.0/3*(y-step), next)
} else {
next = math.Max(step+2.0/3*(y-step), next)
}
} else {
// Minimum has not been bracketed so take the larger step...
if math.Abs(stpc-step) > math.Abs(stpq-step) {
next = stpc
} else {
next = stpq
}
// ...but within reason.
next = math.Max(mt.lower, math.Min(next, mt.upper))
}
default:
// A lower function value, derivatives of the same sign, and the
// magnitude of the derivative does not decrease. The function seems to
// decrease rapidly in the direction of the step.
switch {
case mt.bracketed:
theta := 3*(f-fy)/(y-step) + gy + g
s := math.Max(math.Abs(gy), math.Abs(g))
s = math.Max(s, math.Abs(theta))
gamma := s * math.Sqrt((theta/s)*(theta/s)-(gy/s)*(g/s))
if step > y {
gamma *= -1
}
p := gamma - g + theta
q := gamma - g + gamma + gy
r := p / q
next = step + r*(y-step)
case step > x:
next = mt.upper
default:
next = mt.lower
}
}
if f > fx {
// x is still the best step.
mt.y = step
mt.fy = f
mt.gy = g
} else {
// step is the new best step.
if gNeg {
mt.y = x
mt.fy = fx
mt.gy = gx
}
mt.x = step
mt.fx = f
mt.gx = g
}
mt.bracketed = bracketed
mt.step = next
}