forked from karinemiras/evoman_framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimization_coevolution_demo.py
executable file
·219 lines (150 loc) · 6.03 KB
/
optimization_coevolution_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
###############################################################################
# EvoMan FrameWork - V1.0 2016 #
# DEMO : Neuroevolution - Genetic Algorithm with neural network. #
# Author: Karine Miras #
###############################################################################
# imports framework
import sys
sys.path.insert(0, 'evoman')
from environment import Environment
from demo_controller import player_controller, enemy_controller
# imports other libs
import time
import numpy as np
import os
class environm(Environment):
# implements fitness function
def fitness_single(self):
if self.contacthurt == "player":
return 0.9 * (100 - self.get_enemylife()) + 0.1 * self.get_playerlife() - np.log(self.get_time())
else:
return 0.9 * (100 - self.get_playerlife()) + 0.1 * self.get_enemylife() - np.log(self.get_time())
experiment_name = 'co_demo'
if not os.path.exists(experiment_name):
os.makedirs(experiment_name)
n_hidden_neurons = 10
# initializes simulation for coevolution evolution mode.
env = environm(experiment_name=experiment_name,
enemies=[2],
playermode="ai",
enemymode="ai",
player_controller=player_controller(n_hidden_neurons),
enemy_controller=enemy_controller(n_hidden_neurons),
level=2,
speed="fastest")
env.state_to_log() # checks environment state
#### Optimization for controller solution (best genotype/weights for perceptron 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 = 100
mutation = 0.2
last_best = 0
# runs simulation
def simulation(env, x1, x2):
f, p, e, t = env.play(pcont=x1, econt=x2)
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(x1, x2):
return np.array(list(map(lambda y: simulation(env, y, x2), x1)))
# tournament
def tournament(pop, fit_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, fit_pop):
total_offspring = np.zeros((0, n_vars))
for p in range(0, pop.shape[0], 2):
p1 = tournament(pop, fit_pop)
p2 = tournament(pop, fit_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):
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
print('\nNEW EVOLUTION\n')
pop_p = np.random.uniform(dom_l, dom_u, (npop, n_vars))
pop_e = np.random.uniform(dom_l, dom_u, (npop, n_vars))
fit_pop_p = evaluate(pop_p, pop_e[0])
env.update_parameter('contacthurt', 'enemy')
fit_pop_e = evaluate(pop_e, pop_p[np.argmax(fit_pop_p)])
solutions = [pop_p, fit_pop_p, pop_e, fit_pop_e]
env.update_solutions(solutions)
def evolution(pop, fit_pop, best):
offspring = crossover(pop, fit_pop) # crossover
fit_offspring = evaluate(offspring, best) # evaluation
pop = np.vstack((pop, offspring))
fit_pop = np.append(fit_pop, fit_offspring)
# 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:], np.argmax(fit_pop))
pop = pop[chosen]
fit_pop = fit_pop[chosen]
return pop, fit_pop
# evolution
switch = 4 # defined how many generations each agent has to evolve while the evolution of the other one is 'frozen'
for i in range(1, gens):
# evolves one of the populations
if env.contacthurt == "player":
best = np.argmax(fit_pop_e)
pop_p, fit_pop_p = evolution(pop_p, fit_pop_p, pop_e[best])
else:
best = np.argmax(fit_pop_p)
pop_e, fit_pop_e = evolution(pop_e, fit_pop_e, pop_p[best])
print('\n GEN ', i, ' evolving ', env.contacthurt, ' - player mean ', np.mean(fit_pop_p), ' - enemy mean ',
np.mean(fit_pop_e))
# switches the evolution flag between player and enemy
switch -= 1
if switch == 0:
switch = 4
if env.contacthurt == "player":
env.update_parameter('contacthurt', 'enemy')
else:
env.update_parameter('contacthurt', 'player')
# saves simulation state
solutions = [pop_p, fit_pop_p, pop_e, fit_pop_e]
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