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
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()
def test_equip_communication(comm_radius): playground = SingleRoom(size=(300, 200)) agent_1 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_2 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) comm_1 = CommunicationDevice(agent_1.base_platform, comm_radius) agent_1.add_communication(comm_1) playground.add_agent(agent_1, ((100, 100), 0)) playground.add_agent(agent_2, ((200, 100), 0)) assert agent_1.communication assert not agent_2.communication
def test_multiagents_no_overlapping(pg_test_class): playground = pg_test_class() print('Starting Multiagent testing of ', pg_test_class.__name__) for _ in range(4): agent = BaseAgent(controller=RandomContinuous(), interactive=True) playground.add_agent(agent, allow_overlapping=False) assert len(playground.agents) == 4 engine = Engine(playground, time_limit=100) engine.run()
def test_multiagents(pg_test_class): playground = pg_test_class() print('Starting Multiagent testing of ', pg_test_class.__name__) pos_area_sampler = playground.initial_agent_coordinates for _ in range(100): agent = BaseAgent(controller=RandomContinuous(), interactive=True) playground.add_agent(agent, pos_area_sampler) assert len(playground.agents) == 100 engine = Engine(playground, time_limit=100) engine.run() engine.terminate()
def test_disable_communication_receiver(): playground = SingleRoom(size=(300, 200)) agent_1 = BaseAgent( controller=External(), interactive=False, rotate=False, lateral=False ) agent_2 = BaseAgent( controller=External(), interactive=False, rotate=False, lateral=False ) comm_1 = CommunicationDevice(agent_1.base_platform) agent_1.add_communication(comm_1) comm_2 = CommunicationDevice(agent_2.base_platform) agent_2.add_communication(comm_2) playground.add_agent(agent_1, ((100, 100), 0)) playground.add_agent(agent_2, ((200, 100), 0)) disabler = CommunicationDisabler() playground.add_element(disabler, ((200, 100), 0)) assert agent_1.communication assert agent_2.communication engine = Engine(playground) messages = [(comm_1, 'test', comm_2)] engine.step(messages=messages) assert not comm_1._disabled assert comm_2._disabled assert comm_2.received_message == []
def test_transmission_range(range_1, range_2, distance, in_range): playground = SingleRoom(size=(300, 200)) agent_1 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_2 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) comm_1 = CommunicationDevice(agent_1.base_platform, range_1) agent_1.add_communication(comm_1) comm_2 = CommunicationDevice(agent_2.base_platform, range_2) agent_2.add_communication(comm_2) playground.add_agent(agent_1, ((100, 100), 0)) playground.add_agent(agent_2, ((100 + distance, 100), 0)) assert agent_1.communication assert agent_2.communication assert comm_1.in_transmission_range(comm_2) is in_range assert comm_2.in_transmission_range(comm_1) is in_range engine = Engine(playground) messages = [(comm_1, 'test', comm_2)] engine.step(messages=messages) if in_range: assert comm_2.received_message == [(comm_1, 'test')] else: assert comm_2.received_message == []
def test_list_sensors(): playground = SingleRoom(size=(300, 200)) agent_1 = BaseAgent( controller=External(), interactive=False, rotate=False, lateral=False ) sensor_1 = RgbCamera(anchor=agent_1.base_platform, invisible_elements=agent_1.parts, range=400) sensor_2 = Lidar(anchor=agent_1.base_platform, invisible_elements=agent_1.parts, range=400) sensor_3 = SemanticRay(anchor=agent_1.base_platform, invisible_elements=agent_1.parts, range=400) agent_1.add_sensor(sensor_1) agent_1.add_sensor(sensor_2) agent_1.add_sensor(sensor_3) playground.add_agent(agent_1, ((100, 100), 0)) disabler = SensorDisabler(disabled_sensor_types=[RgbCamera, SemanticRay]) playground.add_element(disabler, ((100, 100), 0)) engine = Engine(playground) engine.step() engine.update_observations() for sensor in [sensor_1, sensor_2, sensor_3]: if isinstance(sensor, RgbCamera): assert sensor._disabled assert np.all(sensor.sensor_values == sensor._get_null_sensor()) elif isinstance(sensor, SemanticRay): assert sensor._disabled assert sensor.sensor_values == sensor._get_null_sensor() else: assert not sensor._disabled if isinstance(sensor.sensor_values, np.ndarray): assert not np.all(sensor.sensor_values == sensor._get_null_sensor()) else: assert not sensor.sensor_values == sensor._get_null_sensor()
def test_moving_element(basic_element): playground = SingleRoom(size=(200, 200)) agent = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) actions = {agent: {agent.longitudinal_force: 1.}} playground.add_agent(agent, ((50, 100), 0)) playground.add_element(basic_element, ((100, 100), 0)) engine = Engine(playground, time_limit=100) while engine.game_on: engine.step(actions) if basic_element.movable: assert agent.position[0] > 100 assert basic_element.position[0] > 100 else: assert basic_element.position[0] == 100
def test_normalize_position_sensor(): agent = BaseAgent(controller=External(), rotate=True) pos = Position(anchor=agent.base_platform, normalize=False) pos_norm = Position(anchor=agent.base_platform, normalize=True) agent.add_sensor(pos) agent.add_sensor(pos_norm) playground = SingleRoom((400, 400)) playground.add_agent(agent, initial_coordinates=((100, 200), math.pi)) engine = Engine(playground, time_limit=1000) engine.update_observations() assert np.all(pos.sensor_values == np.asarray((100, 200, math.pi))) assert np.all(pos_norm.sensor_values == np.asarray((0.25, 0.5, 0.5)))
def run_drones(pg_width, n_agents): n_elem_per_dim = int(pg_width / 100) size_elem = 20 fps = 0 for run in range(N_RUNS): pg = BasicUnmovable(size=(pg_width, pg_width), size_elements=size_elem, n_elements_per_dim=n_elem_per_dim) for agent in range(n_agents): my_agent = BaseAgent(controller=RandomContinuous(), lateral=False, rotate=True, interactive=False) sem_cones = SemanticCones(anchor=my_agent.base_platform, normalize=False, n_cones=36, rays_per_cone=4, max_range=200, fov=360) my_agent.add_sensor(sem_cones) lidar = Lidar(anchor= my_agent.base_platform, normalize=False, resolution=60, max_range=300, fov=180, ) my_agent.add_sensor(lidar) touch = Touch(anchor= my_agent.base_platform, normalize=True, fov=360, max_range=5, resolution=36) my_agent.add_sensor(touch) pos = Position(anchor=my_agent.base_platform) my_agent.add_sensor(pos) pg.add_agent(my_agent) engine_ = Engine(playground=pg, time_limit=RUN_DURATION) t_start = time.time() while engine_.game_on: actions = {} for agent in pg.agents: actions[agent] = agent.controller.generate_actions() engine_.step(actions=actions) engine_.update_observations() t_end = time.time() fps += (RUN_DURATION/ (t_end - t_start)) / N_RUNS return fps
def test_directed_broadcast(): playground = SingleRoom(size=(300, 200)) agent_1 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_2 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_3 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_4 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) comm_1 = CommunicationDevice(agent_1.base_platform, receiver_capacity=1) agent_1.add_communication(comm_1) comm_2 = CommunicationDevice(agent_2.base_platform, receiver_capacity=2) agent_2.add_communication(comm_2) comm_3 = CommunicationDevice(agent_3.base_platform, receiver_capacity=3) agent_3.add_communication(comm_3) comm_4 = CommunicationDevice(agent_4.base_platform, receiver_capacity=4) agent_4.add_communication(comm_4) playground.add_agent(agent_1, ((100, 100), 0)) playground.add_agent(agent_2, ((180, 100), 0)) playground.add_agent(agent_3, ((200, 120), 0)) playground.add_agent(agent_4, ((100, 200), 0)) engine = Engine(playground) # Directed message msg_to_single_agent = [(comm_1, 'test', comm_2)] engine.step(messages=msg_to_single_agent) assert comm_1.received_message == [] assert comm_2.received_message == [(comm_1, 'test')] assert comm_3.received_message == [] assert comm_4.received_message == [] # No message, verify receivers are empty engine.step() assert comm_1.received_message == [] assert comm_2.received_message == [] assert comm_3.received_message == [] assert comm_4.received_message == [] # Broadcast message msg_to_all_agents = [(comm_1, 'test', None)] engine.step(messages=msg_to_all_agents) assert comm_1.received_message == [] assert comm_2.received_message == [(comm_1, 'test')] assert comm_3.received_message == [(comm_1, 'test')] assert comm_4.received_message == [(comm_1, 'test')]
def test_capacity(): playground = SingleRoom(size=(300, 200)) agent_1 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_2 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_3 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) agent_4 = BaseAgent(controller=External(), interactive=False, rotate=False, lateral=False) comm_1 = CommunicationDevice(agent_1.base_platform, receiver_capacity=1) agent_1.add_communication(comm_1) comm_2 = CommunicationDevice(agent_2.base_platform, receiver_capacity=2) agent_2.add_communication(comm_2) comm_3 = CommunicationDevice(agent_3.base_platform, receiver_capacity=3) agent_3.add_communication(comm_3) comm_4 = CommunicationDevice(agent_4.base_platform, receiver_capacity=2) agent_4.add_communication(comm_4) playground.add_agent(agent_1, ((100, 100), 0)) playground.add_agent(agent_2, ((180, 100), 0)) playground.add_agent(agent_3, ((200, 120), 0)) playground.add_agent(agent_4, ((100, 200), 0)) engine = Engine(playground) # Broadcast message msg_to_all_agents = [ (comm_1, 'test_1', None), (comm_2, 'test_2', None), (comm_3, 'test_3', None), (comm_4, 'test_4', None), ] engine.step(messages=msg_to_all_agents) # Assert correct message length assert len(comm_1.received_message) == 1 assert len(comm_2.received_message) == 2 assert len(comm_3.received_message) == 3 assert len(comm_4.received_message) == 2 # Assert priority is given to comm that are closer assert comm_1.received_message == [(comm_2, 'test_2')] assert comm_2.received_message == [(comm_3, 'test_3'), (comm_1, 'test_1')] assert comm_3.received_message == [(comm_2, 'test_2'), (comm_1, 'test_1'), (comm_4, 'test_4')]
def base_forward_interactive_agent_external(): agent = BaseAgent( controller=External(), interactive=True, ) return agent
def base_forward_agent_random(): agent = BaseAgent( controller=RandomDiscrete(), interactive=False, ) return agent