def test_ma_intersection_40_agent_reset_after_respawn(): def check_pos(vehicles): while vehicles: v_1 = vehicles[0] for v_2 in vehicles[1:]: v_1_pos = v_1.position v_2_pos = v_2.position assert norm( v_1_pos[0] - v_2_pos[0], v_1_pos[1] - v_2_pos[1] ) > v_1.WIDTH / 2 + v_2.WIDTH / 2, "Vehicles overlap after reset()" assert not v_1.crash_vehicle, "Vehicles overlap after reset()" vehicles.remove(v_1) env = MultiAgentIntersectionEnv({ "horizon": 50, "num_agents": 40, "use_render": False }) try: _check_spaces_before_reset(env) obs = env.reset() _check_spaces_after_reset(env, obs) assert env.observation_space.contains(obs) for step in range(50): env.reset() check_pos(list(env.vehicles.values())) for v_id in list(env.vehicles.keys())[:20]: env.agent_manager.finish(v_id) env.step({k: [1, 1] for k in env.vehicles.keys()}) env.step({k: [1, 1] for k in env.vehicles.keys()}) env.step({k: [1, 1] for k in env.vehicles.keys()}) finally: env.close()
def test_ma_no_reset_error(): # It is possible that many agents are populated in the same spawn place! def check_pos(vehicles): while vehicles: v_1 = vehicles[0] for v_2 in vehicles[1:]: v_1_pos = v_1.position v_2_pos = v_2.position assert norm( v_1_pos[0] - v_2_pos[0], v_1_pos[1] - v_2_pos[1] ) > v_1.WIDTH / 2 + v_2.WIDTH / 2, "Vehicles overlap after reset()" if v_1.crash_vehicle: x = 1 raise ValueError("Vehicles overlap after reset()") vehicles.remove(v_1) env = MultiAgentIntersectionEnv({ "horizon": 300, "num_agents": 40, "delay_done": 0, "use_render": False }) try: _check_spaces_before_reset(env) obs = env.reset() _check_spaces_after_reset(env, obs) assert env.observation_space.contains(obs) for step in range(50): check_pos(list(env.vehicles.values())) o, r, d, i = env.step({k: [0, 1] for k in env.vehicles.keys()}) env.reset() if d["__all__"]: break finally: env.close()
def test_ma_intersection_close_spawn(): def _no_close_spawn(vehicles): vehicles = list(vehicles.values()) for c1, v1 in enumerate(vehicles): for c2 in range(c1 + 1, len(vehicles)): v2 = vehicles[c2] dis = norm(v1.position[0] - v2.position[0], v1.position[1] - v2.position[1]) assert distance_greater(v1.position, v2.position, length=2.2) MultiAgentIntersectionEnv._DEBUG_RANDOM_SEED = 1 env = MultiAgentIntersectionEnv({ # "use_render": True, "fast": True, "horizon": 50, "num_agents": 16, "map_config": { "exit_length": 30 } }) env.seed(100) try: _check_spaces_before_reset(env) for num_r in range(10): obs = env.reset() _check_spaces_after_reset(env) for _ in range(10): o, r, d, i = env.step({k: [0, 0] for k in env.vehicles.keys()}) assert not any(d.values()) _no_close_spawn(env.vehicles) print('Finish {} resets.'.format(num_r)) finally: env.close() MultiAgentIntersectionEnv._DEBUG_RANDOM_SEED = None
def test_randomize_spawn_place(): last_pos = {} env = MultiAgentIntersectionEnv({"num_agents": 4, "use_render": False, "fast": True}) try: obs = env.reset() for step in range(1000): act = {k: [1, 1] for k in env.vehicles.keys()} last_pos = {kkk: v.position for kkk, v in env.vehicles.items()} o, r, d, i = env.step(act) obs = env.reset() new_pos = {kkk: v.position for kkk, v in env.vehicles.items()} for kkk, new_p in new_pos.items(): assert not np.all(new_p == last_pos[kkk]), (new_p, last_pos[kkk], kkk) finally: env.close()