Beispiel #1
0
    def _current_prob(self, a):
        """
        Given that the agent was previously in prev_state, return the
        probability that the agent is currently in state a.
        """
        a_locations = len(
            percepts.get_locations(
                percepts.visible_vertices(a, self.visible_obstacles),
                self.visible_obstacles))

        b_locations = len(
            percepts.get_locations(
                percepts.visible_vertices(self.prev_state.location,
                                          self.visible_obstacles),
                self.visible_obstacles))

        p_of_a = 1 / a_locations
        p_of_b = 1 / b_locations
        p_of_b_given_a = self._prev_prob(a)

        if p_of_b == 0:
            # Unless the agent is somehow teleported inside of a circle,
            # this should never happen
            return 0
        else:
            # Bayes theorem
            return p_of_b_given_a * p_of_a / p_of_b
Beispiel #2
0
    def test_vertices_relative_to_agent(self):
        visible_obstacles = environment_details.visible_obstacles

        # Test arbitrary points
        p = geometry_helpers.Point(5, 5)
        vertices = [
            geometry_helpers.Point(7, 7),
            geometry_helpers.Point(4, 6),
            geometry_helpers.Point(5, 3),
            geometry_helpers.Point(1, 5),
            geometry_helpers.Point(7, 0)
        ]

        self.assertEqual(
            set([
                geometry_helpers.Point(2, 2),
                geometry_helpers.Point(-1, 1),
                geometry_helpers.Point(0, -2),
                geometry_helpers.Point(-4, 0),
                geometry_helpers.Point(2, -5)
            ]), set(percepts.vertices_relative_to_agent(vertices, p)))

        # Test open space in maze near top-left corner
        p = geometry_helpers.Point(34, 22)
        vertices = percepts.visible_vertices(p, visible_obstacles)

        self.assertEqual(
            set([geometry_helpers.Point(-2, 1),
                 geometry_helpers.Point(1, -1)]),
            set(percepts.vertices_relative_to_agent(vertices, p)))

        # Test on hexagon vertex in maze
        p = geometry_helpers.Point(32, 6)
        vertices = percepts.visible_vertices(p, visible_obstacles)

        self.assertEqual(
            set([
                geometry_helpers.Point(-3, -3),
                geometry_helpers.Point(0, 3),
                geometry_helpers.Point(3, 15)
            ]), set(percepts.vertices_relative_to_agent(vertices, p)))

        # Test open space in maze in between pentagon and triangle
        p = geometry_helpers.Point(13, 19)
        vertices = percepts.visible_vertices(p, visible_obstacles)

        self.assertEqual(
            set([
                geometry_helpers.Point(-1, 0),
                geometry_helpers.Point(-3, -5),
                geometry_helpers.Point(-4.5, 4),
                geometry_helpers.Point(0, -5),
                geometry_helpers.Point(1.5, 2)
            ]), set(percepts.vertices_relative_to_agent(vertices, p)))
Beispiel #3
0
    def test_get_locations(self):
        visible_obstacles = environment_details.visible_obstacles

        # Test upper-right corner (open space)
        p = geometry_helpers.Point(34, 22)
        vertex_list = percepts.visible_vertices(p, visible_obstacles)
        agent_vertex_list = percepts.vertices_relative_to_agent(vertex_list, p)

        self.assertEqual(
            set([
                geometry_helpers.Point(34, 22),
                geometry_helpers.Point(18, 13)
            ]),
            set(percepts.get_locations(agent_vertex_list, visible_obstacles)))

        # Test upper-left corner above pentagon (open space)
        p = geometry_helpers.Point(12, 21)
        vertex_list = percepts.visible_vertices(p, visible_obstacles)
        agent_vertex_list = percepts.vertices_relative_to_agent(vertex_list, p)

        self.assertEqual(
            set([geometry_helpers.Point(12, 21)]),
            set(percepts.get_locations(agent_vertex_list, visible_obstacles)))

        # Test lower-right corner of hexagon (on polygon)
        p = geometry_helpers.Point(32, 6)
        vertex_list = percepts.visible_vertices(p, visible_obstacles)
        agent_vertex_list = percepts.vertices_relative_to_agent(vertex_list, p)

        self.assertEqual(
            set([geometry_helpers.Point(32, 6)]),
            set(percepts.get_locations(agent_vertex_list, visible_obstacles)))

        # Test open space in middle
        p = geometry_helpers.Point(5, 20)
        vertex_list = percepts.visible_vertices(p, visible_obstacles)
        agent_vertex_list = percepts.vertices_relative_to_agent(vertex_list, p)

        self.assertEqual(
            set([geometry_helpers.Point(5, 20)]),
            set(percepts.get_locations(agent_vertex_list, visible_obstacles)))

        # Test far-left side of pentagon (on polygon)
        p = geometry_helpers.Point(20, 16)
        vertex_list = percepts.visible_vertices(p, visible_obstacles)
        agent_vertex_list = percepts.vertices_relative_to_agent(vertex_list, p)

        self.assertEqual(
            set([geometry_helpers.Point(20, 16)]),
            set(percepts.get_locations(agent_vertex_list, visible_obstacles)))
Beispiel #4
0
    def _prev_prob(self, a):
        """
        Given that agent is currently at A, return the
        probability that the agent was previously at prev_state.
        """
        def action_cost(action):
            return self.LRTA_star_cost(
                self.prev_state.location, action,
                self.prev_result.get((self.prev_state.location, action)),
                self.prev_cost_estimates, self.goal)

        visible_from_a = percepts.visible_vertices(a, self.visible_obstacles)

        best_actions_prev = self.best_actions(self.prev_state.location,
                                              action_cost)

        if (self.prev_state.location in visible_from_a
                and a in best_actions_prev):
            # Only consider vertices visible from A which have
            # A as one of the lowest values for lrta_star_cost
            best_actions_v = [
                v for v in visible_from_a
                if a in self.best_actions(v, action_cost)
            ]

            if len(best_actions_v) == 0:
                return 0
            else:
                return (len(best_actions_prev) / len(best_actions_v))
        else:
            return 0
Beispiel #5
0
def sim_agent_action(sim_agent, actual_location):
    """
    Allow agent to update its location based on the visible vertices
    relative to its current position and update its score accordingly.
    """
    relative_verts = percepts.vertices_relative_to_agent(
        percepts.visible_vertices(actual_location, env.visible_obstacles),
        actual_location)
    initial_locations = percepts.get_locations(relative_verts,
                                               env.visible_obstacles)

    sim_agent.update_agent_location(initial_locations)

    sim_agent.score -= geom.distance(sim_agent.prev_state.location,
                                     actual_location)
Beispiel #6
0
    def test_visible_vertices(self):
        visible_obstacles = environment_details.visible_obstacles

        # Test upper-right corner (open space)
        p = geometry_helpers.Point(34, 22)

        self.assertEqual(
            set([
                geometry_helpers.Point(32, 23),
                geometry_helpers.Point(35, 21)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))

        # Test lower-left corner (open space)
        p = geometry_helpers.Point(5, 1)

        self.assertEqual(
            set([
                geometry_helpers.Point(5, 20),
                geometry_helpers.Point(6, 2),
                geometry_helpers.Point(6, 10),
                geometry_helpers.Point(18, 2)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))

        # Test bottom-middle (open space)
        p = geometry_helpers.Point(23, 8)

        self.assertEqual(
            set([
                geometry_helpers.Point(18, 2),
                geometry_helpers.Point(19, 7),
                geometry_helpers.Point(23, 9),
                geometry_helpers.Point(27, 11),
                geometry_helpers.Point(29, 12),
                geometry_helpers.Point(29, 3),
                geometry_helpers.Point(26, 9),
                geometry_helpers.Point(26, 6)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))

        # Test bottom-middle (on triangle)
        p = geometry_helpers.Point(23, 9)

        self.assertEqual(
            set([
                geometry_helpers.Point(18, 2),
                geometry_helpers.Point(19, 7),
                geometry_helpers.Point(19, 12),
                geometry_helpers.Point(26, 6),
                geometry_helpers.Point(16, 17),
                geometry_helpers.Point(22, 11),
                geometry_helpers.Point(26, 9),
                geometry_helpers.Point(27, 11)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))

        # Test far-right vertex of pentagon
        p = geometry_helpers.Point(12, 19)

        self.assertEqual(
            set([
                geometry_helpers.Point(8.5, 23),
                geometry_helpers.Point(14.5, 21),
                geometry_helpers.Point(10, 14),
                geometry_helpers.Point(13, 14)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))

        # Test bottom of quadrilateral (on vertex)
        p = geometry_helpers.Point(32, 11)

        self.assertEqual(
            set([
                geometry_helpers.Point(29, 12),
                geometry_helpers.Point(32, 9),
                geometry_helpers.Point(27, 23),
                geometry_helpers.Point(29, 21),
                geometry_helpers.Point(35, 21)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))

        # Test upper-left corner of rectangle2
        p = geometry_helpers.Point(22, 23)

        self.assertEqual(
            set([
                geometry_helpers.Point(27, 23),
                geometry_helpers.Point(22, 11),
                geometry_helpers.Point(18.5, 23),
                geometry_helpers.Point(21, 20),
                geometry_helpers.Point(18, 10),
                geometry_helpers.Point(19, 12)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))

        # Test top of triangle1 (on triangle)
        p = geometry_helpers.Point(14.5, 21)

        self.assertEqual(
            set([
                geometry_helpers.Point(13, 14),
                geometry_helpers.Point(16, 14),
                geometry_helpers.Point(18.5, 23),
                geometry_helpers.Point(16.5, 21.5),
                geometry_helpers.Point(10, 14),
                geometry_helpers.Point(16, 17),
                geometry_helpers.Point(12, 19),
                geometry_helpers.Point(8.5, 23),
                geometry_helpers.Point(19, 7),
                geometry_helpers.Point(18, 10)
            ]), set(percepts.visible_vertices(p, visible_obstacles)))