-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_design.py
307 lines (241 loc) · 17.9 KB
/
experiment_design.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import numpy as np
import pandas as pd
import progressbar
from evoAlg_operators import evoALg_edgecross, evoALg_singlecross
# defining experiment design functions for both CRSV functions
def singleCRSV(pop_size,tourn_size, cost_matrix, seed):
'''
Performs len(seed) number of trails on the evolutionary algorithm which varies the mutation operator and replacement operator
but keeps the pop_size and tourn_size constant all done with the single point crossover function.
Parameters:
pop_size: population size
tourn_size: tournament size
cost_matrix: cost matrix containing cost for intercity travel
seed: Array of seeds for each trial.
Returns:
A dataframe containing the fitness results per iteration for each combination for each seed and a dataframe which returns the best solution,
its fitness and execution time for each combination for each seed.
'''
N_iter = 10000 # number of iterations for each algorithm
# init empty dfs with mut_types and replace_types
# this data is just for us to get the average fitness and plot convergence curves
df_swap1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'swap', 'repl_func':'1st Weakest'})
df_swapWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'swap', 'repl_func':'Weakest'})
df_insert1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'insert', 'repl_func':'1st Weakest'})
df_insertWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'insert', 'repl_func':'Weakest'})
df_inversion1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'inversion', 'repl_func':'1st Weakest'})
df_inversionWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'inversion', 'repl_func':'Weakest'})
df_scramble1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'scramble', 'repl_func':'1st Weakest'})
df_scrambleWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'scramble', 'repl_func':'Weakest'})
# dataframe for storing best solution and execution time
best_results = pd.DataFrame(columns=['seed','mut_type','repl_type','solution','fitness','exec_time'])
## Progress bar
print("")
print("Pop: {}, Tourn: {}, CRSV: SinglePt".format(pop_size, tourn_size))
#widgets = ['Progress: ', progressbar.Percentage(), ' ', progressbar.Bar()]
#progress = progressbar.ProgressBar(widgets=widgets, maxval = N_iter*len(seed)*8).start()
evals = int(0)
for N in seed:
# - - - - - - - S W A P M U T A T I O N - - - - - - - - -
# SWAP mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'swap', 'replace 1st worst', set_seed=N)
df_swap1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'swap', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# SWAP mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'swap', 'replace worst', set_seed=N)
df_swapWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'swap', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# - - - - - - - I N S E R T M U T A T I O N - - - - - - - - -
# INSERT mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'insert', 'replace 1st worst', set_seed=N)
df_insert1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'insert', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# INSERT mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'insert', 'replace worst', set_seed=N)
df_insertWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'insert', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# - - - - - - - I N V E R S I O N M U T A T I O N - - - - - - - - -
# INVERSION mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'inversion', 'replace 1st worst', set_seed=N)
df_inversion1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'inversion', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# INVERSION mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'inversion', 'replace worst', set_seed=N)
df_inversionWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'inversion', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# - - - - - - - S C R A M B L E M U T A T I O N - - - - - - - - -
# SCRAMBLE mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'scramble', 'replace 1st worst', set_seed=N)
df_scramble1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'scramble', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# SCRAMBLE mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_singlecross(cost_matrix, pop_size, tourn_size, 'scramble', 'replace worst', set_seed=N)
df_scrambleWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'scramble', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
print("Seed {} complete!".format(N))
# add CRSV type to col
all_dfs = pd.concat([df_swap1Wk,df_swapWeak,df_insert1Wk,df_insertWeak,df_inversion1Wk,df_inversionWeak,df_scramble1Wk,df_scrambleWeak], ignore_index=True)
all_dfs['crsv_type'] = 'Single Point'
best_results['crsv_type'] = 'Single Point'
# end progress bar
#progress.finish()
# return dataframes
return all_dfs, best_results
def edgeCRSV(pop_size,tourn_size, cost_matrix, seed):
'''
Performs len(seed) number of trails on the evolutionary algorithm which varies the mutation operator and replacement operator
but keeps the pop_size and tourn_size constant all done with the edge crossover function.
Parameters:
pop_size: population size
tourn_size: tournament size
cost_matrix: cost matrix containing cost for intercity travel
seed: Array of seeds for each trial.
Returns:
A dataframe containing the fitness results per iteration for each combination for each seed and a dataframe which returns the best solution,
its fitness and execution time for each combination for each seed.
'''
N_iter = 10000 # number of iterations for each algorithm
# init empty dfs with mut_types and replace_types
# this data is just for us to get the average fitness and plot convergence curves
df_swap1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'swap', 'repl_func':'1st Weakest'})
df_swapWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'swap', 'repl_func':'Weakest'})
df_insert1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'insert', 'repl_func':'1st Weakest'})
df_insertWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'insert', 'repl_func':'Weakest'})
df_inversion1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'inversion', 'repl_func':'1st Weakest'})
df_inversionWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'inversion', 'repl_func':'Weakest'})
df_scramble1Wk = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'scramble', 'repl_func':'1st Weakest'})
df_scrambleWeak = pd.DataFrame({'iteration':np.arange(N_iter),'mut_func':'scramble', 'repl_func':'Weakest'})
# dataframe for storing best solution and execution time
best_results = pd.DataFrame(columns=['seed','mut_type','repl_type','solution','fitness','exec_time'])
## Progress bar
print("")
print("Pop: {}, Tourn: {}, CRSV: Edge".format(pop_size, tourn_size))
#widgets = ['Progress: ', progressbar.Percentage(), ' ', progressbar.Bar()]
#progress = progressbar.ProgressBar(widgets=widgets, maxval = N_iter*len(seed)*8).start()
evals = int(0)
for N in seed:
# - - - - - - - S W A P M U T A T I O N - - - - - - - - -
# SWAP mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'swap', 'replace 1st worst', set_seed=N)
df_swap1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'swap', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# SWAP mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'swap', 'replace worst', set_seed=N)
df_swapWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'swap', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# - - - - - - - I N S E R T M U T A T I O N - - - - - - - - -
# INSERT mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'insert', 'replace 1st worst', set_seed=N)
df_insert1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'insert', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# INSERT mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'insert', 'replace worst', set_seed=N)
df_insertWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'insert', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# - - - - - - - I N V E R S I O N M U T A T I O N - - - - - - - - -
# INVERSION mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'inversion', 'replace 1st worst', set_seed=N)
df_inversion1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'inversion', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# INVERSION mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'inversion', 'replace worst', set_seed=N)
df_inversionWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'inversion', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# - - - - - - - S C R A M B L E M U T A T I O N - - - - - - - - -
# SCRAMBLE mutation with 1st weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'scramble', 'replace 1st worst', set_seed=N)
df_scramble1Wk[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'scramble', 'repl_type':'1st Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
# SCRAMBLE mutation with Weakest replacement
best_fit, exec_time, best_sol = evoALg_edgecross(cost_matrix, pop_size, tourn_size, 'scramble', 'replace worst', set_seed=N)
df_scrambleWeak[N] = pd.Series(best_fit) # adding the fitness/iter to df
# concating the best solution and execution time of algorithm to df
_ = pd.DataFrame({'seed':N, 'mut_type':'scramble', 'repl_type':'Weakest','solution': [best_sol['solution']], 'fitness': best_sol['fitness'], 'exec_time':exec_time})
best_results = pd.concat([best_results, _], ignore_index=True)
## progress bar update
evals +=N_iter
#progress.update(evals)
print("Seed {} complete!".format(N))
# add CRSV type to col
all_dfs = pd.concat([df_swap1Wk,df_swapWeak,df_insert1Wk,df_insertWeak,df_inversion1Wk,df_inversionWeak,df_scramble1Wk,df_scrambleWeak], ignore_index=True)
all_dfs['crsv_type'] = 'Edge'
best_results['crsv_type'] = 'Edge'
# end progress bar
#progress.finish()
# return dataframes
return all_dfs, best_results