/
BrentMin.cs
168 lines (152 loc) · 5.01 KB
/
BrentMin.cs
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
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/
using System;
using static MechJebLib.Utils.Statics;
using static System.Math;
#nullable enable
// ReSharper disable CompareOfFloatsByEqualityOperator
namespace MechJebLib.Core
{
public static class BrentMin
{
// Brent's 1-dimensional derivative-free local minimization method
//
// Uses golden section search and successive parabolic interpolation.
//
// f - 1-dimensional BrentFun function
// xmin - lower endpoint of the interval
// xmax - upper endpoint of the interval
// rtol - tolerance to solve to (1e-4 or something like that)
// o - object to be passed to BrentFun for extra data (may be null)
// maxiter - cap on iterations (will throw TimeoutException if exceeded)
// fx - output of solved value
// y - output of the function at the solved value
//
public static (double x, double fx) Solve(Func<double, object?, double> f, double xmin, double xmax, object? o = null, double rtol = 1e-9,
int maxiter = 50)
{
double c = 0.5 * (3.0 - Sqrt(5.0)); // C is the square of the inverse of the golden ratio.
double d = 0;
double e = 0.0;
double eps = Sqrt(EPS);
double sa = xmin;
double sb = xmax;
double x = sa + c * (xmax - xmin);
double w = x;
double v = w;
double fx = f(x, o);
double fw = fx;
double fv = fw;
int i = 0;
while (true)
{
double m = 0.5 * (sa + sb);
double t = eps * Abs(x) + rtol;
double t2 = 2.0 * t;
//
// Check the stopping criterion.
//
if (Abs(x - m) <= t2 - 0.5 * (sb - sa)) break;
//
// Fit a parabola.
//
double r = 0.0;
double q = r;
double p = q;
if (t < Abs(e))
{
r = (x - w) * (fx - fv);
q = (x - v) * (fx - fw);
p = (x - v) * q - (x - w) * r;
q = 2.0 * (q - r);
if (0.0 < q) p = -p;
q = Abs(q);
r = e;
e = d;
}
double u;
if (Abs(p) < Abs(0.5 * q * r) &&
q * (sa - x) < p &&
p < q * (sb - x))
{
//
// Take the parabolic interpolation step.
//
d = p / q;
u = x + d;
//
// F must not be evaluated too close to A or B.
//
if (u - sa < t2 || sb - u < t2)
{
if (x < m)
d = t;
else
d = -t;
}
}
//
// A golden-section step.
//
else
{
if (x < m)
e = sb - x;
else
e = sa - x;
d = c * e;
}
//
// F must not be evaluated too close to X.
//
if (t <= Abs(d))
u = x + d;
else if (0.0 < d)
u = x + t;
else
u = x - t;
double fu = f(u, o);
//
// Update A, B, V, W, and X.
//
if (fu <= fx)
{
if (u < x)
sb = x;
else
sa = x;
v = w;
fv = fw;
w = x;
fw = fx;
x = u;
fx = fu;
}
else
{
if (u < x)
sa = u;
else
sb = u;
if (fu <= fw || w == x)
{
v = w;
fv = fw;
w = u;
fw = fu;
}
else if (fu <= fv || v == x || v == w)
{
v = u;
fv = fu;
}
}
if (i++ >= maxiter && maxiter > 0)
throw new TimeoutException("Brent's minimization method: maximum iterations exceeded");
}
return (x, fx);
}
}
}