Commit 6553946c authored by David A.. Werner's avatar David A.. Werner
Browse files

renamed duplicate tests. Test saving body_properties and loading with...

renamed duplicate tests. Test saving body_properties and loading with fitness_function. Basic tests for simulate().
parent ce0452aa
......@@ -74,7 +74,7 @@ def test_simple_save_with_different_muscle_strengths(tmpdir, muscle_strength, lo
@pytest.mark.parametrize("compressed", [True, False])
@pytest.mark.parametrize("fitness", [None, 0.01, 1.0, 10])
def test_simple_save(tmpdir, fitness, compressed):
def test_simple_save_with_fitness(tmpdir, fitness, compressed):
time_step, num_time_steps, num_legs = 1, 1, 2
file_name = tmpdir.join('manduca.npz').strpath
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step, muscle_strength=100)
......@@ -85,6 +85,38 @@ def test_simple_save(tmpdir, fitness, compressed):
assert new_man._fitness == man._fitness
assert new_man._fitness == fitness
@pytest.mark.parametrize("compressed", [True, False])
@pytest.mark.parametrize("body_properties", [None, Manduca.default_body_properties,
Manduca.damped_body_properties])
def test_simple_save_with_body_properties(tmpdir, body_properties, compressed):
time_step, num_time_steps, num_legs = 1, 1, 2
file_name = tmpdir.join('manduca.npz').strpath
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step,
body_properties=body_properties,
muscle_strength=100)
man.save(file_name, compressed=compressed)
new_man_1 = SimpleManduca.load(file_name, muscle_strength=100)
new_man_2 = SimpleManduca.load(file_name, body_properties=body_properties,
muscle_strength=100)
assert new_man_1 == man
assert new_man_1 == new_man_2
@pytest.mark.parametrize('fitness_function', [Manduca.distance_traveled,
lambda m: 1/sum(m.muscles+0.001)])
def test_simple_load_with_specified_fitness_function(tmpdir, fitness_function):
time_step, num_time_steps, num_legs = 1, 1, 2
file_name = tmpdir.join('manduca.npz').strpath
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step,
muscle_strength=100,
fitness_function=fitness_function)
man.save(file_name, compressed=True)
new_man = SimpleManduca.load(file_name, muscle_strength=100,
fitness_function=fitness_function)
assert new_man == man
assert new_man.fitness == man.fitness
assert new_man.fitness_function == man.fitness_function
@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])
......@@ -103,6 +135,31 @@ def test_simulate_result_sizes(num_legs, num_time_steps, time_step, record_level
else:
assert len(x) == 2
@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("sub_intervals", [1,2,10])
def test_simulate_result_sizes(num_legs, num_time_steps, time_step, sub_intervals):
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step, muscle_strength=100)
x0,v0,t0 = man.simulate(record_level=0, sub_intervals=sub_intervals)
x0,v0,t0 = np.array(x0), np.array(v0), np.array(t0)
# Make sure other invalid recording levels use level 0
for invalid_level in [-1,None,"0"]:
x,v,t = man.simulate(record_level=invalid_level, sub_intervals=sub_intervals)
x,v,t = np.array(x), np.array(v), np.array(t)
assert np.array_equal(x,x0)
assert np.array_equal(v,v0)
assert np.array_equal(t,t0)
x1,v1,t1 = man.simulate(record_level=1, sub_intervals=sub_intervals)
x1,v1,t1 = np.array(x1), np.array(v1), np.array(t1)
x2,v2,t2 = man.simulate(record_level=2, sub_intervals=sub_intervals)
x2,v2,t2 = np.array(x2), np.array(v2), np.array(t2)
def test_fitness_reset():
man = SimpleManduca.random_individual(2, 2, 1, muscle_strength=100)
assert man._fitness == None
......
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