-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleapfrog.py
108 lines (84 loc) · 3.04 KB
/
leapfrog.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
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
import numpy as np
from test_functions import *
from utilities import *
@Counter.count
def leapfrog(f, players=20, tol=1e-7, max_iter=1000, loc=None, verbose=False, plotting=False):
if loc:
trange = get_range(f)
xy_range = [loc[0] + (np.abs(trange[0]) * .1),
loc[0] - (np.abs(trange[0]) * .1),
loc[1] + (np.abs(trange[1]) * .1),
loc[1] - (np.abs(trange[1]) * .1)]
else:
xy_range = get_range(f)
# initialize two arrays to hold our players for x and y coordinates
px = np.zeros(players)
py = np.zeros(players)
# initialize fitness function for each player
pf = np.zeros(players)
# create initial conditions
for i in range(0, players):
x = np.random.uniform(xy_range[0], xy_range[1])
y = np.random.uniform(xy_range[2], xy_range[3])
test_val = f(x, y)
px[i] = x
py[i] = y
pf[i] = test_val
# now we can begin the main loop
for iteration in range(0, max_iter):
# get the highest(worst) and lowest(best) players:
# initialize worst player randomly which helps incase the floor is flat (like easom function)
Nhigh = np.random.randint(0, players) # we assign one as the worst player randomly
fhigh = pf[Nhigh]
#initialize best player as first sport incase floor is flat
Nlow = 1
flow = pf[Nlow]
for p in range(0, players):
test_val = pf[p]
if test_val > fhigh:
Nhigh = p
fhigh = test_val
if test_val < flow:
Nlow = p
flow = test_val
# store x and y high values
xhigh = px[Nhigh]
yhigh = py[Nhigh]
# store x and y low values
xlow = px[Nlow]
ylow = py[Nlow]
# begin leapfrogging process
dx = xlow - xhigh
dy = ylow - yhigh
# jump for x and y directions separately
xhigh = xlow - np.random.uniform() * (xhigh - xlow)
yhigh = ylow - np.random.uniform() * (yhigh - ylow)
test_val = f(xhigh, yhigh)
# update arrays
px[Nhigh] = xhigh
py[Nhigh] = yhigh
pf[Nhigh] = test_val
# if we have a new best (leap was successful
if test_val < flow:
Nlow = Nhigh
flow = pf[Nlow]
xlow = xhigh
ylow = yhigh
if test_val <= fhigh:
# initialize worst player randomly which helps incase the floor is flat (like easom function)
Nhigh = np.random.randint(0, players) # we assign one as the worst player randomly
fhigh = pf[Nhigh]
for k in range(0, players):
test_val = pf[k]
# if new worst
if test_val > fhigh:
Nhigh = k
fhigh = test_val
xhigh = px[Nhigh]
yhigh = py[Nhigh]
if (np.abs(dx) < tol or np.abs(dy) < tol):
break
if verbose:
return flow, (xlow, ylow)
else:
return flow