Пример #1
0
def test_ma_roundabout_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 = MultiAgentRoundaboutEnv({"horizon": 50, "num_agents": 40})
    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()
Пример #2
0
def test_infinite_agents():
    env = MultiAgentRoundaboutEnv(
        {
            "map_config": dict(exit_length=20, lane_num=2),
            # "use_render": True, "fast": True,
            "num_agents": -1,
            "delay_done": 50,
            "horizon": 50,
            "debug": True,
            "random_traffic": False
        }
    )
    try:
        o = env.reset()
        env.seed(100)
        env._DEBUG_RANDOM_SEED = 100
        max_num = old_num_of_vehicles = len(env.vehicles)
        for i in range(1, 1000):
            o, r, d, info = env.step({k: [1, 1] for k in env.vehicles})
            print(
                "{} Current active agents: ".format(i), len(env.vehicles), ". Objects: ",
                len(env.agent_manager._object_to_agent)
            )
            max_num = max(len(env.vehicles), max_num)
            # env.render(mode="top_down")
            for kkk, iii in info.items():
                if d[kkk]:
                    assert iii["episode_length"] > 1
            if d["__all__"]:
                o = env.reset()
                print("Finish {} steps.".format(i))
    finally:
        env._DEBUG_RANDOM_SEED = None
        env.close()

    env = MultiAgentRoundaboutEnv({"num_agents": -1, "delay_done": 0, "horizon": 50, "debug": True})
    try:
        o = env.reset()
        max_num = old_num_of_vehicles = len(env.vehicles)
        for i in range(1, 300):
            o, r, d, info = env.step({k: [0, 1] for k in env.vehicles})
            # print("Current active agents: ", len(env.vehicles),
            #       ". Objects: ", len(env.agent_manager._object_to_agent))
            max_num = max(len(env.vehicles), max_num)
            # env.render(mode="top_down")
            for kkk, iii in info.items():
                if d[kkk]:
                    assert iii["episode_length"] > 1
            if d["__all__"]:
                o = env.reset()
                print("Finish {} steps.".format(i))
    finally:
        env.close()
    assert max_num > old_num_of_vehicles
Пример #3
0
def test_save_episode(vis=False):
    """
    1. Set record_episode=True to record each episode
    2. dump_episode when done[__all__] == True
    3. You can keep recent episodes
    4. Input episode data to reset() function can replay the episode !
    """

    setup_logger(True)

    test_dump = True
    dump_recent_episode = 5
    dump_count = 0
    env = MultiAgentRoundaboutEnv(
        dict(use_render=vis,
             manual_control=vis,
             record_episode=True,
             horizon=100))
    try:
        # Test Record
        o = env.reset()
        epi_info = None
        for i in range(1, 100000 if vis else 600):
            o, r, d, info = env.step(
                {agent_id: [0, .2]
                 for agent_id in env.vehicles.keys()})
            if vis:
                env.render()
            if d["__all__"]:
                epi_info = env.engine.dump_episode()
                # test dump json
                if test_dump:
                    with open("test_dump_{}.json".format(dump_count),
                              "w") as f:
                        json.dump(epi_info, f)
                    dump_count += 1
                    dump_count = dump_count % dump_recent_episode
                env.reset()

        # input episode_info to restore
        o = env.reset(epi_info)
        for i in range(1, 100000 if vis else 2000):
            o, r, d, info = env.step(
                {agent_id: [0, 0.1]
                 for agent_id in env.vehicles.keys()})
            if vis:
                env.render()
            if d["__all__"]:
                break
    finally:
        env.close()
Пример #4
0
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()"
            assert not v_1.crash_vehicle, "Vehicles overlap after reset()"
            vehicles.remove(v_1)

    env = MultiAgentRoundaboutEnv({"horizon": 300, "num_agents": 40, "delay_done": 0})
    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(300):
            check_pos(list(env.vehicles.values()))
            o, r, d, i = env.step({k: [0, 1] for k in env.vehicles.keys()})
            if d["__all__"]:
                break
    finally:
        env.close()
Пример #5
0
def test_ma_roundabout_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)

    MultiAgentRoundaboutEnv._DEBUG_RANDOM_SEED = 1
    env = MultiAgentRoundaboutEnv({"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()
        MultiAgentRoundaboutEnv._DEBUG_RANDOM_SEED = None
Пример #6
0
def test_randomize_spawn_place():
    last_pos = {}
    env = MultiAgentRoundaboutEnv({"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()
Пример #7
0
def test_delay_done():
    # Put agent 0 in the left, agent 1 in the right, and let agent 0 dead at first.
    # We wish to see agent 1 hits the dead body of agent 0.
    env = MultiAgentRoundaboutEnv({
        # "use_render": True,
        # "fast": True,
        "target_vehicle_configs": {
            "agent0": {
                "spawn_longitude": 12,
                "spawn_lateral": 0,
                "spawn_lane_index": (">", ">>", 0),
            },
            "agent1": {
                "spawn_longitude": 10,  # locate a little forward
                "spawn_lateral": 0,
                "spawn_lane_index": (">", ">>", 1),
            }
        },
        "num_agents": 2,
        "traffic_density": 0,
        "delay_done": 100,
        "horizon": 100
    })
    try:
        agent0_done = False
        agent1_already_hit = False
        o = env.reset()
        for i in range(1, 300):
            actions = {"agent0": [1, 1], "agent1": [1, 1]}
            if "agent0" not in env.vehicles:
                actions.pop("agent0")
            if "agent1" not in env.vehicles:
                actions.pop("agent1")
            o, r, d, info = env.step(actions)
            if agent0_done:
                assert "agent0" not in o
                assert "agent0" not in info
                assert "agent0" not in d
            if d.get("agent0"):
                agent0_done = True
            if agent0_done:
                if info["agent1"][TerminationState.CRASH_VEHICLE]:
                    agent1_already_hit = True
                    print("Hit!")
            if d["__all__"]:
                assert agent1_already_hit
                agent0_done = False
                agent1_already_hit = False
                env.reset()
    finally:
        env.close()

    env = MultiAgentRoundaboutEnv({
        "num_agents": 5,
        "delay_done": 10,
        "horizon": 100
    })
    try:
        env.reset()
        dead = set()
        for _ in range(300):
            o, r, d, i = env.step({k: [1, 1] for k in env.vehicles.keys()})
            for dead_name in dead:
                assert dead_name not in o
            print("{} there!".format(env.vehicles.keys()))
            print("{} dead!".format([kkk for kkk, ddd in d.items() if ddd]))
            for kkk, ddd in d.items():
                if ddd and kkk != "__all__":
                    dead.add(kkk)
            if d["__all__"]:
                env.reset()
                dead.clear()
    finally:
        env.close()
Пример #8
0
def test_respawn():
    out_of_road_cost = 5555
    out_of_road_penalty = 2222
    env = MultiAgentRoundaboutEnv({
        "num_agents": 2,
        "out_of_road_cost": out_of_road_cost,
        "out_of_road_penalty": out_of_road_penalty,
        "delay_done": 0,  # Since we are testing respawn!
        # "use_render": True, "fast": True
        "crash_done": False,
    })
    try:
        assert set(env.observations.keys()) == {"agent0", "agent1"}
        assert set(env.action_space.spaces.keys()) == {"agent0", "agent1"}
        assert set(env.config["target_vehicle_configs"].keys()) == {
            "agent0", "agent1"
        }
        assert set(env.vehicles.keys()) == set()  # Not initialized yet!

        o = env.reset()

        assert set(o.keys()) == {"agent0", "agent1"}
        assert set(env.observations.keys()) == {"agent0", "agent1"}
        assert set(env.action_space.spaces.keys()) == {"agent0", "agent1"}
        assert set(env.config["target_vehicle_configs"].keys()) == {
            "agent0", "agent1"
        }
        assert set(env.vehicles.keys()) == {"agent0", "agent1"}

        v_id_0 = "agent0"
        v_id_1 = "agent1"
        count = 2
        tracks = []
        done_count = 0
        for i in range(1, 1000):
            o, r, d, info = env.step({v_id_0: [-1, 1], v_id_1: [1, 1]})
            assert set(o.keys()) == set(r.keys()) == set(info.keys())
            assert set(o.keys()).union({"__all__"}) == set(d.keys())
            tracks.append(d)
            if d[v_id_0]:
                assert info[v_id_0][TerminationState.OUT_OF_ROAD]
                assert info[v_id_0]["cost"] == out_of_road_cost
                assert r[v_id_0] == -out_of_road_penalty
                v_id_0 = "agent{}".format(count)
                count += 1
                done_count += 1
            if d[v_id_1]:
                assert info[v_id_1][TerminationState.OUT_OF_ROAD]
                assert info[v_id_1]["cost"] == out_of_road_cost
                assert r[v_id_1] == -out_of_road_penalty
                v_id_1 = "agent{}".format(count)
                count += 1
                done_count += 1
            if all(d.values()):
                raise ValueError()
            if i % 100 == 0:  # Horizon
                v_id_0 = "agent0"
                v_id_1 = "agent1"
                count = 2
                o = env.reset()
                assert set(o.keys()) == {"agent0", "agent1"}
                assert set(env.observations.keys()) == {"agent0", "agent1"}
                assert set(
                    env.action_space.spaces.keys()) == {"agent0", "agent1"}
                assert set(env.config["target_vehicle_configs"].keys()) == {
                    "agent0", "agent1"
                }
                assert set(env.vehicles.keys()) == {"agent0", "agent1"}
    finally:
        env.close()
    assert done_count > 0
    print("Finish {} dones.".format(done_count))