Commit 5d38bf71 authored by David A.. Werner's avatar David A.. Werner
Browse files

Added test code for fitness and simulate

parent 96329640
......@@ -67,7 +67,7 @@ def test_simple_save_with_different_muscle_strengths(tmpdir, muscle_strength, lo
new_man = SimpleManduca.load(file_name, muscle_strength=muscle_strength)
assert np.count_nonzero(new_man.muscles) == 0
assert new_man == man
else:
new_man = SimpleManduca.load(file_name, muscle_strength=loaded_muscle_strength)
assert new_man == man
......@@ -84,3 +84,57 @@ def test_simple_save(tmpdir, fitness, compressed):
new_man = SimpleManduca.load(file_name, muscle_strength=100)
assert new_man._fitness == man._fitness
assert new_man._fitness == fitness
@pytest.mark.parametrize("num_time_steps", [2,5,10])
@pytest.mark.parametrize("num_legs", [2])
@pytest.mark.parametrize("time_step", [0.01, 1.0, 10])
@pytest.mark.parametrize("record_level", [0,1,2,None])
@pytest.mark.parametrize("sub_intervals", [1,2,10])
def test_simulate_result_sizes(num_legs, num_time_steps, time_step, record_level, sub_intervals):
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step, muscle_strength=100)
x,v,t = man.simulate(record_level=record_level, sub_intervals=sub_intervals)
x,v,t = np.array(x), np.array(v), np.array(t)
assert x.shape == v.shape
assert len(x) == len(t)
if record_level == 1:
assert len(x) == num_time_steps + 1
elif record_level == 2:
assert len(x) == num_time_steps*sub_intervals + 1
else:
assert len(x) == 2
def test_fitness_reset():
man = SimpleManduca.random_individual(2, 2, 1, muscle_strength=100)
assert man._fitness == None
man.fitness
assert man._fitness != None
# Make sure fitness is reset when updating things that affect it
# Manually set _fitness
# Update something that should reset it
# Make sure it was reset
# Make sure fitness evaluates to the correct value
# Make sure _fitness is updated as well
man._fitness = 1
man.fitness_function = lambda m: -1
assert man._fitness == None
assert man.fitness == -1
assert man._fitness == -1
man._fitness = 1
man.legs = np.copy(man.legs)
assert man._fitness == None
assert man.fitness == -1
assert man._fitness == -1
man._fitness = 1
man.muscles = np.copy(man.muscles)
assert man._fitness == None
assert man.fitness == -1
assert man._fitness == -1
man._fitness = 1
man.time_step = man.time_step + 0
assert man._fitness == None
assert man.fitness == -1
assert man._fitness == -1
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