def test_warmer_colder(self): self.world = universe.create_world('pushblock', warmercolder=True) block = self.world.objects['block'] player = self.world.objects['agent'] goal = self.world.objects['goal'] goal.position = (10, 10) block.position = goal.position + (2, 0) player.position = goal.position + (3, 0) # check that we lose a point when the block doesn't get closer to the # goal obs, reward, done, info = self.world.step('NOOP') self.assertEqual(reward, -1) self.assertFalse(done) # check that we get a point when the block does get closer to the goal obs, reward, done, info = self.world.step('UP') self.assertEqual(reward, 1) self.assertFalse(done) # check that we get points and the episode ends when the block is on # top of the goal obs, reward, done, info = self.world.step('UP') self.assertEqual(reward, 1001) self.assertTrue(done)
def run_world(name): """Check that we can run a world and that it behaves as expected""" #save the initial class-hierarchy state so we don't pollute the namespace TypeFamily._save_class_hierarchy_state() params = {} #make sure we don't get prompted for actions world_params = universe.load_parameters(name, reload_module=True) if issubclass(core.Agent.get_class(world_params['agent']), HumanAgent): params['agent'] = 'random' #create the world world = universe.create_world(name, reload_module=True, **params) #test the world tc.TestWorld('test_world', world=world).test_world() #run the simulation for a bit tc.TestWorld('test_run', world=world).test_run() #examine the world again tc.TestWorld('test_world', world=world).test_world() #make sure we can rewind the world tc.TestWorld('test_state', world=world).test_state() #restore the initial class-hierarchy state TypeFamily._restore_class_hierarchy_state()
def test_loop(self): """Check that ghost goes around a loop when there are no branching choices""" objects, height, width = h.world.screen( """ XXXXXXXXX X BX*X X XXX XXX X XXX XXXXXXXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) blinky = self.world.objects['blinky'] blinky.orientation = 1 #UP self.assertTrue(np.array_equal(blinky.position, (1, 5))) # the squares of the track track = ([(1, x) for x in xrange(5, 0, -1)] + [(y, 1) for y in xrange(2, 4)] + [(3, x) for x in xrange(2, 6)] + [(2, 5)]) # go around the track two times for _ in xrange(2): for t in track: # check that Blinky is where we expect self.assertTrue(np.array_equal(blinky.position, t)) obs, reward, done, info = self.world.step('NOOP')
def test_skull_behavior(self): # create the objects we want screen = """ ------------------ - - X @ * X ------------------ - - - - - - - - - - ------------------ """ objects, width, height = h.world.screen(screen, pixelzuma.legend) self.world = universe.create_world('pixelzuma', objects=objects, width=width, height=height) # remember skull's initial position skull = self.world.objects['skull'] anchor = skull.position # check skull's movement self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, 1))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, 2))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, 3))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, 2))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, 1))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, 0))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, -1))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, -2))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, -3))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, -2))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, -1))) self.world.step('NOOP') self.assertTrue(np.array_equal(skull.position, anchor + (0, 0)))
def test_key_usable(self): # create the objects we want screen = """ ------------------ - - X D * X ------------------ - - - - - - - - - - ------------------ """ objects, width, height = h.world.screen(screen, pixelzuma.legend) self.world = universe.create_world('pixelzuma', objects=objects, width=width, height=height) # give pedro a key self.world.objects['pedro'].inventory = ['KEY'] # check that we get a reward and that we can move through the door obs, reward, done, info = self.world.step('LEFT') self.assertEqual(reward, 0) self.assertFalse(done) obs, reward, done, info = self.world.step('LEFT') self.assertEqual(reward, 300) self.assertFalse(done) obs, reward, done, info = self.world.step('LEFT') self.assertEqual(reward, 0) self.assertFalse(done)
def test_ball_leaves_screen(self): # create the obejcts we want objects, height, width = h.world.screen(""" WWWW W BW W* W """, block_breaker.legend) self.world = universe.create_world('block_breaker', objects=objects, height=height, width=width) # point the ball down and to the right self.world.objects['ball'].velocity = (1.01, 1) # get the goal self.assertEqual(len(self.world.goals), 1) goal = self.world.goals['bricks_destroyed'] # check that goal is not achieved self.assertFalse(goal.is_achieved()) # step obs, reward, done, info = self.world.step('NOOP') # check that we got a penalty and that the episode terminated self.assertEqual(reward, -1001) self.assertTrue(done) # check that goal is not achieved self.assertFalse(goal.is_achieved())
def test_actions(self): """Test that actions have the right effect""" # create the objects we want objects, height, width = h.world.screen(""" XXXX X* X X X XXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) player = self.world.objects['chomper_player'] posn = player.position # check that actions do what we think they should self.world.step('RIGHT') self.assertTrue(np.array_equal(player.position, posn + (0, 1))) self.world.step('DOWN') self.assertTrue(np.array_equal(player.position, posn + (1, 1))) self.world.step('LEFT') self.assertTrue(np.array_equal(player.position, posn + (1, 0))) self.world.step('UP') self.assertTrue(np.array_equal(player.position, posn))
def __init__(self, name, run=True, end_prompt=True, **kwargs): self._name = name #create the environment self.world = universe.create_world(name, defaults=self._defaults, **kwargs)
def test_teleport_ghost(self): """Test that the teleporter teleports ghosts""" objects, height, width = h.world.screen( """ XXXXXXXXXXXXXX TB T XXXXXXXXXXXXXX X*XXXXXXXXXXXX XXXXXXXXXXXXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) blinky = self.world.objects['blinky'] blinky.orientation = 0 self.world.step('NOOP') self.assertTrue(np.array_equal(blinky.position, (1, 13)), repr(blinky.position)) self.world.step('NOOP') blinky.orientation = 2 self.world.step('NOOP') self.assertTrue(np.array_equal(blinky.position, (1, 0)), repr(blinky.position))
def test_enclosure(self): seen_enclosed = seen_not_enclosed = False for seed in xrange(100): if seen_enclosed and seen_not_enclosed: break self.world = universe.create_world('enclosure', seed=seed) block = self.world.objects['block'] container = self.world.objects['container'] tlbr = container.extent objects = [x for x in self.world.objects if x.name == 'basic'] if tlbr[0] <= block.position[0] <= tlbr[2] and tlbr[1] <= block.position[1] <= tlbr[3]: seen_enclosed = True # check that true class is correct self.assertEqual(self.world.enclosure_true_class, 'ENCLOSED') # check that we get a reward for correct classification and the # episode terminates obs, reward, done, info = self.world.step('CLASSIFY_ENCLOSED') self.assertEqual(reward, 99) self.assertTrue(done) # check that we get a penalty for incorrect classification and # the episode terminates obs, reward, done, info = self.world.step('CLASSIFY_NOT_ENCLOSED') self.assertEqual(reward, -101) self.assertTrue(done) # check that we can change the true class block.position = (1, 1) self.assertEqual(self.world.enclosure_true_class, 'NOT_ENCLOSED') else: seen_not_enclosed = True # check that true class is correct self.assertEqual(self.world.enclosure_true_class, 'NOT_ENCLOSED') # check that we get a penalty for incorrect classification and # the episode terminates obs, reward, done, info = self.world.step('CLASSIFY_ENCLOSED') self.assertEqual(reward, -101) self.assertTrue(done) # check that we get a reward for correct classification and the # episode terminates obs, reward, done, info = self.world.step('CLASSIFY_NOT_ENCLOSED') self.assertEqual(reward, 99) self.assertTrue(done) # check that we can change the true class self.world.objects['classifier'].position = (10, 10) block.position = container.position self.assertEqual(self.world.enclosure_true_class, 'ENCLOSED') # check that we've seen both cases self.assertTrue(seen_enclosed) self.assertTrue(seen_not_enclosed)
def test_ghosts_reverse_on_pellet(self): """Test that ghosts reverse direction when Chomper eats a power pellet""" objects, height, width = h.world.screen( """ XXXXXXXXXXXXXX X B P I CX XXXXXXXXXXXXXX X*O X XXXXXXXXXXXXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) player = self.world.objects['chomper_player'] blinky = self.world.objects['blinky'] pinky = self.world.objects['pinky'] inky = self.world.objects['inky'] clyde = self.world.objects['clyde'] # point ghosts left blinky.orientation = 0 pinky.orientation = 0 inky.orientation = 0 clyde.orientation = 0 obs, reward, done, info = self.world.step('RIGHT') self.assertEqual(blinky.orientation, 2) self.assertEqual(pinky.orientation, 2) self.assertEqual(inky.orientation, 2) self.assertEqual(clyde.orientation, 2)
def test_teleport(self): """Test that the teleporter teleports Chomper""" objects, height, width = h.world.screen( """ XXXXXXXXXXXXXX T* T XXXXXXXXXXXXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) player = self.world.objects['chomper_player'] self.world.step('LEFT') self.assertTrue(np.array_equal(player.position, (1, 13)), repr(player.position)) self.world.step('LEFT') self.world.step('RIGHT') self.assertTrue(np.array_equal(player.position, (1, 0)), repr(player.position))
def test_respawn(self): # create the objects we want screen = """ ------------------ - - X S * X ------------------ - - - - - - - - - - ------------------ """ objects, width, height = h.world.screen(screen, pixelzuma.legend) self.world = universe.create_world('pixelzuma', objects=objects, width=width, height=height) pedro = self.world.objects['pedro'] posn = pedro.position self.world.step('LEFT') self.world.step('LEFT') self.world.step('NOOP') # check that we respawn in our original position self.assertTrue(np.array_equal(pedro.position, posn))
def test_ghosts_reverse_on_mode_change(self): """Test that ghosts reverse direction when the mode changes""" objects, height, width = h.world.screen( """ XXXXXXXXXXXXXXX X B P I C X XXXXXXXXXXXXXXX X* X XXXXXXXXXXXXXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) player = self.world.objects['chomper_player'] blinky = self.world.objects['blinky'] pinky = self.world.objects['pinky'] inky = self.world.objects['inky'] clyde = self.world.objects['clyde'] # point ghosts left blinky.orientation = 0 pinky.orientation = 0 inky.orientation = 0 clyde.orientation = 0 self.world.time = 29 obs, reward, done, info = self.world.step('NOOP') self.assertEqual(blinky.orientation, 2) self.assertEqual(pinky.orientation, 2) self.assertEqual(inky.orientation, 2) self.assertEqual(clyde.orientation, 2)
def test_actions(self): self.world = universe.create_world('mazes', seed=0, method='subdivide', width=7, height=7) self.world.remove_objects(self.world.objects['wall']) objects, _, _ = h.world.screen( """ WWWWWWW W W W W W W W WW W W W W W W WWWWWWW """, self.world._legend) self.world.create_objects(objects) player = self.world.objects['self'] posn = player.position # check that actions do what we expect self.world.step('RIGHT') self.assertTrue(np.array_equal(player.position, posn + (0, 1))) self.world.step('DOWN') self.assertTrue(np.array_equal(player.position, posn + (1, 1))) self.world.step('LEFT') self.assertTrue(np.array_equal(player.position, posn + (1, 0))) self.world.step('UP') self.assertTrue(np.array_equal(player.position, posn))
def test_energy(self): self.world = universe.create_world('same_vs_different', seed=0) self.world.agent = agents.RandomAgent(self.world) energy = self.world.energy for _ in xrange(100): self.world.step() self.assertEqual(self.world.energy, energy)
def test_position_y(self): seen_same = seen_different = False for seed in xrange(100): if seen_same and seen_different: break self.world = universe.create_world('same_vs_different', seed=seed, attribute='position_y') objects = [x for x in self.world.objects if x.name != 'self'] if objects[0].position_y == objects[1].position_y: seen_same = True # check that true class is correct self.assertEqual(self.world.svd_true_class, 'SAME') # check that we get a reward for correct classification and the # episode terminates obs, reward, done, info = self.world.step('CLASSIFY_SAME') self.assertEqual(reward, 99) self.assertTrue(done) # check that we get a penalty for incorrect classification and # the episode terminates obs, reward, done, info = self.world.step('CLASSIFY_DIFFERENT') self.assertEqual(reward, -101) self.assertTrue(done) # check that we can change the true class objects[0].position_y = objects[1].position_y + 1 self.assertEqual(self.world.svd_true_class, 'DIFFERENT') else: seen_different = True # check that true class is correct self.assertEqual(self.world.svd_true_class, 'DIFFERENT') # check that we get a penalty for incorrect classification and # the episode terminates obs, reward, done, info = self.world.step('CLASSIFY_SAME') self.assertEqual(reward, -101) self.assertTrue(done) # check that we get a reward for correct classification and the # episode terminates obs, reward, done, info = self.world.step('CLASSIFY_DIFFERENT') self.assertEqual(reward, 99) self.assertTrue(done) # check that we can change the true class objects[0].position_y = objects[1].position_y self.assertEqual(self.world.svd_true_class, 'SAME') # check that we've seen both cases self.assertTrue(seen_same) self.assertTrue(seen_different)
def test_energy(self): self.world = universe.create_world('occlusion', seed=0) # check that energy is conserved self.world.agent = agents.RandomAgent(self.world) energy = self.world.energy for _ in xrange(100): self.world.step() self.assertEqual(self.world.energy, energy)
def test_occlusion(self): seen_occlusion = seen_not_occlusion = False for seed in xrange(100): if seen_occlusion and seen_not_occlusion: break self.world = universe.create_world('occlusion', seed=seed) objects = [x for x in self.world.objects if x.name == 'basic'] if np.sum(np.abs(objects[0].position - objects[1].position)) <= 1.0: seen_occlusion = True # check that true class is correct self.assertEqual(self.world.occlusion_true_class, 'OCCLUSION') # check that we get a reward for correct classification and the # episode terminates obs, reward, done, info = self.world.step('CLASSIFY_OCCLUSION') self.assertEqual(reward, 99) self.assertTrue(done) # check that we get a penalty for incorrect classification and # the episode terminates obs, reward, done, info = self.world.step('CLASSIFY_NOT_OCCLUSION') self.assertEqual(reward, -101) self.assertTrue(done) # check that we can change the true class objects[0].position = objects[1].position + (2, 0) self.assertEqual(self.world.occlusion_true_class, 'NOT_OCCLUSION') else: seen_not_occlusion = True # check that true class is correct self.assertEqual(self.world.occlusion_true_class, 'NOT_OCCLUSION') # check that we get a penalty for incorrect classification and # the episode terminates obs, reward, done, info = self.world.step('CLASSIFY_OCCLUSION') self.assertEqual(reward, -101) self.assertTrue(done) # check that we get a reward for correct classification and the # episode terminates obs, reward, done, info = self.world.step('CLASSIFY_NOT_OCCLUSION') self.assertEqual(reward, 99) self.assertTrue(done) # check that we can change the true class objects[0].position = objects[1].position self.assertEqual(self.world.occlusion_true_class, 'OCCLUSION') # check that we've seen both cases self.assertTrue(seen_occlusion) self.assertTrue(seen_not_occlusion)
def test_energy(self): self.world = universe.create_world('mazes', seed=0, method='subdivide', width=7, height=7) self.world.agent = agents.RandomAgent(self.world) energy = self.world.energy for _ in xrange(100): self.world.step() self.assertEqual(self.world.energy, energy)
def test_sword_usable(self): # create the objects we want screen = """ ------------------ - - X S* X ------------------ - - - - - - - - - - ------------------ """ objects, width, height = h.world.screen(screen, pixelzuma.legend) self.world = universe.create_world('pixelzuma', objects=objects, width=width, height=height) # give pedro the sword self.world.objects['pedro'].inventory = ['SWORD'] # check that we get the rewards we expect and episode doesn't end obs, reward, done, info = self.world.step('LEFT') self.assertEqual(reward, 3000) self.assertFalse(done) obs, reward, done, info = self.world.step('LEFT') self.assertEqual(reward, 0) self.assertFalse(done) obs, reward, done, info = self.world.step('LEFT') self.assertEqual(reward, 0) self.assertFalse(done) obs, reward, done, info = self.world.step('LEFT') self.assertEqual(reward, 0) self.assertFalse(done) # check that we didn't die self.assertFalse( any( isinstance(event, pixelzuma.DeathEvent) for event in self.world.events)) # check that we killed the spider kill_events = [ event for event in self.world.events if isinstance(event, pixelzuma.KillEvent) ] self.assertEqual(len(kill_events), 1) self.assertEqual(kill_events[0].type, 'SPIDER') # check that we no longer have the sword self.assertEqual(self.world.objects['pedro'].inventory, [])
def test_sometimes_robots_spawn_same(self): # check that sometimes robots spawn on top of each other, leading to # their deaths robots_died = False for _ in xrange(100): self.world = universe.create_world('robots', width=8, height=8) if not all(robot.alive for robot in self.world.objects['robot']): robots_died = True break self.assertTrue(robots_died)
def test_bounce(self): # create the obejcts we want objects, height, width = h.world.screen( """ WWWW W W W*BW WBBW WPPW """, block_breaker.legend) self.world = universe.create_world('block_breaker', objects=objects, height=height, width=width) ball = self.world.objects['ball'] # point the ball up and to the right ball.velocity = (-1.01, 1) self.assertTrue(np.array_equal(ball.state_index, (2, 1)), repr(ball.state_index)) # step and check ball position obs, reward, done, info = self.world.step('NOOP') self.assertTrue(np.array_equal(ball.state_index, (1, 2))) # step and check ball position obs, reward, done, info = self.world.step('NOOP') self.assertTrue(np.array_equal(ball.state_index, (2, 1))) # step and check ball position obs, reward, done, info = self.world.step('NOOP') self.assertTrue(np.array_equal(ball.state_index, (1, 2))) # step and check ball position obs, reward, done, info = self.world.step('NOOP') self.assertTrue(np.array_equal(ball.state_index, (2, 1))) # step and check ball position obs, reward, done, info = self.world.step('NOOP') self.assertTrue(np.array_equal(ball.state_index, (3, 2))) # step and check ball position obs, reward, done, info = self.world.step('NOOP') self.assertTrue(np.array_equal(ball.state_index, (2, 1))) # step and check ball position obs, reward, done, info = self.world.step('NOOP') self.assertTrue(np.array_equal(ball.state_index, (1, 2)))
def test_actions(self): self.world = universe.create_world('occlusion', seed=0) player = self.world.objects['classifier'] posn = player.position # check that actions do what we expect self.world.step('RIGHT') self.assertTrue(np.array_equal(player.position, posn + (0, 1))) self.world.step('DOWN') self.assertTrue(np.array_equal(player.position, posn + (1, 1))) self.world.step('LEFT') self.assertTrue(np.array_equal(player.position, posn + (1, 0))) self.world.step('UP') self.assertTrue(np.array_equal(player.position, posn))
def test_walls_unpushable_and_goal_rewards(self): self.world = universe.create_world('mazes', seed=0, method='subdivide', width=7, height=7) self.world.remove_objects(self.world.objects['wall']) self.world.remove_objects(self.world.objects['goal']) objects, _, _ = h.world.screen( """ WWWWWWW W W W W W W W WW W W W W W GW WWWWWWW """, self.world._legend) self.world.create_objects(objects) player = self.world.objects['self'] posn = player.position # check that we can't push the walls self.world.step('UP') self.assertTrue(np.array_equal(player.position, posn)) self.world.step('LEFT') self.assertTrue(np.array_equal(player.position, posn)) self.world.step('RIGHT') self.assertTrue(np.array_equal(player.position, posn)) # solve the maze self.world.step('DOWN') self.world.step('DOWN') self.world.step('RIGHT') self.world.step('RIGHT') self.world.step('RIGHT') self.world.step('DOWN') self.world.step('DOWN') obs, reward, done, info = self.world.step('RIGHT') # check that the episode terminated and we got a reward self.assertTrue(done) self.assertEqual(reward, 999)
def __init__(self, name, run=True, end_prompt=True, **kwargs): kwargs['agent'] = core.Agent print 'ran init' self._name = name self._end_prompt = end_prompt #create the environment start = time.clock() self.world = universe.create_world(name, defaults=self._defaults, ignore_world_agent=True, **kwargs) end = time.clock() print name, end - start, '???' self.lag = end - start
def test_ghost_wall(self): """Test that ghosts cannot go through walls""" objects, height, width = h.world.screen(""" XXX* XBXX XXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) blinky = self.world.objects['blinky'] posn = blinky.position for _ in xrange(10): obs, reward, done, info = self.world.step('NOOP') # check that blinky didn't move self.assertTrue(np.array_equal(blinky.position, posn))
def test_chomper_cannot_go_through_ghost(self): """Test that Chomper cannot go through a ghost""" objects, height, width = h.world.screen(""" XXXX XB*X XXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) # point blinky right blinky = self.world.objects['blinky'] blinky.orientation = 2 obs, reward, done, info = self.world.step('LEFT') self.assertTrue(done) self.assertEqual(reward, -1001)
def test_chomper_wall(self): """Check that Chomper can't go through walls""" objects, height, width = h.world.screen(""" XXX X*X XXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) player = self.world.objects['chomper_player'] posn = player.position for action in ['LEFT', 'UP', 'RIGHT', 'DOWN']: obs, reward, done, info = self.world.step(action) # check that we didn't move self.assertTrue(np.array_equal(player.position, posn))
def test_clyde_choice(self): """Test that Clyde always makes the right choice when turning""" objects, height, width = h.world.screen( """ XXXXXXXXXXXXXX X X XXXXXXXCXXXXXX X *X XXXXXXXXXXXXXX """, chomper.legend) self.world = universe.create_world('chomper', objects=objects, width=width, height=height) player = self.world.objects['chomper_player'] player.orientation = 0 # left clyde = self.world.objects['clyde'] clyde.orientation = 3 # down self.world.step('NOOP') self.assertTrue(np.array_equal(clyde.position, (3, 7)), repr(clyde.position)) self.world.step('NOOP') self.assertTrue(np.array_equal(clyde.position, (3, 6)), repr(clyde.position)) clyde.position = clyde.initial_position clyde.orientation = 1 # up, so clyde will go down after flip self.world.time = 29 self.world.step('NOOP') self.assertTrue(np.array_equal(clyde.position, (3, 7)), repr(clyde.position)) self.world.step('NOOP') self.assertTrue(np.array_equal(clyde.position, (3, 6)), repr(clyde.position))