def test_multiple_departure_time_vehicle(self): """Check that departure time can be define.""" vehicles = [ dynamic_routing_utils.Vehicle("O->A", "D->E", 0), dynamic_routing_utils.Vehicle("O->A", "D->E", 0.5), dynamic_routing_utils.Vehicle("O->A", "D->E", 1.0) ] game = dynamic_routing.DynamicRoutingGame( { "max_num_time_step": 10, "time_step_length": 0.5, "players": -1 }, vehicles=vehicles) pyspiel.random_sim_test(game, num_sims=10, serialize=False, verbose=True)
def create_games(origin, destination, num_vehicles, graph, max_time_step, time_step_length=1.0, departure_time=None): if departure_time is not None: raise NotImplementedError("To do.") list_of_vehicles = [ dynamic_routing_utils.Vehicle(origin, destination) for _ in range(num_vehicles) ] game = dynamic_routing.DynamicRoutingGame( { "max_num_time_step": max_time_step, "time_step_length": time_step_length }, network=graph, vehicles=list_of_vehicles) seq_game = pyspiel.convert_to_turn_based(game) od_demand = [ dynamic_routing_utils.OriginDestinationDemand(origin, destination, 0, num_vehicles) ] mfg_game = mean_field_routing_game.MeanFieldRoutingGame( { "max_num_time_step": max_time_step, "time_step_length": time_step_length }, network=graph, od_demand=od_demand) return game, seq_game, mfg_game
def test_vehicle_destination_outside_network(self): """Check raise assertion if vehicle's destination is outside the Network.""" vehicles = [dynamic_routing_utils.Vehicle("O->A", "E->F", 0)] with self.assertRaises(ValueError): dynamic_routing.DynamicRoutingGame( { "max_num_time_step": 10, "time_step_length": 0.5, "players": -1 }, vehicles=vehicles)
def test_braess_paradox(self): """Test that Braess paradox can be reproduced with the mean field game.""" num_player = 8 braess_network = dynamic_routing_utils.Network( { "O": "A", "A": ["B", "C"], "B": ["C", "D"], "C": ["D"], "D": ["E"], "E": [] }, node_position={ "O": (0, 0), "A": (1, 0), "B": (2, 1), "C": (2, -1), "D": (3, 0), "E": (4, 0) }, bpr_a_coefficient={ "O->A": 0, "A->B": 1.0, "A->C": 0, "B->C": 0, "B->D": 0, "C->D": 1.0, "D->E": 0 }, bpr_b_coefficient={ "O->A": 1.0, "A->B": 1.0, "A->C": 1.0, "B->C": 1.0, "B->D": 1.0, "C->D": 1.0, "D->E": 1.0 }, capacity={ "O->A": num_player, "A->B": num_player, "A->C": num_player, "B->C": num_player, "B->D": num_player, "C->D": num_player, "D->E": num_player }, free_flow_travel_time={ "O->A": 0, "A->B": 1.0, "A->C": 2.0, "B->C": 0.25, "B->D": 2.0, "C->D": 1.0, "D->E": 0 }) demand = [ dynamic_routing_utils.Vehicle("O->A", "D->E") for _ in range(num_player) ] game = dynamic_routing.DynamicRoutingGame( {"time_step_length": 0.125, "max_num_time_step": 40}, network=braess_network, vehicles=demand) class TruePathPolicy(policy.Policy): def __init__(self, game): super().__init__(game, list(range(num_player))) self._path = {} def action_probabilities(self, state, player_id=None): assert player_id is not None legal_actions = state.legal_actions(player_id) if not legal_actions: return {dynamic_routing_utils.NO_POSSIBLE_ACTION: 1.0} elif len(legal_actions) == 1: return {legal_actions[0]: 1.0} else: if legal_actions[0] == 2: if self._path[player_id] in ["top", "middle"]: return {2: 1.0} elif self._path[player_id] == "bottom": return {3: 1.0} else: raise ValueError() elif legal_actions[0] == 4: if self._path[player_id] == "top": return {5: 1.0} elif self._path[player_id] == "middle": return {4: 1.0} else: raise ValueError() raise ValueError(f"{legal_actions} is not correct.") class NashEquilibriumBraess(TruePathPolicy): def __init__(self, game): super().__init__(game) for player_id in range(num_player): if player_id % 2 == 0: self._path[player_id] = "middle" if player_id % 4 == 1: self._path[player_id] = "top" if player_id % 4 == 3: self._path[player_id] = "bottom" class SocialOptimumBraess(NashEquilibriumBraess): def __init__(self, game): super().__init__(game) for player_id in range(num_player): if player_id % 2 == 0: self._path[player_id] = "top" if player_id % 2 == 1: self._path[player_id] = "bottom" ne_policy = NashEquilibriumBraess(game) # TODO(cabannes): debug issue with nash conv computation and uncomment the # following line. # self.assertEqual(exploitability.nash_conv(game, ne_policy), 0.0) self.assertSequenceAlmostEqual( -expected_game_score.policy_value(game.new_initial_state(), ne_policy), [3.75] * num_player) so_policy = SocialOptimumBraess(game) # TODO(cabannes): debug issue with nash conv computation and uncomment the # following line. # self.assertEqual(exploitability.nash_conv(game, so_policy), 0.125) self.assertSequenceAlmostEqual( -expected_game_score.policy_value(game.new_initial_state(), so_policy), [3.5] * num_player)
def test_vehicle_2(self): """Test instanciation of with departure time.""" vehicle = utils.Vehicle("O->A", "B->D", 10.5) self.assertEqual(vehicle.origin, "O->A") self.assertEqual(vehicle.destination, "B->D") self.assertEqual(vehicle.departure_time, 10.5)
# See the License for the specific language governing permissions and # limitations under the License. """Default data for dynamic routing game.""" from open_spiel.python.games import dynamic_routing_utils LINE_NETWORK = dynamic_routing_utils.Network({ "bef_O": "O", "O": ["A"], "A": ["D"], "D": ["aft_D"], "aft_D": [] }) LINE_NETWORK_VEHICLES_DEMAND = [ dynamic_routing_utils.Vehicle("bef_O->O", "D->aft_D") for _ in range(2) ] LINE_NETWORK_OD_DEMAND = [ dynamic_routing_utils.OriginDestinationDemand("bef_O->O", "D->aft_D", 0, 100) ] BRAESS_NUM_PLAYER = 5 BRAESS_NETWORK = dynamic_routing_utils.Network( { "O": "A", "A": ["B", "C"], "B": ["C", "D"], "C": ["D"], "D": ["E"],