def _assign_areas(self): list_coord = [(50, 50), (50, 150), (150, 150), (150, 50)] random.shuffle(list_coord) # Starting area of the agent area_start_center = list_coord.pop() area_start = CoordinateSampler(center=area_start_center, area_shape='rectangle', width_length=[80, 80]) agent_starting_area = area_start # invisible endzone at one corner of the game vm_center = list_coord.pop() prod_center = list_coord.pop() area_prod = CoordinateSampler(center=prod_center, area_shape='rectangle', width_length=[80, 80]) area_vm = CoordinateSampler(center=vm_center, area_shape='rectangle', width_length=[80, 80]) return agent_starting_area, area_prod, area_vm
def __init__(self, size=(200, 200), **playground_params): super().__init__(size=size, **playground_params) endgoal_01 = VisibleEndGoal(reward=50) self.add_scene_element(endgoal_01, ((20, 180), 0)) deathtrap_01 = VisibleDeathTrap() self.add_scene_element(deathtrap_01, ((180, 180), 0)) poison = Poison() self.add_scene_element(poison, ((15, 15), 0)) poison_area = CoordinateSampler(area_shape='rectangle', center=[100, 150], width_length=[20, 20]) for _ in range(5): poison = Poison() self.add_scene_element(poison, poison_area) candy_area = CoordinateSampler(area_shape='rectangle', center=[50, 100], width_length=[20, 20]) for _ in range(5): candy = Candy() self.add_scene_element(candy, candy_area) # outside on purpose outside_area = CoordinateSampler(area_shape='rectangle', center=[200, 100], width_length=[50, 50]) for _ in range(8): candy = Candy() self.add_scene_element(candy, outside_area)
def _assign_areas(self): list_room_coordinates = [ room_coord for room_coord, _ in self.area_rooms.items() ] random.shuffle(list_room_coordinates) # Starting area of the agent area_start_center, area_start_shape = self.area_rooms[ list_room_coordinates.pop()] area_start = CoordinateSampler(center=area_start_center, area_shape='rectangle', width_length=area_start_shape) agent_starting_area = area_start # invisible endzone at one corner of the game dispenser_center, dispenser_shape = self.area_rooms[ list_room_coordinates.pop()] prod_center, prod_shape = self.area_rooms[list_room_coordinates.pop()] area_prod = CoordinateSampler(center=prod_center, area_shape='rectangle', width_length=prod_shape) area_dispenser = CoordinateSampler(center=dispenser_center, area_shape='rectangle', width_length=dispenser_shape) return agent_starting_area, area_prod, area_dispenser
def __init__(self): super().__init__(size=(200, 200)) # Starting area of the agent area_center, _ = self.area_rooms[(0, 0)] area_start = CoordinateSampler(center=area_center, area_shape='rectangle', width_length=(100, 100)) self.agent_starting_area = area_start additional_types = set() for loc in ["down-left", "up-right"]: area_center, size_area = self.get_area((0, 0), loc) area = CoordinateSampler(center=area_center, area_shape='rectangle', width_length=size_area) field = scene_elements.Field(entity_produced=scene_elements.Candy, production_area=area, probability=0.01, limit=2) self.add_scene_element(field) additional_types.add(field.entity_produced) field = scene_elements.Field(entity_produced=scene_elements.Poison, production_area=area, probability=0.01, limit=2) self.add_scene_element(field) additional_types.add(field.entity_produced) self.time_limit = 2000 self.time_limit_reached_reward = -1 self.create_entity_types_map(additional_types)
def __init__(self, size=(300, 300), **playground_params): super().__init__(size=size, **playground_params) goal_1 = GoalZone() self.add_scene_element(goal_1, [(20, 20), 0]) apple = Apple(physical_shape='pentagon', graspable=True) self.add_scene_element(apple, [(100, 50), 0]) rotten = RottenApple() self.add_scene_element(rotten, [(100, 100), 0]) area_1 = CoordinateSampler(area_shape='rectangle', center=[200, 150], width_length=[20, 50]) dispenser_1 = Dispenser(entity_produced=Poison, production_area=area_1) self.add_scene_element(dispenser_1, [(150, 150), 0]) area_2 = CoordinateSampler(area_shape='gaussian', center=[150, 50], variance=300, radius=60) dispenser_2 = Dispenser(entity_produced=Candy, production_area=area_2) self.add_scene_element(dispenser_2, [(100, 150), 0]) dispenser_3 = Dispenser(entity_produced=Candy, entity_produced_params={ 'radius': 3, 'reward': 42 }, movable=True, mass=5) self.add_scene_element(dispenser_3, [(200, 150), 0]) key_chest = Key(default_config_key='pentagon', radius=7, graspable=True, mass=10) self.add_scene_element(key_chest, [(50, 200), 0]) treasure = Apple() chest = Chest(key=key_chest, treasure=treasure, width_length=[20, 50]) self.add_scene_element(chest, [(100, 200), 0.2]) vending = VendingMachine() self.add_scene_element(vending, [(200, 200), 0]) coin = Coin(graspable=True) self.add_scene_element(coin, [(150, 200), 0]) vending.accepted_coins.append(coin) coin = Coin(graspable=True) self.add_scene_element(coin, [(150, 220), 0]) vending.accepted_coins.append(coin) coin = Coin(graspable=True) self.add_scene_element(coin, [(150, 240), 0]) vending.accepted_coins.append(coin)
def __init__(self, size=(200, 200), **playground_params): super().__init__(size=size, **playground_params) area_1 = CoordinateSampler(area_shape='rectangle', center=[70, 70], width_length=[30, 100]) field = Field(Poison, production_area=area_1) self.add_scene_element(field) area_2 = CoordinateSampler(area_shape='rectangle', center=[200, 70], width_length=[50, 50]) field = Field(Candy, production_area=area_2) self.add_scene_element(field)
def __init__(self, time_limit=100, probability_production=0.4): super().__init__(size=(200, 200)) fireball_texture = { 'texture_type': 'centered_random_tiles', 'size_tiles': 4 } additional_types = set() # First Fireball text_1 = {'color_min': [220, 0, 200], 'color_max': [255, 100, 220]} trajectory = Trajectory('waypoints', trajectory_duration=300, waypoints=[[20, 20], [20, 180], [180, 180], [180, 20]]) fireball = scene_elements.Fireball(texture={ **fireball_texture, **text_1 }) self.add_scene_element(fireball, trajectory) additional_types.add(type(fireball)) # Second Fireball text_2 = {'color_min': [180, 0, 0], 'color_max': [220, 100, 0]} trajectory = Trajectory('waypoints', trajectory_duration=150, waypoints=[[40, 40], [160, 160]]) fireball = scene_elements.Fireball(texture={ **fireball_texture, **text_2 }) self.add_scene_element(fireball, trajectory) # Third Fireball text_3 = {'color_min': [220, 100, 0], 'color_max': [255, 120, 0]} trajectory = Trajectory('waypoints', trajectory_duration=180, waypoints=[[40, 160], [160, 40]]) fireball = scene_elements.Fireball(texture={ **fireball_texture, **text_3 }) self.add_scene_element(fireball, trajectory) # Foraging area_prod = CoordinateSampler(center=(100, 100), area_shape='rectangle', width_length=(150, 150)) field = scene_elements.Field(scene_elements.Candy, production_area=area_prod, probability=probability_production) self.add_scene_element(field) additional_types.add(field.entity_produced) self.time_limit = time_limit self.create_entity_types_map(additional_types)
def test_add_remove_agent_in_area(base_forward_agent): playground_1 = SingleRoom((800, 300)) agent = base_forward_agent areas = {'up': [0, 800, 0, 150], 'down': [0, 800, 150, 300], 'right': [400, 800, 0, 300], 'left': [0, 400, 0, 300], 'up-right': [400, 800, 0, 150], 'up-left': [0, 400, 0, 150], 'down-right': [400, 800, 150, 300], 'down-left': [0, 400, 150, 300], } for area_name, coord in areas.items(): location, size = playground_1.get_area((0, 0), area_name) pos_area_sampler = CoordinateSampler(location, area_shape='rectangle', width_length=size) playground_1.add_agent(agent, pos_area_sampler) min_x, max_x, min_y, max_y = coord assert min_x < agent.position[0] < max_x assert min_y < agent.position[1] < max_y playground_1.remove_agent(agent)
def _agent_teleports(self, arbiter, space, data): agent = self._get_agent_from_shape(arbiter.shapes[0]) teleport = self._get_scene_element_from_shape(arbiter.shapes[1]) if teleport is None or teleport.target is None or ( agent, teleport) in self._teleported: return True if agent.is_teleporting: return True if teleport.target.traversable: agent.position = teleport.target.position else: area_shape = teleport.target.physical_shape if area_shape == 'rectangle': width = teleport.target.width + agent.base_platform.radius * 2 + 1 length = teleport.target.length + agent.base_platform.radius * 2 + 1 angle = teleport.target.angle sampler = CoordinateSampler( center=teleport.target.position, area_shape=area_shape, angle=angle, width_length=[width + 2, length + 2], excl_width_length=[width, length], ) else: radius = teleport.target.radius + agent.base_platform.radius + 1 sampler = CoordinateSampler( center=teleport.target.position, area_shape='circle', radius=radius, excl_radius=radius, ) agent.coordinates = sampler.sample() if (agent, teleport.target) not in self._teleported: self._teleported.append((agent, teleport.target)) agent.is_teleporting = True return True
def __init__(self, entity_produced, entity_produced_params=None, production_area=None, **kwargs): """ Default: pink circle of radius 15. Args: entity_produced: Class of the entity produced by the dispenser. entity_produced_params: Dictionary of additional parameters for the entity_produced. production_area: PositionAreaSampler. If no production_area has been set, the entities will be produced around the dispenser. **kwargs: other params to configure entity. Refer to Entity class. Keyword Args: production_limit: maximum number of entities produced. Default: 15. """ default_config = parse_configuration('element_interactive', self.entity_type) entity_params = {**default_config, **kwargs} super().__init__(**entity_params) self.pm_interaction_shape.collision_type = CollisionTypes.INTERACTIVE self.entity_produced = entity_produced if entity_produced_params is None: self.entity_produced_params = {} else: self.entity_produced_params = entity_produced_params if production_area is None: self.local_dispenser = True self.location_sampler = CoordinateSampler(area_shape='circle', center=[0, 0], radius=self.radius + 10) else: self.local_dispenser = False self.location_sampler = production_area self.production_limit = entity_params['production_limit'] self.produced_entities = []
def test_agent_initial_position3(base_forward_agent): agent = base_forward_agent # Setting initial position in playground as PositionAreaSampler playground = SingleRoom((300, 300)) center, shape = playground.area_rooms[(0, 0)] playground.initial_agent_coordinates = CoordinateSampler( center, area_shape='rectangle', width_length=shape) playground.add_agent(agent) pos_1 = agent.position playground.remove_agent(agent) playground.add_agent(agent) assert agent.position != pos_1 playground.remove_agent(agent)
def __init__( self, time_limit=1000, reward_reached_time_limit=-10, reward_reached_endgoal=10, reward_reached_deathtrap=-10, wall_texture_seed=None, ): super().__init__(size=(200, 200), wall_texture_seed=wall_texture_seed) # Starting area of the agent area_center, _ = self.area_rooms[(0, 0)] area_start = CoordinateSampler(center=area_center, area_shape='rectangle', width_length=(100, 100)) self.initial_agent_coordinates = area_start # Visual Cues for the agent to orient itself. obstacle_1 = Basic(default_config_key='pentagon', radius=9) self.add_scene_element(obstacle_1, ((60, 30), 0.34)) obstacle_2 = Basic(default_config_key='rectangle', width_length=[8, 12]) self.add_scene_element(obstacle_2, ((130, 150), 1.7)) obstacle_3 = Basic(default_config_key='square', radius=8) self.add_scene_element(obstacle_3, ((40, 140), 0.4)) obstacle_4 = Basic(physical_shape='triangle', radius=14, texture=(150, 200, 200)) self.add_scene_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 self.create_entity_types_map([Basic])
def __init__(self): super().__init__(size=(600, 600), room_layout=(3, 3), wall_type='colorful') # Starting area of the agent area_start = CoordinateSampler(center=(300, 300), area_shape='rectangle', width_length=(600, 600)) self.agent_starting_area = area_start # invisible endzone at one corner of the game invisible_endzone = GoalZone(reward=10) self.add_scene_element(invisible_endzone, ((20, 20), 0)) self.time_limit = 3000
def __init__(self, size=(400, 200), room_layout=(3, 2), wall_type='classic', **kwargs): self.width, self.length = size default_config = parse_configuration('playground', 'connected-rooms-2d') playground_params = {**default_config, **kwargs} # Wall parameters self._wall_texture = kwargs.get( 'wall_texture', parse_configuration('playground', wall_type)) self._wall_params = { **parse_configuration('playground', 'wall'), **kwargs.get('wall_params', {}), 'texture': self._wall_texture } self._wall_depth = self._wall_params['depth'] # Door parameters self._doorstep_type = playground_params['doorstep_type'] self._doorstep_size = playground_params['doorstep_size'] self._size_door = [self._wall_depth, self._doorstep_size] # Environment Layout self.room_layout = room_layout self._width_room = float(self.width) / self.room_layout[0] self._length_room = float(self.length) / self.room_layout[1] self.area_rooms = self._compute_area_rooms() self.doorsteps = self._compute_doorsteps() super().__init__(size=size) self._add_external_walls() self._add_room_walls() # By default, an agent starts in a random position of the first room center, shape = self.area_rooms[(0, 0)] shape = shape[0] - self._wall_depth, shape[1] - self._wall_depth self.initial_agent_coordinates = CoordinateSampler( center=center, area_shape='rectangle', width_length=shape)
def test_multiagents_no_overlapping(base_forward_agent): for pg_name, pg_class in PlaygroundRegister.playgrounds['test'].items(): playground = pg_class() print('Starting Multiagent testing of ', pg_class.__name__) center, shape = playground.area_rooms[(0, 0)] pos_area_sampler = CoordinateSampler(center=center, area_shape='rectangle', width_length=shape) for _ in range(2): agent = BaseAgent(controller=RandomContinuous(), interactive=True) playground.add_agent(agent, pos_area_sampler) assert len(playground.agents) == 2 engine = Engine(playground, time_limit=100, screen=False) engine.run(update_screen=False)
def __init__(self, time_limit=1000, wall_texture_seed=None): super().__init__(size=(450, 450), room_layout=(3, 3), wall_type='colorful', wall_texture_seed=wall_texture_seed) # Starting area of the agent area_start = CoordinateSampler(center=(225, 225), area_shape='rectangle', width_length=(450, 450)) self.initial_agent_coordinates = area_start # invisible endzone at one corner of the game invisible_endzone = GoalZone(reward=10) self.add_scene_element(invisible_endzone, ((20, 20), 0)) self.time_limit = time_limit self.create_entity_types_map()
def __init__(self): super().__init__(size=(200, 200)) # Starting area of the agent area_center, _ = self.area_rooms[(0, 0)] area_start = CoordinateSampler(center=area_center, area_shape='rectangle', width_length=(100, 100)) self.agent_starting_area = area_start # Obstacles obstacle_1 = Basic(default_config_key='pentagon', radius=9) self.add_scene_element(obstacle_1, ((60, 30), 0.34)) obstacle_2 = Basic(default_config_key='rectangle', width_length=[8, 12]) self.add_scene_element(obstacle_2, ((130, 150), 1.7)) obstacle_3 = Basic(default_config_key='square', radius=8) self.add_scene_element(obstacle_3, ((40, 140), 0.4)) obstacle_4 = Basic(physical_shape='triangle', radius=14, texture=(150, 200, 200)) self.add_scene_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._set_goal() self.time_limit = 2000 self.time_limit_reached_reward = -1
def __init__( self, time_limit=1000, wall_texture_seed=None, ): super().__init__(size=(450, 150), room_layout=(3, 1), doorstep_size=60, wall_type='colorful', wall_texture_seed=wall_texture_seed) self.time_limit = time_limit door = self.add_door(((1, 0), (2, 0))) area_key_center, area_key_shape = self.area_rooms[(0, 0)] area_key = CoordinateSampler(center=area_key_center, area_shape='rectangle', width_length=area_key_shape) key = scene_elements.Key(graspable=True) self.add_scene_element(key, area_key) lock = scene_elements.Lock(door=door, key=key) lock_position = self.random_position_on_wall((1, 0), 'right', lock.radius) self.add_scene_element(lock, lock_position) vm_center, vm_shape = self.get_area((2, 0), 'up-right') area_vm = CoordinateSampler(center=vm_center, area_shape='rectangle', width_length=vm_shape) vm = scene_elements.VendingMachine() self.add_scene_element(vm, area_vm, allow_overlapping=False) dispenser_center, dispenser_shape = self.get_area((2, 0), 'down-right') area_dispenser = CoordinateSampler(center=dispenser_center, area_shape='rectangle', width_length=dispenser_shape) prod_center, prod_shape = self.area_rooms[(0, 0)] area_prod = CoordinateSampler(center=prod_center, area_shape='rectangle', width_length=prod_shape) dispenser = scene_elements.Dispenser(scene_elements.Coin, production_area=area_prod, entity_produced_params={ 'graspable': True, 'vending_machine': vm }) self.add_scene_element(dispenser, area_dispenser, allow_overlapping=False) area_start_center, area_start_shape = self.area_rooms[(1, 0)] area_start = CoordinateSampler(center=area_start_center, area_shape='rectangle', width_length=area_start_shape) self.initial_agent_coordinates = area_start self.create_entity_types_map([dispenser.entity_produced])
class Dispenser(InteractiveSceneElement): """Dispenser produces a new entity in an area of the playground when activated. """ entity_type = SceneElementTypes.DISPENSER interactive = True def __init__(self, entity_produced, entity_produced_params=None, production_area=None, **kwargs): """ Default: pink circle of radius 15. Args: entity_produced: Class of the entity produced by the dispenser. entity_produced_params: Dictionary of additional parameters for the entity_produced. production_area: PositionAreaSampler. If no production_area has been set, the entities will be produced around the dispenser. **kwargs: other params to configure entity. Refer to Entity class. Keyword Args: production_limit: maximum number of entities produced. Default: 15. """ default_config = parse_configuration('element_interactive', self.entity_type) entity_params = {**default_config, **kwargs} super().__init__(**entity_params) self.pm_interaction_shape.collision_type = CollisionTypes.INTERACTIVE self.entity_produced = entity_produced if entity_produced_params is None: self.entity_produced_params = {} else: self.entity_produced_params = entity_produced_params if production_area is None: self.local_dispenser = True self.location_sampler = CoordinateSampler(area_shape='circle', center=[0, 0], radius=self.radius + 10) else: self.local_dispenser = False self.location_sampler = production_area self.production_limit = entity_params['production_limit'] self.produced_entities = [] @property def reward(self): return 0 def activate(self, _): elem_add = None if len(self.produced_entities ) < self.production_limit and self.activated is False: if self.local_dispenser: initial_coordinate = self.location_sampler.sample( self.position) else: initial_coordinate = self.location_sampler.sample() obj = self.entity_produced(is_temporary_entity=True, **self.entity_produced_params) self.produced_entities.append(obj) elem_add = (obj, initial_coordinate) self.activated = True return None, elem_add def reset(self): self.produced_entities = [] super().reset()