예제 #1
0
    def test_automatic_return(self):
        """
        Check linear graph: half of the graph is outside the view, and the car that was sent outside
        is returning automatically to the nearest node in the view.
        """
        g = nx.Graph()
        g.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)])
        nx.set_node_attributes(g, {
            0: (0, 1),
            1: (0, 2),
            2: (0, 3),
            3: (0, 4),
            4: (0, 5),
            5: (0, 6)
        },
                               name="coords")
        orders = [(1, 4, 0, 10, 80)]
        drivers = np.array([0, 1, 0, 0, 0, 0])
        action = np.array([0, 0, 0], dtype=float)

        env = TaxiEnv(g, orders, 1, drivers, 30)
        env.set_view([0, 1, 2])
        env.step(action)
        # check that the final destination of the car is the node 2, in (10+2) intervals
        # after performing step(), the env should set current_node_id to 2, and time to 12
        assert env.time == 12
        assert env.current_node_id == 2
        d = env.all_driver_list[0]
        d.status = 1
        d.income = 80
        d.position = 2
예제 #2
0
    def test_view(self):
        g = nx.Graph()
        g.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 4), (1, 5)])
        nx.set_node_attributes(g, {
            0: (0, 1),
            1: (0, 2),
            2: (0, 3),
            3: (0, 4),
            4: (0, 5),
            5: (1, 1)
        },
                               name="coords")
        orders = [(3, 2, 2, 3, 3)
                  ]  # <source, destination, time, length, price>
        drivers = np.array([1, 1, 1, 1, 1, 1])
        action = np.array([1, 0, 0], dtype=float)

        env = TaxiEnv(g, orders, 1, drivers, 10)
        env.seed(123)
        env.set_view([2, 3, 4])
        obs, _, _ = env.get_observation()

        # check observation space and content
        assert env.observation_space_shape == obs.shape
        view_size = 3
        assert env.observation_space_shape == (
            view_size * 4 + 10,
        )  # default income is not included, so its <driver, order, idle, time_id, node_id>
        assert env.action_space_shape == (
            3,
        )  # degree of 1 is 3, but of the rest is 2. So it should be 2 + 1 (staying action)
        assert env.current_node_id in [2, 3, 4]

        # an action [1, 0, 0] for the node 2 means to go to node 3, because its the only neighbor in the view
        env.step(action)
        assert env.current_node_id in [2, 3, 4]
        env.step(action)
        assert env.current_node_id in [2, 3, 4]
        obs, rew, done, info = env.step(action)
        assert (obs[:view_size] == np.array([0.5, 1, 0])).all(
        )  # there are 2 drivers in the node 3 at the end, one from node 2, one from node 4.
        assert (obs[view_size:2 * view_size] == np.array([0, 0, 0])).all()
        assert (obs[2 * view_size:3 * view_size] == np.array([0.5, 1,
                                                              0])).all()
        # next time iteration should happen
        assert env.time == 1
        assert env.current_node_id in [2, 3]
        assert (obs[2 * view_size:3 * view_size] == np.array([0.5, 1,
                                                              0])).all()
        assert (obs[3 * view_size:3 * view_size + 10] == np.array(
            [0, 1, 0, 0, 0, 0, 0, 0, 0, 0])).all()
        assert obs[3 * view_size + 10:].shape == (3, )
        assert (obs[3 * view_size + 10:] == np.array([
            1, 0, 0
        ])).all() or (obs[3 * view_size + 10:] == np.array([0, 1, 0])).all()
        assert [d.position for d in env.all_driver_list] == [0, 1, 3, 2, 3, 5]