def test_no_taste(self): blobble_env = BlobbleEnv() blobble_env.reset() observation = blobble_env.reset_test([3, 3, 5], TEST_FOOD, taste=False) self.assertEqual(0, observation[3], 'Should be no taste 0') observation, reward, done, _ = blobble_env.step(4) # Step West self.assertEqual(0, observation[3], 'Should be no taste 0')
def test_move_east(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(3) # Step East self.assertEqual(0, observation[0], 'Should be northerly=0') self.assertEqual(1, observation[1], 'Should be easterly=1')
def test_move_too_far_north(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([10, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(1) # Step North self.assertEqual(10, observation[0], 'Should be northerly=10') self.assertEqual(0, observation[1], 'Should be easterly=0')
def test_move_too_far_west(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, -10, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(4) # Step West self.assertEqual(0, observation[0], 'Should be northerly=0') self.assertEqual(-10, observation[1], 'Should be easterly=-10')
def test_reward_eat_no_nutritional_value(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(0) # Eat food=0 self.assertEqual(5, observation[2], 'Should be health=5') self.assertEqual(0.5, reward, 'Should be reward=0.5')
def test_eat_no_step(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(0) # No step self.assertEqual(0, observation[0], 'Should be northerly=0') self.assertEqual(0, observation[1], 'Should be easterly=0')
def test_eat(self): blobble_env = BlobbleEnv() blobble_env.reset() observation = blobble_env.reset_test([3, 3, 5], TEST_FOOD) self.assertEqual(1, observation[3], 'Should be food of value 1') observation, reward, done, _ = blobble_env.step(0) # Eat food self.assertEqual(0, observation[3], 'Should be no food')
def test_move_south(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(0) # No step observation, reward, done, _ = blobble_env.step(1) # Step North observation, reward, done, _ = blobble_env.step(2) # Step South self.assertEqual(0, observation[0], 'Should be northerly=0') self.assertEqual(0, observation[1], 'Should be easterly=0')
def test_reward_eat_nutritional_value(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(3) # Move East observation, reward, done, _ = blobble_env.step(1) # Move North observation, reward, done, _ = blobble_env.step(0) # Eat food=5 self.assertEqual(10, observation[2], 'Should be health=10') self.assertEqual(1.0, reward, 'Should be reward=1.0')
def test_eat_and_move_west(self): blobble_env = BlobbleEnv() blobble_env.reset() observation = blobble_env.reset_test([3, 3, 5], TEST_FOOD) print(observation) self.assertEqual(1, observation[3], 'Should be food of value 1') observation, reward, done, _ = blobble_env.step(8) # Eat food, Go West print(observation) observation, reward, done, _ = blobble_env.step(3) # Go East back to original (no food) spot print(observation) self.assertEqual(0, observation[3], 'Should be no food')
def test_smell_south(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(0) # Eat food at current location ''' [ ., ., B, ., .] [ ., 2, 0, -5 .], [ ., ., 0, ., .], ''' self.assertEqual(-2, observation[5], 'Should be sniff south=0.75')
def test_smell_west(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(0) # Eat food at current location ''' [ ., 4, ., ., .], [ 2, 4, B, ., .], [ ., 2, ., ., .], ''' self.assertEqual(0, observation[7], 'Should be sniff west=3')
def test_smell_north(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([0, 0, 5], TEST_FOOD) observation, reward, done, _ = blobble_env.step(0) # Eat food at current location ''' [ ., ., ., ., .], [ ., 4, 2, 3, .], [ ., ., B, ., .] ''' self.assertEqual(3, observation[4], 'Should be sniff north=4')
def test_taste(self): blobble_env = BlobbleEnv() blobble_env.reset() observation = blobble_env.reset_test([0, 0, 5], TEST_FOOD) self.assertEqual(0, observation[3], 'Should be taste=0') observation = blobble_env.reset_test([-1, -4, 5], TEST_FOOD) self.assertEqual(-2, observation[3], 'Should be taste=-2') observation = blobble_env.reset_test([3, 3, 5], TEST_FOOD) self.assertEqual(1, observation[3], 'Should be taste=1') observation = blobble_env.reset_test([3, 4, 5], TEST_FOOD) self.assertEqual(-2, observation[3], 'Should be taste=-2')
def test_no_smell(self): blobble_env = BlobbleEnv() blobble_env.reset() observation = blobble_env.reset_test([3, 3, 5], TEST_FOOD, smell=False) self.assertEqual(0, observation[4], 'Should be no smell north') self.assertEqual(0, observation[5], 'Should be no smell south') self.assertEqual(0, observation[6], 'Should be no smell east') self.assertEqual(0, observation[7], 'Should be no smell west') observation, reward, done, _ = blobble_env.step(4) # Step West self.assertEqual(0, observation[4], 'Should be no smell north after step') self.assertEqual(0, observation[5], 'Should be no smell south after step') self.assertEqual(0, observation[6], 'Should be no smell east after step') self.assertEqual(0, observation[7], 'Should be no smell west after step')
def test_reset_observation(self): blobble_env = BlobbleEnv() observation = blobble_env.reset() expected = np.array((0, 0, 5), dtype=np.float) np.testing.assert_array_equal(expected, observation[0:3], 'Observation not as expected after reset') observation = blobble_env.reset_test([1, 2, 3], TEST_FOOD) expected = np.array((1, 2, 3), dtype=np.float) np.testing.assert_array_equal(expected, observation[0:3], 'Observation not as expected after reset test')
def create_blobble_video(video_filename, num_episodes=1, fps=30): filename = video_filename + ".mp4" blobble_env = BlobbleEnv() print('Observation space: '+str(blobble_env.observation_space)) print('Action space: '+str(blobble_env.action_space)) with imageio.get_writer(filename, fps=fps) as video: for i in range(num_episodes-1): print(i) blobble_env.reset() done = False while not done: action = np.random.randint(8) print('Next Action: ', action) observation, reward, done, _ = blobble_env.step(action) blobble_env.render_print() video.append_data(blobble_env.render(mode='rgb_array')) blobble_env.close()
def test_render_human(self): blobble_env = BlobbleEnv() blobble_env.reset() blobble_env.reset_test([-2, 5, 5], TEST_FOOD) blobble_env.render(mode='human')