/
nsga2.py
265 lines (206 loc) · 8.65 KB
/
nsga2.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
# This file is part of DEAP.
#
# DEAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DEAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
from builtins import input
import array
import random
import json
import numpy as np
import time
import argparse
import os
from math import sqrt, ceil
from itertools import chain
from deap import algorithms
from deap import base
from deap import benchmarks
from deap.benchmarks.tools import diversity, convergence, hypervolume
from deap import creator
from deap import tools
from scoop import futures
from model import quad_landing
from nn import nn, rnn, ctrnn
from test import plot_population, plot_selection, log_population, test_agents
MUTATION_RATE = 0.3
creator.create("Fitness", base.Fitness, weights=(-1.0, -1.0, -1.0))
creator.create("Individual", list, fitness=creator.Fitness)
toolbox = base.Toolbox()
def mutSet(individual):
"""Mutation that pops or add an element."""
individual[0].mutate(MUTATION_RATE)
return individual
def evalLanding(individual):
# set noise paramters
# time delay range [1,5] time steps
# divegence sensor noise [0,0.15]s-1
# thrust time constant [0.02, 0.1]s
dyn = quad_landing(delay=ceil(4*np.random.random_sample())+1,
noise=0.1*np.random.random_sample()+0.05,
noise_p=0.25*np.random.random_sample(),
thrust_tc=0.04*np.random.random_sample()+0.005,
dt=1./ceil(20*np.random.random_sample() + 30),
computational_delay_prob=np.random.random_sample()/5.
)
h0 = [2., 4., 6., 8.]
t_score = 0.
h_score = 0.
v_score = 0.
for i in range(len(h0)):
obs = dyn.reset()
individual[0].reset()
dyn.set_h0(h0[i])
done = False
#energy = 0.
while not done:
obs, _, done, _ = dyn.step(individual[0].predict(obs, dyn.t))
#energy += dyn.thrust_cmd + dyn.G
t = dyn.t
h = dyn.y[0]
v = dyn.y[1]
# penalize not landing, here only the hieght matters
if t >= dyn.MAX_T or h >= dyn.MAX_H:
v = -2.
t = dyn.MAX_T
# don't differentiate hieght score between sucessful individuals
#if h <= dyn.MIN_H:
# h = dyn.MIN_H
# don't differentiate velocity score between sucessful individuals
#if np.abs(v) <= 0.01:
# v = 0.
# penalize high speed crashing, not a viable solution
#if v < -2.:
# h = dyn.MAX_H
# t = dyn.MAX_T
t_score += t
h_score += h
v_score += v*v #np.abs(v)
# minimize time to end of sim, final height and velocity
return t_score, h_score, v_score
def cxSet(ind1, ind2):
return ind1, ind2
neural_type = nn # nn. rnn, ctrnn
toolbox.register("individual", tools.initRepeat, creator.Individual, neural_type, 1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalLanding)
toolbox.register("mate", cxSet)
toolbox.register("mutate", mutSet)
toolbox.register("select", tools.selNSGA2)
toolbox.register("map", futures.map)
def main(seed=None):
random.seed(seed)
NGEN = 250
MU = 100
log_interval = 25
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)
stats.register("median", np.median, axis=0)
logbook = tools.Logbook()
logbook.header = "gen", "evals", "std", "min", "avg", "max", "median"
pop = toolbox.population(n=MU)
hof = tools.ParetoFront()
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in pop if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# This is just to assign the crowding distance to the individuals
# no actual selection is done
pop = toolbox.select(pop, len(pop))
record = stats.compile(pop)
logbook.record(gen=0, evals=len(invalid_ind), **record)
print(logbook.stream)
hof.update(pop)
basepath = os.path.dirname(os.path.abspath(__file__))
log_dir = '{}/logs/{}/'.format(basepath, time.strftime('%y%m%d-%H%M%S'))
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# log initial population
os.makedirs('{}0'.format(log_dir))
for i, agent in enumerate(pop):
agent[0].save_weights('{}0/{}_weights.csv'.format(log_dir, i), overwrite=True)
log_population(pop, '{}0'.format(log_dir))
# Begin the generational process
for gen in range(1, NGEN+1):
# Get Offspring
# first get pareto front
pareto_fronts = tools.sortNondominated(pop, len(pop))
selection = pareto_fronts[0]
len_pareto = len(pareto_fronts[0])
rest = list(chain(*pareto_fronts[1:]))
if len(rest) % 4:
rest.extend(random.sample(selection, 4 - (len(rest) % 4)))
selection.extend(tools.selTournamentDCD(rest, len(rest)))
offspring = [toolbox.mutate(toolbox.clone(ind)) for ind in selection[:len(pop)]]
# Revaluate the individuals in last population
fitnesses = toolbox.map(toolbox.evaluate, pop)
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
# Evaluate the new offspring
fitnesses = toolbox.map(toolbox.evaluate, offspring)
for ind, fit in zip(offspring, fitnesses):
ind.fitness.values = fit
# Update the hall of fame with the generated individuals
hof.update(offspring)
plot_population(pop, offspring, lim = [[10,120],[0,0],[0,4]])
# Select the next generation population
pop = toolbox.select(pop + offspring, MU)
pareto_fronts = tools.sortNondominated(pop, len(pop))
plot_selection(pop, pareto_front_size=len(pareto_fronts[0]), lim = [[10,120],[0,0],[0,4]])
record = stats.compile(pop)
logbook.record(gen=gen, evals=len(offspring)+len(pop), **record)
print(logbook.stream)
if gen % log_interval == 0 or gen == NGEN:
os.makedirs('{}{}'.format(log_dir, gen))
for i, agent in enumerate(pop):
agent[0].save_weights('{}{}/{}_weights.csv'.format(log_dir, gen, i), overwrite=True)
log_population(pop, '{}{}'.format(log_dir, gen))
with open('{}/gen_stats.txt'.format(log_dir), 'w') as fp:
np.savetxt(fp, logbook, fmt="%s")
plot_population(pop)
print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0, 11.0]))
os.makedirs('{}hof'.format(log_dir))
for i, agent in enumerate(hof):
agent[0].save_weights('{}hof/{}_weights.csv'.format(log_dir, i), overwrite=True)
log_population(hof, '{}hof'.format(log_dir))
return pop, logbook
if __name__ == "__main__":
# parse input arguments
parser = argparse.ArgumentParser()
parser.add_argument('--mode', choices=['evolve','train', 'test'], default='evolve')
#parser.add_argument('--env-name', type=str, default='BreakoutDeterministic-v4')
parser.add_argument('--weights', type=str, default=None)
parser.add_argument('--visualize', action='store_const', const=1, default=0)
args = parser.parse_args()
if args.mode == 'evolve':
pop, stats = main()
elif args.mode == 'test':
if args.weights is None:
raise ValueError('You must provide a model to train with the weights input')
test_agents(args.weights)
input('Press any key to exit')
# pop.sort(key=lambda x: x.fitness.values)
# print(stats)
# print("Convergence: ", convergence(pop, optimal_front))
# print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1]))
# import matplotlib.pyplot as plt
# import numpy
# front = numpy.array([ind.fitness.values for ind in pop])
# optimal_front = numpy.array(optimal_front)
# plt.scatter(optimal_front[:,0], optimal_front[:,1], c="r")
# plt.scatter(front[:,0], front[:,1], c="b")
# plt.axis("tight")
# plt.show()