-
Notifications
You must be signed in to change notification settings - Fork 46
/
optimization.cpp
36 lines (33 loc) · 897 Bytes
/
optimization.cpp
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
const double eps = 1e-10;
// assume a unimodal or monotonous function
template <typename T, typename F>
double ternary_search(T lt, F f, double l, double r) {
double yl = f(l);
double yr = f(r);
double ym = f((l+r)/2);
if (lt(ym, lt(yl, yr) ? yl : yr))
return lt(yl, yr) ? yr : yl;
while( (r-l > eps && abs(r/l-1) > eps)
|| (abs(yl-yr) > eps && abs(yl/yr-1) > eps)) {
double m1 = (2*l + r) / 3;
double m2 = (l + 2*r) / 3;
double ym1 = f(m1);
double ym2 = f(m2);
if (lt(ym1, ym2)) {
l = m1;
yl = ym1;
} else {
r = m2;
yr = ym2;
}
}
return f((l+r)/2);
}
template <typename F>
double ternary_search_max(F f, double l, double r) {
return ternary_search(less<double>(), f, l, r);
}
template <typename F>
double ternary_search_min(F f, double l, double r) {
return ternary_search(greater<double>(), f, l, r);
}