-
Notifications
You must be signed in to change notification settings - Fork 173
/
optimization_generalist_demo.py
executable file
·298 lines (203 loc) · 7.71 KB
/
optimization_generalist_demo.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
###############################################################################
# EvoMan FrameWork - V1.0 2016 #
# DEMO : Neuroevolution - Genetic Algorithm with neural network. #
# Author: Karine Miras #
# karine.smiras@gmail.com #
###############################################################################
# imports framework
import sys
from evoman.environment import Environment
from demo_controller import player_controller
# imports other libs
import time
import numpy as np
from math import fabs,sqrt
import glob, os
# choose this for not using visuals and thus making experiments faster
headless = True
if headless:
os.environ["SDL_VIDEODRIVER"] = "dummy"
n_hidden_neurons = 10
experiment_name = 'multi_demo'
if not os.path.exists(experiment_name):
os.makedirs(experiment_name)
# initializes simulation in multi evolution mode, for multiple static enemies.
env = Environment(experiment_name=experiment_name,
enemies=[7,8],
multiplemode="yes",
playermode="ai",
player_controller=player_controller(n_hidden_neurons),
enemymode="static",
level=2,
speed="fastest",
visuals=False)
# default environment fitness is assumed for experiment
env.state_to_log() # checks environment state
#### Optimization for controller solution (best genotype-weights for phenotype-network): Ganetic Algorihm ###
ini = time.time() # sets time marker
# genetic algorithm params
run_mode = 'train' # train or test
# number of weights for multilayer with 10 hidden neurons.
n_vars = (env.get_num_sensors()+1)*n_hidden_neurons + (n_hidden_neurons+1)*5
dom_u = 1
dom_l = -1
npop = 100
gens = 30
mutation = 0.2
last_best = 0
np.random.seed(420)
# runs simulation
def simulation(env,x):
f,p,e,t = env.play(pcont=x)
return f
# normalizes
def norm(x,pfit_pop):
if ( max(pfit_pop) - min(pfit_pop) ) > 0:
x_norm = ( x - min(pfit_pop) )/( max(pfit_pop) - min(pfit_pop) )
else:
x_norm = 0
if x_norm <= 0:
x_norm = 0.0000000001
return x_norm
# evaluation
def evaluate(x):
return np.array(list(map(lambda y: simulation(env,y), x)))
# tournament
def tournament(pop):
c1 = np.random.randint(0,pop.shape[0], 1)
c2 = np.random.randint(0,pop.shape[0], 1)
if fit_pop[c1] > fit_pop[c2]:
return pop[c1][0]
else:
return pop[c2][0]
# limits
def limits(x):
if x>dom_u:
return dom_u
elif x<dom_l:
return dom_l
else:
return x
# crossover
def crossover(pop):
total_offspring = np.zeros((0,n_vars))
for p in range(0,pop.shape[0], 2):
p1 = tournament(pop)
p2 = tournament(pop)
n_offspring = np.random.randint(1,3+1, 1)[0]
offspring = np.zeros( (n_offspring, n_vars) )
for f in range(0,n_offspring):
# crossover
cross_prop = np.random.uniform(0,1)
offspring[f] = p1*cross_prop+p2*(1-cross_prop)
# mutation
for i in range(0,len(offspring[f])):
if np.random.uniform(0 ,1)<=mutation:
offspring[f][i] = offspring[f][i]+np.random.normal(0, 1)
offspring[f] = np.array(list(map(lambda y: limits(y), offspring[f])))
total_offspring = np.vstack((total_offspring, offspring[f]))
return total_offspring
# kills the worst genomes, and replace with new best/random solutions
def doomsday(pop,fit_pop):
worst = int(npop/4) # a quarter of the population
order = np.argsort(fit_pop)
orderasc = order[0:worst]
for o in orderasc:
for j in range(0,n_vars):
pro = np.random.uniform(0,1)
if np.random.uniform(0,1) <= pro:
pop[o][j] = np.random.uniform(dom_l, dom_u) # random dna, uniform dist.
else:
pop[o][j] = pop[order[-1:]][0][j] # dna from best
fit_pop[o]=evaluate([pop[o]])
return pop,fit_pop
# loads file with the best solution for testing
if run_mode =='test':
bsol = np.loadtxt(experiment_name+'/best.txt')
print( '\n RUNNING SAVED BEST SOLUTION \n')
env.update_parameter('speed','normal')
evaluate([bsol])
sys.exit(0)
# initializes population loading old solutions or generating new ones
if not os.path.exists(experiment_name+'/evoman_solstate'):
print( '\nNEW EVOLUTION\n')
pop = np.random.uniform(dom_l, dom_u, (npop, n_vars))
fit_pop = evaluate(pop)
best = np.argmax(fit_pop)
mean = np.mean(fit_pop)
std = np.std(fit_pop)
ini_g = 0
solutions = [pop, fit_pop]
env.update_solutions(solutions)
else:
print( '\nCONTINUING EVOLUTION\n')
env.load_state()
pop = env.solutions[0]
fit_pop = env.solutions[1]
best = np.argmax(fit_pop)
mean = np.mean(fit_pop)
std = np.std(fit_pop)
# finds last generation number
file_aux = open(experiment_name+'/gen.txt','r')
ini_g = int(file_aux.readline())
file_aux.close()
# saves results for first pop
file_aux = open(experiment_name+'/results.txt','a')
file_aux.write('\n\ngen best mean std')
print( '\n GENERATION '+str(ini_g)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)))
file_aux.write('\n'+str(ini_g)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)) )
file_aux.close()
# evolution
last_sol = fit_pop[best]
notimproved = 0
for i in range(ini_g+1, gens):
offspring = crossover(pop) # crossover
fit_offspring = evaluate(offspring) # evaluation
pop = np.vstack((pop,offspring))
fit_pop = np.append(fit_pop,fit_offspring)
best = np.argmax(fit_pop) #best solution in generation
fit_pop[best] = float(evaluate(np.array([pop[best] ]))[0]) # repeats best eval, for stability issues
best_sol = fit_pop[best]
# selection
fit_pop_cp = fit_pop
fit_pop_norm = np.array(list(map(lambda y: norm(y,fit_pop_cp), fit_pop))) # avoiding negative probabilities, as fitness is ranges from negative numbers
probs = (fit_pop_norm)/(fit_pop_norm).sum()
chosen = np.random.choice(pop.shape[0], npop , p=probs, replace=False)
chosen = np.append(chosen[1:],best)
pop = pop[chosen]
fit_pop = fit_pop[chosen]
# searching new areas
if best_sol <= last_sol:
notimproved += 1
else:
last_sol = best_sol
notimproved = 0
if notimproved >= 15:
file_aux = open(experiment_name+'/results.txt','a')
file_aux.write('\ndoomsday')
file_aux.close()
pop, fit_pop = doomsday(pop,fit_pop)
notimproved = 0
best = np.argmax(fit_pop)
std = np.std(fit_pop)
mean = np.mean(fit_pop)
# saves results
file_aux = open(experiment_name+'/results.txt','a')
print( '\n GENERATION '+str(i)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)))
file_aux.write('\n'+str(i)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)) )
file_aux.close()
# saves generation number
file_aux = open(experiment_name+'/gen.txt','w')
file_aux.write(str(i))
file_aux.close()
# saves file with the best solution
np.savetxt(experiment_name+'/best.txt',pop[best])
# saves simulation state
solutions = [pop, fit_pop]
env.update_solutions(solutions)
env.save_state()
fim = time.time() # prints total execution time for experiment
print( '\nExecution time: '+str(round((fim-ini)/60))+' minutes \n')
file = open(experiment_name+'/neuroended', 'w') # saves control (simulation has ended) file for bash loop file
file.close()
env.state_to_log() # checks environment state