1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.optim.univariate;
23
24 import org.hipparchus.exception.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.optim.ConvergenceChecker;
27 import org.hipparchus.optim.nonlinear.scalar.GoalType;
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.Precision;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class BrentOptimizer extends UnivariateOptimizer {
48
49
50
51 private static final double GOLDEN_SECTION = 0.5 * (3 - FastMath.sqrt(5));
52
53
54
55 private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d);
56
57
58
59 private final double relativeThreshold;
60
61
62
63 private final double absoluteThreshold;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public BrentOptimizer(double rel,
82 double abs,
83 ConvergenceChecker<UnivariatePointValuePair> checker) {
84 super(checker);
85
86 if (rel < MIN_RELATIVE_TOLERANCE) {
87 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
88 rel, MIN_RELATIVE_TOLERANCE);
89 }
90 if (abs <= 0) {
91 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
92 abs, 0);
93 }
94
95 relativeThreshold = rel;
96 absoluteThreshold = abs;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public BrentOptimizer(double rel,
114 double abs) {
115 this(rel, abs, null);
116 }
117
118
119 @Override
120 protected UnivariatePointValuePair doOptimize() {
121 final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
122 final double lo = getMin();
123 final double mid = getStartValue();
124 final double hi = getMax();
125
126
127 final ConvergenceChecker<UnivariatePointValuePair> checker
128 = getConvergenceChecker();
129
130 double a;
131 double b;
132 if (lo < hi) {
133 a = lo;
134 b = hi;
135 } else {
136 a = hi;
137 b = lo;
138 }
139
140 double x = mid;
141 double v = x;
142 double w = x;
143 double d = 0;
144 double e = 0;
145 double fx = computeObjectiveValue(x);
146 if (!isMinim) {
147 fx = -fx;
148 }
149 double fv = fx;
150 double fw = fx;
151
152 UnivariatePointValuePair previous = null;
153 UnivariatePointValuePair current
154 = new UnivariatePointValuePair(x, isMinim ? fx : -fx);
155
156 UnivariatePointValuePair best = current;
157
158 while (true) {
159 final double m = 0.5 * (a + b);
160 final double tol1 = relativeThreshold * FastMath.abs(x) + absoluteThreshold;
161 final double tol2 = 2 * tol1;
162
163
164 final boolean stop = FastMath.abs(x - m) <= tol2 - 0.5 * (b - a);
165 if (!stop) {
166 double u;
167
168 if (FastMath.abs(e) > tol1) {
169 double r = (x - w) * (fx - fv);
170 double q = (x - v) * (fx - fw);
171 double p = (x - v) * q - (x - w) * r;
172 q = 2 * (q - r);
173
174 if (q > 0) {
175 p = -p;
176 } else {
177 q = -q;
178 }
179
180 r = e;
181 e = d;
182
183 if (p > q * (a - x) &&
184 p < q * (b - x) &&
185 FastMath.abs(p) < FastMath.abs(0.5 * q * r)) {
186
187 d = p / q;
188 u = x + d;
189
190
191 if (u - a < tol2 || b - u < tol2) {
192 if (x <= m) {
193 d = tol1;
194 } else {
195 d = -tol1;
196 }
197 }
198 } else {
199
200 if (x < m) {
201 e = b - x;
202 } else {
203 e = a - x;
204 }
205 d = GOLDEN_SECTION * e;
206 }
207 } else {
208
209 if (x < m) {
210 e = b - x;
211 } else {
212 e = a - x;
213 }
214 d = GOLDEN_SECTION * e;
215 }
216
217
218 if (FastMath.abs(d) < tol1) {
219 if (d >= 0) {
220 u = x + tol1;
221 } else {
222 u = x - tol1;
223 }
224 } else {
225 u = x + d;
226 }
227
228 double fu = computeObjectiveValue(u);
229 if (!isMinim) {
230 fu = -fu;
231 }
232
233
234 previous = current;
235 current = new UnivariatePointValuePair(u, isMinim ? fu : -fu);
236 best = best(best,
237 best(previous,
238 current,
239 isMinim),
240 isMinim);
241
242 if (checker != null && checker.converged(getIterations(), previous, current)) {
243 return best;
244 }
245
246
247 if (fu <= fx) {
248 if (u < x) {
249 b = x;
250 } else {
251 a = x;
252 }
253 v = w;
254 fv = fw;
255 w = x;
256 fw = fx;
257 x = u;
258 fx = fu;
259 } else {
260 if (u < x) {
261 a = u;
262 } else {
263 b = u;
264 }
265 if (fu <= fw ||
266 Precision.equals(w, x)) {
267 v = w;
268 fv = fw;
269 w = u;
270 fw = fu;
271 } else if (fu <= fv ||
272 Precision.equals(v, x) ||
273 Precision.equals(v, w)) {
274 v = u;
275 fv = fu;
276 }
277 }
278 } else {
279 return best(best,
280 best(previous,
281 current,
282 isMinim),
283 isMinim);
284 }
285
286 incrementIterationCount();
287 }
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301 private UnivariatePointValuePair best(UnivariatePointValuePair a,
302 UnivariatePointValuePair b,
303 boolean isMinim) {
304 if (a == null) {
305 return b;
306 }
307 if (b == null) {
308 return a;
309 }
310
311 if (isMinim) {
312 return a.getValue() <= b.getValue() ? a : b;
313 } else {
314 return a.getValue() >= b.getValue() ? a : b;
315 }
316 }
317 }