-
Notifications
You must be signed in to change notification settings - Fork 11
/
brentminimize.go
284 lines (275 loc) · 6.48 KB
/
brentminimize.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
package optimize
import (
"math"
)
type bracketer struct {
growLimit float64
maxIter int
}
// Bracket the minimum of the function.
// Given a function and distinct initial points, search in the
// downhill direction (as defined by the initital points) and return
// new points xa, xb, xc that bracket the minimum of the function
// f(xa) > f(xb) < f(xc). It doesn't always mean that obtained
// solution will satisfy xa<=x<=xb
func (b bracketer) bracket(f func(float64) float64, xa0, xb0 float64) (xa, xb, xc, fa, fb, fc float64, funcalls int) {
var (
tmp1, tmp2, val, denom, w, wlim, fw float64
iter int
)
_gold := 1.618034 //# golden ratio: (1.0+sqrt(5.0))/2.0
_verysmallNum := 1e-21
xa, xb = xa0, xb0
fa, fb = f(xa), f(xb)
if fa < fb {
xa, xb = xb, xa
fa, fb = fb, fa
}
xc = xb + _gold*(xb-xa)
fc = f(xc)
funcalls = 3
iter = 0
for fc < fb {
tmp1 = (xb - xa) * (fb - fc)
tmp2 = (xb - xc) * (fb - fa)
val = tmp2 - tmp1
if math.Abs(val) < _verysmallNum {
denom = 2.0 * _verysmallNum
} else {
denom = 2.0 * val
}
w = xb - ((xb-xc)*tmp2-(xb-xa)*tmp1)/denom
wlim = xb + b.growLimit*(xc-xb)
if iter > b.maxIter {
panic("bracket: Too many iterations.")
}
iter++
if (w-xc)*(xb-w) > 0.0 {
fw = f(w)
funcalls++
if fw < fc {
xa = xb
xb = w
fa = fb
fb = fw
return xa, xb, xc, fa, fb, fc, funcalls
} else if fw > fb {
xc = w
fc = fw
return xa, xb, xc, fa, fb, fc, funcalls
}
w = xc + _gold*(xc-xb)
fw = f(w)
funcalls++
} else if (w-wlim)*(wlim-xc) >= 0.0 {
w = wlim
fw = f(w)
funcalls++
} else if (w-wlim)*(xc-w) > 0.0 {
fw = f(w)
funcalls++
if fw < fc {
xb = xc
xc = w
w = xc + _gold*(xc-xb)
fb = fc
fc = fw
fw = f(w)
funcalls++
}
} else {
w = xc + _gold*(xc-xb)
fw = f(w)
funcalls++
}
xa = xb
xb = xc
xc = w
fa = fb
fb = fc
fc = fw
}
return xa, xb, xc, fa, fb, fc, funcalls
}
// BrentMinimizer is the translation of class Brent in scipy/optimize/optimize.py
// Uses inverse parabolic interpolation when possible to speed up convergence of golden section method.
type BrentMinimizer struct {
Func func(float64) float64
Tol float64
Maxiter int
mintol float64
cg float64
Xmin float64
Fval float64
Iter, Funcalls int
Brack []float64
bracketer
FnMaxFev func(int) bool
}
// NewBrentMinimizer returns an initialized *BrentMinimizer
func NewBrentMinimizer(fun func(float64) float64, tol float64, maxiter int, fnMaxFev func(int) bool) *BrentMinimizer {
return &BrentMinimizer{
Func: fun,
Tol: tol,
Maxiter: maxiter,
mintol: 1.0e-11,
cg: 0.3819660,
bracketer: bracketer{growLimit: 110, maxIter: 1000},
FnMaxFev: fnMaxFev,
}
}
// SetBracket can be used to set initial bracket of BrentMinimizer. len(brack) must be between 1 and 3 inclusive.
func (bm *BrentMinimizer) SetBracket(brack []float64) {
bm.Brack = make([]float64, len(brack))
copy(bm.Brack, brack)
}
func (bm *BrentMinimizer) getBracketInfo() (float64, float64, float64, float64, float64, float64, int) {
fun := bm.Func
brack := bm.Brack
var xa, xb, xc float64
var fa, fb, fc float64
var funcalls int
switch len(brack) {
case 0:
xa, xb, xc, fa, fb, fc, funcalls = bm.bracketer.bracket(fun, 0, 1)
case 2:
xa, xb, xc, fa, fb, fc, funcalls = bm.bracketer.bracket(fun, brack[0], brack[1])
case 3:
xa, xb, xc = brack[0], brack[1], brack[2]
if xa > xc {
xa, xc = xc, xa
}
fa, fb, fc = fun(xa), fun(xb), fun(xc)
if !((fb < fa) && (fb < fc)) {
panic("getBracketInfo: not a brackeding interval")
}
funcalls = 3
}
return xa, xb, xc, fa, fb, fc, funcalls
}
// Optimize search the value of X minimizing bm.Func
func (bm *BrentMinimizer) Optimize() (x, fx float64, iter, funcalls int) {
var xa, xb, xc, fb, _mintol, _cg, v, fv, w, fw, a, b, deltax, tol1, tol2, xmid, rat, tmp1, tmp2, p, dxTemp, u, fu float64
if bm.FnMaxFev == nil {
bm.FnMaxFev = func(int) bool { return false }
}
//# set up for optimization
f := bm.Func
xa, xb, xc, _, fb, _, funcalls = bm.getBracketInfo()
_mintol = bm.mintol
_cg = bm.cg
// #################################
// #BEGIN CORE ALGORITHM
//#################################
//x = w = v = xb
v, w, x = xb, xb, xb
//fw = fv = fx = func(*((x,) + self.args))
fx = fb
fv, fw = fx, fx
if xa < xc {
a = xa
b = xc
} else {
a = xc
b = xa
}
deltax = 0.0
funcalls++
iter = 0
for iter < bm.Maxiter && !bm.FnMaxFev(funcalls) {
tol1 = bm.Tol*math.Abs(x) + _mintol
tol2 = 2.0 * tol1
xmid = 0.5 * (a + b)
//# check for convergence
if math.Abs(x-xmid) < (tol2 - 0.5*(b-a)) {
break
}
// # XXX In the first iteration, rat is only bound in the true case
// # of this conditional. This used to cause an UnboundLocalError
// # (gh-4140). It should be set before the if (but to what?).
if math.Abs(deltax) <= tol1 {
if x >= xmid {
deltax = a - x //# do a golden section step
} else {
deltax = b - x
}
rat = _cg * deltax
} else { //# do a parabolic step
tmp1 = (x - w) * (fx - fv)
tmp2 = (x - v) * (fx - fw)
p = (x-v)*tmp2 - (x-w)*tmp1
tmp2 = 2.0 * (tmp2 - tmp1)
if tmp2 > 0.0 {
p = -p
}
tmp2 = math.Abs(tmp2)
dxTemp = deltax
deltax = rat
//# check parabolic fit
if (p > tmp2*(a-x)) && (p < tmp2*(b-x)) &&
(math.Abs(p) < math.Abs(0.5*tmp2*dxTemp)) {
rat = p * 1.0 / tmp2 //# if parabolic step is useful.
u = x + rat
if (u-a) < tol2 || (b-u) < tol2 {
if xmid-x >= 0 {
rat = tol1
} else {
rat = -tol1
}
}
} else {
if x >= xmid {
deltax = a - x //# if it's not do a golden section step
} else {
deltax = b - x
}
rat = _cg * deltax
}
}
if math.Abs(rat) < tol1 { //# update by at least tol1
if rat >= 0 {
u = x + tol1
} else {
u = x - tol1
}
} else {
u = x + rat
}
fu = f(u) //# calculate new output value
funcalls++
if fu > fx { //# if it's bigger than current
if u < x {
a = u
} else {
b = u
}
if (fu <= fw) || (w == x) {
v = w
w = u
fv = fw
fw = fu
} else if (fu <= fv) || (v == x) || (v == w) {
v = u
fv = fu
}
} else {
if u >= x {
a = x
} else {
b = x
}
v = w
w = x
x = u
fv = fw
fw = fx
fx = fu
}
iter++
}
// #################################
// #END CORE ALGORITHM
// #################################
bm.Xmin, bm.Fval, bm.Iter, bm.Funcalls = x, fx, iter, funcalls
return
}