Commit 3b614c42 authored by David A.. Werner's avatar David A.. Werner
Browse files

Added code for example assignment results

parent 3821501f
import pickle
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
histories = pickle.load(open('assignment_simulation_histories.p', 'r'))
fig, ax = plt.subplots()
for idx, history in enumerate(histories):
x = [gen.generation_number for gen in history]
y = [gen.max_fitness for gen in history]
ax.plot(x, y, label = str(idx))
ax.set(xlabel='Generation #', ylabel='max fitness', title='Three Simulations')
ax.yaxis.grid(True)
fig.legend()
plt.savefig('assignment_fitness_vs_gen.png')
plt.close()
fig, ax = plt.subplots()
styles = {'surviving_ancestors': 'b-',
'surviving_children': 'r--',
'surviving_mutants': 'k-.'}
labels = {'surviving_ancestors': 'previous generation',
'surviving_children': 'new children',
'surviving_mutants': 'new mutants'}
for attr in ['surviving_ancestors', 'surviving_children', 'surviving_mutants']:
style = styles[attr]
ys = []
for idx, history in enumerate(histories):
history = filter(lambda gen: getattr(gen, attr) is not None, history)
ys.append([getattr(gen, attr) for gen in history])
x = [gen.generation_number for gen in history]
y = np.mean(ys, axis=0)
ax.plot(x, y, style, label = labels[attr])
ax.set(xlabel='Generation #', ylabel='Number per Group', title='Types of Surviving Manducas')
ax.yaxis.grid(True)
fig.legend()
plt.savefig('assignment_survivors_vs_gen.png')
plt.close()
fig, ax = plt.subplots()
styles = {'duplicate_population': 'b-',
'duplicate_children': 'r--',
'duplicate_mutants': 'k-.'}
labels = {'duplicate_population': 'previous generation',
'duplicate_children': 'new children',
'duplicate_mutants': 'new mutants'}
for attr in ['duplicate_population', 'duplicate_children', 'duplicate_mutants']:
style = styles[attr]
ys = []
for idx, history in enumerate(histories):
history = filter(lambda gen: getattr(gen, attr) is not None, history)
ys.append([getattr(gen, attr) for gen in history])
x = [gen.generation_number for gen in history]
y = np.mean(ys, axis=0)
ax.plot(x, y, style, label = labels[attr])
ax.set(xlabel='Generation #', ylabel='Number per Group', title='Types of Duplicate Manducas')
ax.yaxis.grid(True)
fig.legend()
plt.savefig('assignment_dups_vs_gen.png')
plt.close()
""" This is a script that generates the basic functionality of the
EE194_Manduca_Simulator package with multithreading support"""
import numpy as np
import pickle
import multiprocessing
import tqdm
from manduca import SimpleManduca, EvolutionSimulator, EvolutionParameters
POPULATION_SIZE, NUM_GENERATIONS= 100, 200
evolution_parameters = EvolutionParameters(POPULATION_SIZE, 50, 50, 10)
num_legs, time_segments, time_step = 5, 10, 10
rng = np.random.RandomState()
rng.seed(0xBAAA)
def random_manduca():
return SimpleManduca.random_individual(num_legs, time_segments, time_step, rng=rng)
histories = []
try:
pool = multiprocessing.Pool(int(multiprocessing.cpu_count()))
for idx,RND_SEED in enumerate(tqdm.tqdm([0xBADC0DE, 0xDEADBEEF, 0xFACEB00C],
desc='70 year simulations', unit='sims')):
simulator = EvolutionSimulator(random_manduca, pool=pool,
evolution_parameters = evolution_parameters,
random_number_generator=rng, random_seed=RND_SEED)
simulator.run_simulation(NUM_GENERATIONS)
histories.append(simulator.history)
simulator.update_fitness()
simulator.population[0].save('fittest_manduca_{}.npz'.format(idx))
finally:
pool.close()
pool.join()
pickle.dump(histories, open('assignment_simulation_histories.p', 'wb'))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment