예제 #1
0
    def __init__(self, size, n_elements_per_dim, size_elements,
                 **playground_params):

        super().__init__(size=size, **playground_params)

        basic_cfg_keys = [
            'rectangle', 'square', 'pentagon', 'triangle', 'hexagon'
        ]

        for ind_x in range(n_elements_per_dim):
            for ind_y in range(n_elements_per_dim):

                coord_x = (1 + ind_x) * size[0] / (n_elements_per_dim + 1)
                coord_y = (1 + ind_x) * size[1] / (n_elements_per_dim + 1)
                orientation = random.uniform(0, 2 * math.pi)

                cfg = random.choice(basic_cfg_keys)

                if cfg == 'rectangle':
                    element = Physical(config_key=cfg,
                                       name='test',
                                       size=(size_elements, size_elements))
                else:
                    element = Physical(config_key=cfg,
                                       name='test',
                                       radius=size_elements)

                self.add_element(element,
                                 initial_coordinates=((coord_x, coord_y),
                                                      orientation))
예제 #2
0
    def __init__(self, size=(200, 200), **playground_params):

        super().__init__(size=size, **playground_params)

        rectangle_01 = Physical(config_key='rectangle',
                                graspable=True,
                                mass=10)
        self.add_element(rectangle_01, ((150, 160), 0.2))

        circle_01 = Physical(config_key='circle',
                             graspable=True,
                             mass=10,
                             texture=[150, 150, 150])
        self.add_element(circle_01, ((50, 50), 0))

        square_01 = Physical(config_key='square', graspable=True, mass=10)
        self.add_element(square_01, ((150, 60), math.pi / 4))

        pentagon_01 = Physical(config_key='pentagon',
                               radius=15,
                               graspable=True,
                               mass=10)
        self.add_element(pentagon_01, ((50, 160), 0))

        hexagon_01 = Physical(config_key='hexagon', graspable=True, mass=10)
        self.add_element(hexagon_01, ((100, 100), 0))
예제 #3
0
    def __init__(self, size=(300, 700), **playground_params):

        super().__init__(size=size, **playground_params)

        pos_left = (100, 100)
        pos_right = (200, 100)
        vis_beam = InvisibleBeam(destination=(pos_right, math.pi))
        self.add_element(vis_beam, (pos_left, 0))

        pos_left = pos_left[0], pos_left[1] + 100
        pos_right = pos_right[0], pos_right[1] + 100
        coord_sampler = CoordinateSampler(pos_right,
                                          area_shape='circle',
                                          radius=20)
        vis_beam = InvisibleBeam(destination=coord_sampler, keep_inertia=False)
        self.add_element(vis_beam, (pos_left, 0))

        pos_left = pos_left[0], pos_left[1] + 100
        pos_right = pos_right[0], pos_right[1] + 100
        target = Physical(config_key='circle', radius=5)
        self.add_element(target, (pos_right, 0))
        homing = VisibleBeamHoming(destination=target,
                                   keep_inertia=True,
                                   relative_teleport=False)
        self.add_element(homing, (pos_left, math.pi))

        pos_left = pos_left[0], pos_left[1] + 100
        pos_right = pos_right[0], pos_right[1] + 100
        target = Physical(config_key='circle', radius=5)
        self.add_element(target, (pos_right, math.pi / 2))
        homing = VisibleBeamHoming(destination=target,
                                   relative_teleport=True,
                                   keep_inertia=False)
        self.add_element(homing, (pos_left, 0))

        pos_left = pos_left[0], pos_left[1] + 100
        pos_right = pos_right[0], pos_right[1] + 100
        target = Traversable(config_key='circle', radius=10)
        self.add_element(target, (pos_right, math.pi / 2))
        homing = VisibleBeamHoming(destination=target,
                                   relative_teleport=True,
                                   keep_inertia=False)
        self.add_element(homing, (pos_left, 0))

        pos_left = pos_left[0] - 90, pos_left[1] + 100
        pos_right = pos_right[0] + 90, pos_right[1] + 100
        portal_red = Portal(color=PortalColor.RED)
        self.add_element(portal_red, (pos_left, 0))
        portal_blue = Portal(color=PortalColor.BLUE)
        self.add_element(portal_blue, (pos_right, math.pi))

        portal_red.destination = portal_blue
        portal_blue.destination = portal_red
예제 #4
0
def test_physical_physical():
    # non traversable vs non traversable
    playground = SingleRoom(size=(200, 200))

    elem_1 = Physical(radius=20, movable=True, mass=5, config_key='circle')
    playground.add_element(elem_1, initial_coordinates=((100, 100), 0))

    elem_2 = Physical(radius=20, movable=True, mass=5, config_key='circle')
    playground.add_element(elem_2, initial_coordinates=((100, 100), 0))

    playground.update(10)
    playground.update(10)
    playground.update(10)

    assert elem_1.position != elem_2.position
예제 #5
0
    def __init__(
        self,
        time_limit=1000,
        reward_reached_time_limit=-10,
        reward_reached_endgoal=10,
        reward_reached_deathtrap=-10,
        playground_seed: Optional[int] = None,
    ):

        super().__init__(size=(200, 200), playground_seed=playground_seed)

        # Starting area of the agent
        area_center = self.grid_rooms[0][0].center
        area_start = CoordinateSampler(center=area_center,
                                       area_shape='rectangle',
                                       size=(100, 100))
        self.initial_agent_coordinates = area_start

        # Visual Cues for the agent to orient itself.
        obstacle_1 = Physical(config_key='pentagon', radius=9)
        self.add_element(obstacle_1, ((60, 30), 0.34))

        obstacle_2 = Physical(config_key='rectangle', size=[8, 12])
        self.add_element(obstacle_2, ((130, 150), 1.7))

        obstacle_3 = Physical(config_key='square', radius=8)
        self.add_element(obstacle_3, ((40, 140), 0.4))

        obstacle_4 = Physical(physical_shape='triangle',
                              radius=14,
                              texture=(150, 200, 200))
        self.add_element(obstacle_4, ((160, 60), 0))

        self.goal = None
        self.cue = None

        self.goal_locations = (((20, 20), 0), ((180, 20), 0), ((180, 180), 0),
                               ((20, 180), 0))
        self.cue_colors = ((200, 50, 200), (50, 200, 50), (200, 50, 50),
                           (50, 50, 200))

        self.reward_goal = reward_reached_endgoal
        self.reward_deathtrap = reward_reached_deathtrap

        self._set_goal()

        self.time_limit = time_limit
        self.time_limit_reached_reward = reward_reached_time_limit
예제 #6
0
def test_grasping():

    playground = SingleRoom(size=(200, 200))

    agent_1 = BaseAgent(
        controller=External(),
        interactive=True,
        rotate=True,
        lateral=False,
        radius=10,
    )
    playground.add_agent(agent_1, ((100, 100), 0))

    elem = Physical(config_key='circle', mass=5, radius=10, graspable=True)
    initial_position_elem = ((100 + agent_1.base_platform.radius + elem.radius + 2, 100), 0)
    playground.add_element(elem, initial_position_elem)

    engine = Engine(playground)

    actions = {agent_1: {agent_1.grasp: 1, agent_1.rotation_velocity: 1}}
    engine.step(actions)
    engine.step(actions)

    assert (elem.position, elem.angle) != initial_position_elem
    assert elem.held_by[0].part.agent is agent_1

    engine.step()
    assert not elem.held_by
예제 #7
0
    def _set_goal(self):

        index_goal = random.randint(0, 3)
        loc = self.goal_locations[index_goal]
        col = self.cue_colors[index_goal]

        if self.goal is not None:
            self._remove_element_from_playground(self.goal)
            self._remove_element_from_playground(self.cue)

        self.cue = Physical(physical_shape='circle',
                            radius=10,
                            texture=col,
                            is_temporary_entity=True)
        self.add_element(self.cue, ((100, 100), 0))

        self.goal = GoalZone(reward=self.reward_goal, is_temporary_entity=True)
        self.add_element(self.goal, loc)

        for i in range(4):
            if i != index_goal:
                loc = self.goal_locations[i]
                other_goal = DeathZone(reward=self.reward_deathtrap,
                                       is_temporary_entity=True)
                self.add_element(other_goal, loc)
예제 #8
0
    def __init__(self, size=(400, 200), **playground_params):

        super().__init__(size=size, **playground_params)

        rectangle_01 = Physical(config_key='rectangle', name='test')
        self.add_element(rectangle_01,
                         initial_coordinates=((150, 160), math.pi / 4))

        circle_01 = Traversable(config_key='circle',
                                movable=False,
                                mass=100,
                                texture=[150, 150, 150])
        self.add_element(circle_01, ((50, 50), 0))

        square_01 = Physical(config_key='square', movable=True, mass=10)
        self.add_element(square_01, ((150, 60), 0))

        pentagon_01 = Physical(config_key='pentagon', radius=15)
        self.add_element(pentagon_01, ((50, 160), math.pi / 2))

        tri_01 = Physical(config_key='triangle', movable=False, mass=5)
        self.add_element(tri_01, ((100, 100), math.pi / 4))

        tri_01 = Physical(config_key='triangle',
                          movable=False,
                          mass=5,
                          radius=20)
        self.add_element(tri_01, ((300, 66), 0))

        tri_01 = Physical(config_key='triangle',
                          movable=False,
                          mass=5,
                          radius=20)
        self.add_element(tri_01, ((300, 133), math.pi / 3))
예제 #9
0
def basic_element(request, is_movable, elem_radius):

    if request.param == 'rectangle':
        kwargs = {'size': (elem_radius, elem_radius)}
    elif request.param == 'polygon':
        kwargs = {'vertices': [[-10, 0], [0, 5], [-2, 0], [0, -5], [-10, 0]]}
    else:
        kwargs = {'radius': elem_radius}

    return Physical(config_key=request.param,
                    movable=is_movable,
                    mass=10,
                    **kwargs)
예제 #10
0
    def __init__(self):

        super().__init__(size=(200, 200))

        texture_obj = {'texture_type': 'color', 'color': [100, 220, 170]}

        obj_params = {
            'texture': texture_obj,
            'physical_shape': 'square',
            'radius': 22
        }

        self.my_obj = Physical(**obj_params)

        self.add_element(self.my_obj, ((150, 160), 0.2))
예제 #11
0
def test_agent_overlapping(base_forward_agent_random, empty_playground):

    elem = Physical(config_key='square')
    empty_playground.add_element(elem, ((50, 50), 0))
    empty_playground.add_agent(base_forward_agent_random, ((50, 50), 0))

    assert empty_playground._overlaps(base_forward_agent_random)
    assert empty_playground._overlaps(base_forward_agent_random, elem)

    empty_playground.remove_agent(base_forward_agent_random)

    with pytest.raises(ValueError):
        empty_playground.add_agent(base_forward_agent_random, ((50, 50), 0),
                                   allow_overlapping=False)

    with pytest.raises(ValueError):
        empty_playground.add_agent(base_forward_agent_random)
        empty_playground.add_agent(base_forward_agent_random)
예제 #12
0
def test_grasping_sensor():

    playground = SingleRoom(size=(200, 200))

    agent_1 = BaseAgent(
        controller=External(),
        interactive=True,
        rotate=True,
        lateral=False,
        radius=10,
    )

    rgb = RgbCamera(anchor=agent_1.base_platform)
    agent_1.add_sensor(rgb)

    playground.add_agent(agent_1, ((100, 100), 0))

    elem = Physical(config_key='circle', mass=5, radius=10, graspable=True)
    initial_position_elem = ((100 + agent_1.base_platform.radius + elem.radius + 2, 100), 0)
    playground.add_element(elem, initial_position_elem)

    engine = Engine(playground)
    engine.step()
    engine.update_observations()
    obs_1 = rgb.sensor_values[:]

    actions = {agent_1: {agent_1.grasp: 1}}
    engine.step(actions)
    engine.update_observations()
    obs_2 = rgb.sensor_values[:]

    engine.update_observations(grasped_invisible=True)
    obs_3 = rgb.sensor_values[:]

    assert (obs_1 == obs_2).all()
    assert (obs_3 != obs_1).any()

    playground.remove_add_within(elems_remove=[elem], elems_add=[])
    engine.step()
    engine.update_observations()
    obs_4 = rgb.sensor_values[:]

    assert (obs_4 == obs_3).all()
예제 #13
0
def test_beam_homing(base_forward_interactive_agent_external):

    playground = SingleRoom(size=(200, 200))
    agent = base_forward_interactive_agent_external

    destination = Physical(config_key='pentagon')
    playground.add_element(destination, ((70, 70), 0))

    beam = VisibleBeamHoming(destination=destination, invisible_range=4)

    playground.add_agent(agent, ((100, 100), 0))
    playground.add_element(beam, ((140, 100), 0))

    engine = Engine(playground, time_limit=100)

    actions = {agent: {agent.longitudinal_force: 1}}

    while not agent.has_teleported:
        engine.step(actions)

    assert agent.position.get_distance(destination.position) < agent.base_platform.radius + destination.radius + 4 + 3
예제 #14
0
    def __init__(self, size=(200, 400), **playground_params):

        super().__init__(size=size, **playground_params)

        self.initial_agent_coordinates = ((100, 50), 3.14)

        x_dispenser = 50
        x_area = 150

        # Dispenser on Area
        area = CoordinateSampler(area_shape='rectangle',
                                 center=(x_area, 50),
                                 size=(20, 60),
                                 angle=math.pi / 3)
        dispenser = Dispenser(
            element_produced=Poison,
            element_produced_params={'reward': -5},
            production_area=area,
            production_limit=20,
        )
        self.add_element(dispenser, ((x_dispenser, 50), 0))

        # Dispenser on Area
        area = CoordinateSampler(area_shape='circle',
                                 center=(x_area, 100),
                                 radius=30)
        dispenser = Dispenser(
            element_produced=Poison,
            element_produced_params={'reward': -5},
            production_area=area,
        )
        self.add_element(dispenser, ((x_dispenser, 100), 0))

        # Dispenser on Area
        area = CoordinateSampler(area_shape='circle',
                                 center=(x_area, 150),
                                 radius=50,
                                 min_radius=30)
        dispenser = Dispenser(
            element_produced=Candy,
            element_produced_params={'reward': 5},
            production_area=area,
        )
        self.add_element(dispenser, ((x_dispenser, 150), 0))

        # Dispenser on Area
        area = CoordinateSampler(area_shape='gaussian',
                                 center=(x_area, 200),
                                 std=30,
                                 radius=60)
        dispenser = Dispenser(
            element_produced=Poison,
            element_produced_params={'reward': -5},
            production_area=area,
        )
        self.add_element(dispenser, ((x_dispenser, 200), 0))

        # Dispenser on Area
        area = CoordinateSampler(area_shape='gaussian',
                                 center=(x_area, 250),
                                 std=40,
                                 radius=60,
                                 min_radius=20)
        dispenser = Dispenser(
            element_produced=Candy,
            element_produced_params={'reward': 5},
            production_area=area,
        )
        self.add_element(dispenser, ((x_dispenser, 250), 0))

        # Dispenser on Dispenser

        dispenser = Dispenser(element_produced=Candy,
                              element_produced_params={'reward': 5},
                              production_range=20,
                              graspable=True)
        self.add_element(dispenser, ((x_dispenser, 300), 0))

        # Dispenser on Element

        pentagon_01 = Physical(config_key='pentagon',
                               radius=15,
                               graspable=True,
                               mass=5)
        self.add_element(pentagon_01, ((x_area, 350), math.pi / 2))

        dispenser = Dispenser(element_produced=Candy,
                              element_produced_params={'reward': 5},
                              production_range=20,
                              production_area=pentagon_01)
        self.add_element(dispenser, ((x_dispenser, 350), 0))
예제 #15
0
    def __init__(self, size=(400, 400), **playground_params):

        super().__init__(size=size, **playground_params)

        vertices = [
            (20, 20),
            (15, 30),
            (-5, 20),
            (-10, -20),
            (0, -20),

        ]

        # First line is classic polygons

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))

        poly_01 = Physical(physical_shape='polygon', vertices=vertices, texture=texture)
        self.add_element(poly_01,
                         initial_coordinates=((100, 100), 0))

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))
        poly_02 = Physical(physical_shape='polygon', vertices=vertices, texture=texture)
        self.add_element(poly_02,
                         initial_coordinates=((200, 100), math.pi/4))

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))
        poly_03 = Physical(physical_shape='polygon', vertices=vertices, texture=texture)
        self.add_element(poly_03,
                         initial_coordinates=((300, 100), math.pi/2))

        # Check that it works when polys are offset
        vertices = [(x+20, y+20) for (x,y) in vertices]

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))

        poly_04 = Physical(physical_shape='polygon', vertices=vertices, texture=texture)
        self.add_element(poly_04,
                         initial_coordinates=((100, 200), 0))

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))
        poly_05 = Physical(physical_shape='polygon', vertices=vertices, texture=texture)
        self.add_element(poly_05,
                         initial_coordinates=((200, 200), math.pi / 4))

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))
        poly_06 = Physical(physical_shape='polygon', vertices=vertices, texture=texture)
        self.add_element(poly_06,
                         initial_coordinates=((300, 200), math.pi / 2))

        # Check that it works when polys are movable.

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))

        poly_07 = Physical(physical_shape='polygon', vertices=vertices, texture=texture, mass=5, movable=True)
        self.add_element(poly_07,
                         initial_coordinates=((100, 300), 0))

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))
        poly_08 = Physical(physical_shape='polygon', vertices=vertices, texture=texture, mass=5, movable=True)
        self.add_element(poly_08,
                         initial_coordinates=((200, 300), math.pi / 4))

        texture = RandomTilesTexture(size_tiles=4,
                                     color_min=(50, 100, 150),
                                     color_max=(100, 150, 200),
                                     rng=default_rng(10))
        poly_09 = Physical(physical_shape='polygon', vertices=vertices, texture=texture, mass=5, movable=True)
        self.add_element(poly_09,
                         initial_coordinates=((300, 300), math.pi / 2))