-
Notifications
You must be signed in to change notification settings - Fork 1
/
gss.py
48 lines (39 loc) · 1 KB
/
gss.py
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
import math
INV_PHI = (math.sqrt(5) - 1) / 2 # 1 / phi
INV_PHI_SUARE = (3 - math.sqrt(5)) / 2 # 1 / phi^2
def gss(f, a, b, tol=1e-5):
"""
Golden-section search.
Given a function f with a single local minimum in
the interval [a,b], gss returns a subset interval
[c,d] that contains the minimum with d-c <= tol.
"""
a, b = min(a, b), max(a, b)
h = b - a
if h <= tol:
return a, b
# Required steps to achieve tolerance
n = int(math.ceil(math.log(tol / h) / math.log(INV_PHI)))
c = a + INV_PHI_SUARE * h
d = a + INV_PHI * h
yc = f(c)
yd = f(d)
for k in range(n - 1):
if yc < yd:
b = d
d = c
yd = yc
h = INV_PHI * h
c = a + INV_PHI_SUARE * h
yc = f(c)
else:
a = c
c = d
yc = yd
h = INV_PHI * h
d = a + INV_PHI * h
yd = f(d)
if yc < yd:
return a, d
else:
return c, b