-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooke_jeeves.py
More file actions
103 lines (75 loc) · 2.32 KB
/
hooke_jeeves.py
File metadata and controls
103 lines (75 loc) · 2.32 KB
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
import numpy as np
from test_functions import *
from utilities import *
def local_search(x, y, f, delta, xy_range):
best = f(x,y)
current = f(x+delta, y)
flag = False
if current < best and x+delta < xy_range[1] and x+delta > xy_range[0]:
best = current
xf = x + delta
yf = y
flag = True
current = f(x, y+delta)
if current < best and y+delta < xy_range[3] and y+delta > xy_range[2]:
best = current
xf = x
yf = y + delta
flag = True
current = f(x-delta, y)
if current < best and x-delta < xy_range[1] and x-delta > xy_range[0]:
best = current
xf = x - delta
yf = y
flag = True
current = f(x, y-delta)
if current < best and y-delta < xy_range[3] and y-delta > xy_range[2]:
best = current
xf = x
yf = y - delta
flag = True
if flag == True:
return xf, yf
else:
return x, y
@Counter.count
def hooke_jeeves(f, tol=1e-7, max_iter=100, loc=None, verbose=False, plotting=False):
trange = get_range(f)
if loc:
xy_range = [trange[0] + (np.abs(trange[0])/2),
trange[0] - (np.abs(trange[0])/2),
trange[1] + (np.abs(trange[1])/2),
trange[1] - (np.abs(trange[1])/2)]
x = loc[0]
y = loc[1]
else:
# get search space range
xy_range = trange
# get random initial points
x = np.random.uniform(xy_range[0], xy_range[1])
y = np.random.uniform(xy_range[2], xy_range[3])
# the contraction ratio
alpha = 0.5
# set delta as 20% of the size of the search space
delta = max(np.abs(xy_range[0] - xy_range[1]), np.abs(xy_range[2] - xy_range[3])) * .20
last_x = x
last_y = y
# set initial best minimum
best = f(x,y)
for i in range(0, max_iter):
#print(best)
current = f(x,y)
if current < best:
best = current
last_x = x
last_y = y
x, y = local_search(x, y, f, delta, xy_range)
# if we dont have a new best point reduce the delta of our pattern search
if last_x == x and last_y == y:
delta = delta * alpha
if delta < tol:
break
if verbose:
return best, (x, y)
else:
return best