Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Alexander R. Hankin
EE194_Manduca_Simulator_Demo
Commits
55b78922
Commit
55b78922
authored
May 09, 2018
by
David A.. Werner
Browse files
Updated interface & damped manduca
parent
6bb2adb1
Changes
7
Hide whitespace changes
Inline
Side-by-side
assignment_anim.py
View file @
55b78922
...
...
@@ -3,7 +3,7 @@ from manduca import SimpleManduca, Manduca
from
manduca.Visualization
import
animate
,
animate_as_gif
,
animate_as_mp4
manducas
=
[]
for
m_file
in
glob
.
glob
(
'fittest_manduca_*.npz'
):
for
m_file
in
sorted
(
glob
.
glob
(
'fittest_manduca_*.npz'
)
)
:
if
"complex"
in
m_file
:
manduca
=
Manduca
.
load
(
m_file
)
else
:
...
...
@@ -14,7 +14,7 @@ animate(manducas[0])
animate_as_gif
(
'one_manduca.gif'
,
manducas
[
0
])
animate_as_mp4
(
'one_manduca.mp4'
,
manducas
[
0
])
multi_kwargs
=
{
'dpi'
:
200
,
'show_winners'
:
True
,
'show_fitnesses'
:
True
,
multi_kwargs
=
{
'dpi'
:
200
,
'show_winners'
:
True
,
'show_fitnesses'
:
True
,
'subplot_kwargs'
:{
'figsize'
:
(
16
,
9
)}}
animate
(
*
manducas
,
**
multi_kwargs
)
animate_as_gif
(
'all_manducas.gif'
,
*
manducas
,
**
multi_kwargs
)
...
...
assignment_plots.py
View file @
55b78922
...
...
@@ -25,7 +25,7 @@ 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
):
...
...
@@ -49,7 +49,7 @@ 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
):
...
...
@@ -73,7 +73,7 @@ labels = {'parent_survival_rate': 'previous generation',
'child_survival_rate'
:
'children'
,
'mutant_survival_rate'
:
'mutants'
}
for
attr
in
[
'parent_survival_rate'
,
'child_survival_rate'
,
'mutant_survival_rate'
]:
style
=
styles
[
attr
]
ys
=
[]
for
idx
,
history
in
enumerate
(
histories
):
...
...
assignment_sim.py
View file @
55b78922
...
...
@@ -18,6 +18,10 @@ def random_simple_manduca():
def
random_manduca
():
return
Manduca
.
random_individual
(
num_legs
,
time_segments
,
time_step
,
rng
=
rng
)
def
random_damped_simple_manduca
():
return
SimpleManduca
.
random_individual
(
num_legs
,
time_segments
,
time_step
,
rng
=
rng
,
body_properties
=
Manduca
.
damped_body_properties
)
histories
=
[]
try
:
pool
=
multiprocessing
.
Pool
(
int
(
multiprocessing
.
cpu_count
()))
...
...
@@ -38,6 +42,14 @@ try:
simulator
.
update_fitness
()
simulator
.
population
[
0
].
save
(
'fittest_manduca_complex.npz'
)
# Run a simulation with damped_body_properties
simulator
=
EvolutionSimulator
(
random_damped_simple_manduca
,
pool
=
pool
,
evolution_parameters
=
evolution_parameters
,
random_number_generator
=
rng
,
random_seed
=
0xBAD
)
simulator
.
run_simulation
(
NUM_GENERATIONS
)
simulator
.
update_fitness
()
simulator
.
population
[
0
].
save
(
'fittest_manduca_3_damped.npz'
)
# Run a simulation with other number of legs
for
num_legs
in
[
2
,
3
,
7
]:
simulator
=
EvolutionSimulator
(
random_simple_manduca
,
pool
=
pool
,
...
...
@@ -47,7 +59,7 @@ try:
simulator
.
update_fitness
()
simulator
.
population
[
0
].
save
(
'fittest_manduca_legs_{}.npz'
.
format
(
num_legs
))
finally
:
pool
.
close
()
pool
.
join
()
pool
.
close
()
pool
.
join
()
pickle
.
dump
(
histories
,
open
(
'assignment_simulation_histories.p'
,
'wb'
))
basic_simulation.py
View file @
55b78922
""" This is a script that generates the basic functionality of the
EE194_Manduca_Simulator package """
from
manduca
import
SimpleManduca
,
EvolutionSimulator
from
manduca
import
SimpleManduca
,
EvolutionSimulator
,
EvolutionParameters
POPULATION_SIZE
,
NUM_GENERATIONS
=
10
,
25
num_legs
,
time_segments
,
time_step
=
2
,
4
,
10
POPULATION_SIZE
,
NUM_GENERATIONS
=
20
,
70
evolution_parameters
=
EvolutionParameters
(
POPULATION_SIZE
,
50
,
50
,
10
)
num_legs
,
time_segments
,
time_step
=
5
,
10
,
10
def
random_manduca
():
return
SimpleManduca
.
random_individual
(
num_legs
,
time_segments
,
time_step
)
simulator
=
EvolutionSimulator
(
(
random_manduca
,
POPULATION_SIZE
)
)
simulator
=
EvolutionSimulator
(
random_manduca
,
evolution_parameters
=
evolution_parameters
)
simulator
.
run_simulation
(
NUM_GENERATIONS
)
print
simulator
.
history
[
-
1
]
rnd_seed_simulation.py
View file @
55b78922
""" This is a script that generates the basic functionality of the
EE194_Manduca_Simulator package with reproducable results """
import
numpy
as
np
from
manduca
import
SimpleManduca
,
Population
,
EvolutionSimulator
from
manduca
import
SimpleManduca
,
Population
,
EvolutionSimulator
,
EvolutionParameters
POPULATION_SIZE
,
NUM_GENERATIONS
=
10
,
25
evolution_parameters
=
EvolutionParameters
(
POPULATION_SIZE
,
50
,
50
,
10
)
num_legs
,
time_segments
,
time_step
=
2
,
4
,
10
def
random_manduca
():
...
...
@@ -13,7 +14,8 @@ RND_SEED = 0xDEADBEEF
rng
=
np
.
random
.
RandomState
()
rng
.
seed
(
RND_SEED
)
simulator
=
EvolutionSimulator
((
random_manduca
,
POPULATION_SIZE
),
random_number_generator
=
rng
,
simulator
=
EvolutionSimulator
(
random_manduca
,
random_number_generator
=
rng
,
evolution_parameters
=
evolution_parameters
,
random_seed
=
0xDEADBEEF
)
simulator
.
run_simulation
(
NUM_GENERATIONS
)
print
simulator
.
history
[
-
1
]
simulation_with_pool.py
View file @
55b78922
""" This is a script that generates the basic functionality of the
EE194_Manduca_Simulator package with multithreading support"""
import
multiprocessing
from
manduca
import
SimpleManduca
,
EvolutionSimulator
from
manduca
import
SimpleManduca
,
EvolutionSimulator
,
EvolutionParameters
POPULATION_SIZE
,
NUM_GENERATIONS
=
10
,
25
evolution_parameters
=
EvolutionParameters
(
POPULATION_SIZE
,
50
,
50
,
10
)
num_legs
,
time_segments
,
time_step
=
2
,
4
,
10
def
random_manduca
():
...
...
@@ -11,9 +12,10 @@ def random_manduca():
try
:
pool
=
multiprocessing
.
Pool
(
int
(
multiprocessing
.
cpu_count
()))
simulator
=
EvolutionSimulator
((
random_manduca
,
POPULATION_SIZE
),
pool
=
pool
)
simulator
=
EvolutionSimulator
(
random_manduca
,
pool
=
pool
,
evolution_parameters
=
evolution_parameters
)
simulator
.
run_simulation
(
NUM_GENERATIONS
)
finally
:
pool
.
close
()
pool
.
join
()
pool
.
close
()
pool
.
join
()
print
simulator
.
history
[
-
1
]
speedup.py
View file @
55b78922
...
...
@@ -3,7 +3,7 @@ import time
import
numpy
as
np
import
multiprocessing
from
functools
import
wraps
from
manduca
import
SimpleManduca
,
EvolutionSimulator
from
manduca
import
SimpleManduca
,
EvolutionSimulator
,
EvolutionParameters
def
timed_function
(
return_result
=
True
,
return_time
=
True
,
print_time
=
False
):
...
...
@@ -31,23 +31,25 @@ def timed_function(return_result=True,return_time=True, print_time=False):
def
run_sim
():
return
simulator
.
run_simulation
(
NUM_GENERATIONS
)
POPULATION_SIZE
,
NUM_GENERATIONS
=
10
,
10
POPULATION_SIZE
,
NUM_GENERATIONS
=
200
,
2
evolution_parameters
=
EvolutionParameters
(
POPULATION_SIZE
,
50
,
50
,
10
)
num_legs
,
time_segments
,
time_step
=
5
,
10
,
10
def
random_manduca
():
return
SimpleManduca
.
random_individual
(
num_legs
,
time_segments
,
time_step
)
simulator
=
EvolutionSimulator
(
(
random_manduca
,
POPULATION_SIZE
)
)
simulator
=
EvolutionSimulator
(
random_manduca
,
evolution_parameters
=
evolution_parameters
)
print
"Running single-threaded code.."
time_ms
=
run_sim
()
try
:
pool
=
multiprocessing
.
Pool
(
int
(
multiprocessing
.
cpu_count
()))
simulator
=
EvolutionSimulator
((
random_manduca
,
POPULATION_SIZE
),
pool
=
pool
)
simulator
=
EvolutionSimulator
(
random_manduca
,
pool
=
pool
,
evolution_parameters
=
evolution_parameters
)
print
"Running multi-threaded code.."
time_ms_multi
=
run_sim
()
finally
:
pool
.
close
()
pool
.
join
()
pool
.
close
()
pool
.
join
()
print
"Number of cores: {}"
.
format
(
multiprocessing
.
cpu_count
())
print
"Speedup: {}"
.
format
(
time_ms
/
time_ms_multi
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment