def test_prev_state_probability(self): # Test prev_state not visible from a a = geometry_helpers.Point(6, 2) goal = geometry_helpers.Point(34, 22) visible_obstacles = environment_details.visible_obstacles sim_agent = agent.Agent(goal, None, visible_obstacles) sim_agent.prev_result = {} sim_agent.prev_cost_estimates = {} sim_agent.prev_state = agent.State(geometry_helpers.Point(32, 23), 1) self.assertEqual(0, sim_agent._prev_prob(a)) # Test location A not an optimal move from prev_state a = geometry_helpers.Point(5, 20) sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 1) sim_agent.prev_result[(sim_agent.prev_state.location, a)] = a sim_agent.prev_cost_estimates[a] = 50 self.assertEqual(0, sim_agent._prev_prob(a)) # Test location A is visible from prev_state, and is an # optimal move from prev_state sim_agent.prev_result = {} sim_agent.prev_cost_estimates = {} sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 1) a = geometry_helpers.Point(6, 10) self.assertEqual(0.375, sim_agent._prev_prob(a))
def test_refine_possible_locations(self): # Sanity check, no possible locations obstacles = environment_details.visible_obstacles possible_locations = [] goal = geometry_helpers.Point(34, 22) sim_agent = agent.Agent(goal, None, obstacles) sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 1) sim_agent.prev_result = {} sim_agent.prev_cost_estimates = {} self.assertEqual([], sim_agent._refine_loc(possible_locations)) # prev_state not visible from any of the possible locations possible_locations = [geometry_helpers.Point(35, 21)] self.assertEqual([], sim_agent._refine_loc(possible_locations)) # only point (6, 10) from possible_locations is visible possible_locations = [geometry_helpers.Point(6, 10), geometry_helpers.Point(35, 21)] self.assertEqual([agent.State(geometry_helpers.Point(6, 10), 0.375)], sim_agent._refine_loc(possible_locations)) # Sanity check, a refined list of locations shouldn't be # updated again. possible_locations = [geometry_helpers.Point(6, 10)] self.assertEqual([agent.State(geometry_helpers.Point(6, 10), 0.375)], sim_agent._refine_loc(possible_locations))
def test_LRTA_star_agent(self): visible_obstacles = environment_details.visible_obstacles goal_point = geometry_helpers.Point(4, 5) agent_location = geometry_helpers.Point(4, 5) sim_agent = agent.Agent(goal_point, agent.State(agent_location, 1), visible_obstacles) sim_agent.result = {} sim_agent.cost_estimates = {} sim_agent.prev_state = agent.State(None, 1) sim_agent.prev_action = None sim_agent.LRTA_star_agent() self.assertEqual(None, sim_agent.prev_action) self.assertEqual(None, sim_agent.prev_state.location) self.assertEqual({}, sim_agent.result) self.assertEqual({}, sim_agent.cost_estimates) # Update goal point so agent has to plan an action sim_agent.goal = geometry_helpers.Point(34, 22) sim_agent.LRTA_star_agent() self.assertEqual(sim_agent.cost_estimates[sim_agent.prev_state.location], percepts.heuristic(sim_agent.prev_state.location, sim_agent.goal)) self.assertFalse((None, None) in sim_agent.result) self.assertFalse(None in sim_agent.cost_estimates) self.assertEqual(geometry_helpers.Point(6, 2), sim_agent.prev_action) self.assertEqual(geometry_helpers.Point(4, 5), sim_agent.prev_state.location) sim_agent.belief_state = agent.State( percepts.perform_action(sim_agent.prev_action), 1) sim_agent.LRTA_star_agent() self.assertEqual(sim_agent.prev_state.location, sim_agent.result[(geometry_helpers.Point(4, 5), geometry_helpers.Point(6, 2))]) self.assertEqual(34.48187929913333, sim_agent.cost_estimates[geometry_helpers.Point(4, 5)]) self.assertEqual(geometry_helpers.Point(18, 2), sim_agent.prev_action) self.assertEqual(geometry_helpers.Point(6, 2), sim_agent.prev_state.location)
def test_update_agent_location(self): possible_locations = [geometry_helpers.Point(6, 10)] obstacles = environment_details.visible_obstacles goal = geometry_helpers.Point(34, 22) sim_agent = agent.Agent(goal, None, obstacles) sim_agent.prev_state = None sim_agent.prev_action = geometry_helpers.Point(6, 10) sim_agent.prev_result = {} sim_agent.prev_cost_estimates = {} sim_agent.belief_history = [] # Agent is certain of its current location sim_agent.update_agent_location(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) self.assertEqual([], sim_agent.belief_history) # Agent is uncertain of current location and does not # have a previous location for context possible_locations.append(geometry_helpers.Point(18, 2)) sim_agent.update_agent_location(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) self.assertEqual([[agent.State(geometry_helpers.Point(6, 10), 0.5), agent.State(geometry_helpers.Point(18, 2), 0.5)]], sim_agent.belief_history) # Agent is uncertain of current location but # has previous state for context sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 0.7) sim_agent.belief_history = [] sim_agent.update_agent_location(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) self.assertEqual([[agent.State(geometry_helpers.Point(6, 10), 0.5), agent.State(geometry_helpers.Point(18, 2), 0.5)]], sim_agent.belief_history)
def test_update_certain(self): possible_locations = [geometry_helpers.Point(6, 10)] sim_agent = agent.Agent(None, None, None) sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 0.7) sim_agent.prev_action = geometry_helpers.Point(6, 10) sim_agent.belief_history = [[agent.State(geometry_helpers.Point(5, 3), 0.8)]] # Agent is uncertain of past location, update previous belief states sim_agent._update_certain(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) # TODO This will change when we add update_previous_belief logic # Add logic to check that agent.results were updated self.assertEqual([], sim_agent.belief_history) # Agent is certain of past location, and ended at # intended destination. sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 1) sim_agent._update_certain(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) self.assertEqual([], sim_agent.belief_history) # Agent is certain of past location, but did not end up at # intended destination. # TODO Add logic to check that recovery scheme worked sim_agent.prev_action = geometry_helpers.Point(18, 2) sim_agent._update_certain(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) # TODO This will change when we add update_previous_belief logic # Add logic to check that agent.results were updated self.assertEqual([], sim_agent.belief_history)
def test_current_state_probability(self): a = geometry_helpers.Point(6, 10) obstacles = environment_details.visible_obstacles goal = geometry_helpers.Point(34, 22) sim_agent = agent.Agent(goal, None, obstacles) sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 1) sim_agent.prev_cost_estimates = {} sim_agent.prev_result = {} self.assertEqual(0.375, sim_agent._current_prob(a))
def agent_reached_goal(sim_agent, goal_reward): """ Award goal_points to the agent for reaching the goal and reset it to a new starting point. After resetting the agent, return the agent's true current location. """ sim_agent.belief_state = agent.State( percepts.get_new_position(env.visible_obstacles, env.x_bounds, env.y_bounds), 1) sim_agent.belief_history = [] sim_agent.prev_state = agent.State(None, 1) sim_agent.score += goal_reward sim_agent.reached_goal = True actual_location = sim_agent.belief_state.location print('Resetting agent to point {}'.format( sim_agent.belief_state.location)) print('Agent score: {}'.format(sim_agent.score)) return actual_location
def run_simulation(number_of_turns, goal_point, goal_reward, initial_location): """ Given the number of turns allowed, run the simulation for the agent navigating a maze of polygons. """ sim_agent = agent.Agent(goal_point, agent.State(initial_location, 1), env.visible_obstacles) actual_location = initial_location remaining_turns = number_of_turns print('Agent starting at point {}'.format(initial_location)) print('Agent goal: {}'.format(goal_point)) while remaining_turns > 0: print('Agent currently believes it is at point {}'.format( sim_agent.belief_state.location)) print('Agent is actually at point {}'.format(actual_location)) sim_agent.LRTA_star_agent() if not sim_agent.prev_action: actual_location = agent_reached_goal(sim_agent, goal_reward) continue actual_location = sim_perform_action(sim_agent, actual_location) sim_agent_action(sim_agent, actual_location) print('Agent now at point {}'.format(actual_location)) print('Agent score: {}'.format(sim_agent.score)) remaining_turns -= 1 if not sim_agent.reached_goal: print('Agent failed to find goal in alloted turns.') print('Final agent location: {}'.format(agent_location)) return sim_agent.reached_goal
def test_update_uncertain(self): possible_locations = [geometry_helpers.Point(6, 10), geometry_helpers.Point(35, 21)] obstacles = environment_details.visible_obstacles goal = geometry_helpers.Point(34, 22) sim_agent = agent.Agent(goal, None, obstacles) sim_agent.prev_state = agent.State(geometry_helpers.Point(6, 2), 1) sim_agent.prev_action = geometry_helpers.Point(6, 10) sim_agent.prev_result = {} sim_agent.prev_cost_estimates = {} sim_agent.belief_history = [[agent.State(geometry_helpers.Point(5, 3), 0.8)]] # Agent is initially uncertain of current location # but is able to refine it sim_agent._update_uncertain(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) self.assertEqual([], sim_agent.belief_history) # After refining the current possible locations there are no # possible results, so agent searches back in its history to # find a previous state that fits with the current possibilities. sim_agent.belief_history = [[agent.State(geometry_helpers.Point(35, 21), 0.8), agent.State(geometry_helpers.Point(6, 2), 0.2)]] sim_agent.prev_state = agent.State(geometry_helpers.Point(35, 21), 1) sim_agent._update_uncertain(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) self.assertEqual([], sim_agent.belief_history) # Agent once again has to search back in its history to find a # compatible previous state, but this time is still not # certain of its current location afterwards sim_agent.belief_history = [[agent.State(geometry_helpers.Point(35, 21), 0.8), agent.State(geometry_helpers.Point(6, 2), 0.2)]] sim_agent.prev_state = agent.State(geometry_helpers.Point(35, 21), 1) possible_locations.append(geometry_helpers.Point(18, 2)) sim_agent._update_uncertain(possible_locations) self.assertEqual(geometry_helpers.Point(6, 10), sim_agent.belief_state.location) self.assertEqual([[agent.State(geometry_helpers.Point(35, 21), 0.8), agent.State(geometry_helpers.Point(6, 2), 0.2)], [agent.State(geometry_helpers.Point(6, 10), 0.375), agent.State(geometry_helpers.Point(18, 2), 0.3)]], sim_agent.belief_history)