-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsa.py
210 lines (176 loc) · 6.34 KB
/
gsa.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import random
from timeit import default_timer
import numpy as np
import pandas as pd
from math import exp, ceil, sqrt
from solution import Solution
G_0 = 100
ALPHA = 20
MAX_T = 1000
FIXED_TIME_SHORT = 5
FIXED_TIME_LONG = 20
OUTPUT_RATE = 100
OUTPUT_FILE = "out-"
def update(func, pop, t):
best, worst, G = 0, 0, 0
if func.max:
best = max(sol.fit for sol in pop)
worst = min(sol.fit for sol in pop)
else:
best = min(sol.fit for sol in pop)
worst = max(sol.fit for sol in pop)
G = G_0 * exp(-ALPHA * t/MAX_T)
return best, worst, G
def updateBest(func, pop, t):
#Ensure that k is always equal to or greater than 1 and smaller than population size
k = int(ceil(len(pop) * (1 - t/MAX_T)))
if (func.max):
pop.sort(key=lambda sol: sol.fit, reverse=True)
else:
pop.sort(key=lambda sol: sol.fit)
return pop[0:k]
def calcMasses(pop, best, worst):
div = best - worst
threshold = 1e-15
s = 0
if abs(div) < threshold:
for sol in pop:
sol.mass = 1
s += 1
else:
for sol in pop:
mass = abs(sol.fit - worst)/div
sol.mass = mass
s += mass
for sol in pop:
sol.mass /= s
def calcMovement(func, pop):
for sol in pop:
sol.vel *= np.random.random(func.dims)
sol.vel += sol.force
sol.pos += sol.vel
def reinitialiseSolutions(func, pop):
for i in range(len(pop)):
if not func.inside(pop[i]):
pop[i] = func.generateSolution()
def initialise(func, size):
return [func.generateSolution() for i in range(size)]
def formatProgress(t, time, pop, columns):
data = list(map(str,[t,time] + [sol.fit for sol in pop]))
return pd.Series(dict(zip(columns,data)))
def output(columns, best, t, force, func, conditions, time):
data = list(map(str,[func.num, force.desc, best, t, time]))
return pd.Series(dict(zip(columns,data)))
def getDist(sol, kth):
return sqrt(np.sum((sol-kth)**2))
def calculateBasicForce(dims, sol, kbest, G):
pos = sol.pos
result = np.zeros(dims)
for kth in kbest:
if sol is not kth:
kth_pos = kth.pos
dist = getDist(pos,kth_pos)
result += np.random.random(dims) * G * kth.mass * (kth_pos - pos) / dist
return result
def basicForce(func, pop, kbest, G):
for sol in pop:
sol.force = calculateBasicForce(func.dims, sol, kbest, G)
def iterationCondition(step, time):
return step < MAX_T
def iterationUpdate(func, pop, step, time):
best, worst, G = 0, 0, 0
if func.max:
best = max(sol.fit for sol in pop)
worst = min(sol.fit for sol in pop)
else:
best = min(sol.fit for sol in pop)
worst = max(sol.fit for sol in pop)
G = G_0 * exp(-ALPHA * step/MAX_T)
k = int(ceil(len(pop) * (1 - step/MAX_T)))
if (func.max):
pop.sort(key=lambda sol: sol.fit, reverse=True)
else:
pop.sort(key=lambda sol: sol.fit)
return best, worst, G, pop[0:k]
def timeShortCondition(step, time):
return time < FIXED_TIME_SHORT
def timeShortUpdate(func, pop, step, time):
best, worst, G = 0, 0, 0
if func.max:
best = max(sol.fit for sol in pop)
worst = min(sol.fit for sol in pop)
else:
best = min(sol.fit for sol in pop)
worst = max(sol.fit for sol in pop)
G = G_0 * exp(-ALPHA * time/FIXED_TIME_SHORT)
k = int(ceil(len(pop) * (1 - time/FIXED_TIME_SHORT)))
if (func.max):
pop.sort(key=lambda sol: sol.fit, reverse=True)
else:
pop.sort(key=lambda sol: sol.fit)
return best, worst, G, pop[0:k]
def timeLongCondition(step, time):
return time < FIXED_TIME_LONG
def timeLongUpdate(func, pop, step, time):
best, worst, G = 0, 0, 0
if func.max:
best = max(sol.fit for sol in pop)
worst = min(sol.fit for sol in pop)
else:
best = min(sol.fit for sol in pop)
worst = max(sol.fit for sol in pop)
G = G_0 * exp(-ALPHA * time/FIXED_TIME_LONG)
k = int(ceil(len(pop) * (1 - time/FIXED_TIME_LONG)))
if (func.max):
pop.sort(key=lambda sol: sol.fit, reverse=True)
else:
pop.sort(key=lambda sol: sol.fit)
return best, worst, G, pop[0:k]
#takes the function, population size, force calculator, stop condition, seed
def gsa(function, pop_size, force, conditions, columns, outputProgress = True, suffix = "", seed = None):
if seed is not None:
random.seed(seed)
np.random.seed(seed)
population = initialise(function, pop_size)
function.getFitnesses(population)
t = 0
start = default_timer()
current_time = default_timer()
if outputProgress:
#Track progress
progColumns = ["t","time"] + ["fitness" + str(i) for i in range(1,pop_size + 1)]
progress = pd.DataFrame(columns = progColumns)
progress = progress.append(formatProgress(t, 0, population, progColumns), ignore_index = True)
while (conditions(t,current_time - start)):
#Update best, worst, G and kbest
best, worst, G, kbest = conditions.update(function, population, t, current_time - start)
#Mass calculations
calcMasses(population, best, worst)
#Force calculations
force(function, population, kbest, G)
#Calculate velocity and position
calcMovement(function, population)
#Remove solutions which are out of bounds
reinitialiseSolutions(function, population)
t += 1
#Evaluate fitnesses
function.getFitnesses(population)
current_time = default_timer()
if (outputProgress and t % OUTPUT_RATE == 0):
#output current data
progress = progress.append(formatProgress(t, current_time- start, population, progColumns), ignore_index = True)
if outputProgress:
progress.to_csv(function.desc + suffix + ".csv", index = False)
return output(columns, best, t, force, function, conditions, current_time - start)
def main():
pass
if __name__ == '__main__':
main()
else:
iterationCondition.desc = "fixed-it"
timeShortCondition.desc = "fixed-time-short"
timeLongCondition.desc = "fixed-time-long"
basicForce.desc = "GSA"
iterationCondition.update = iterationUpdate
timeShortCondition.update = timeShortUpdate
timeLongCondition.update = timeLongUpdate