Example #1
0
 def test_sample_size(self):
     self.assertEqual(
         len(
             get_initial_state(200, 10, cache=False,
                               save=False).get_scooters()),
         200,
     )
Example #2
0
 def test_create_multiple_vehicles(self):
     multiple_vehicle_state = get_initial_state(100,
                                                number_of_vans=2,
                                                number_of_bikes=3)
     # There is a total of 5 vehicles
     self.assertEqual(len(multiple_vehicle_state.vehicles), 5)
     # No vehicle has the same id
     for vehicle in multiple_vehicle_state.vehicles:
         self.assertEqual(
             1,
             len([
                 id_vehicle
                 for id_vehicle in multiple_vehicle_state.vehicles
                 if id_vehicle.id == vehicle.id
             ]),
         )
     # There are three vehicles with no scooter capacity
     self.assertEqual(
         3,
         len([
             vehicle for vehicle in multiple_vehicle_state.vehicles
             if vehicle.scooter_inventory_capacity == 0
         ]),
     )
     # There are two vehicles with scooter capacity
     self.assertEqual(
         2,
         len([
             vehicle for vehicle in multiple_vehicle_state.vehicles
             if vehicle.scooter_inventory_capacity > 0
         ]),
     )
Example #3
0
 def test_number_of_actions(self):
     bigger_state = get_initial_state(sample_size=1000, initial_location_depot=False)
     vehicle = bigger_state.vehicles[0]
     vehicle.current_location = random.choice(
         [
             cluster
             for cluster in bigger_state.clusters
             if cluster.number_of_scooters() > 0
         ]
     )
     self.assertLess(
         len(
             bigger_state.get_possible_actions(
                 vehicle, self.number_of_neighbours, divide=2
             )
         ),
         len(bigger_state.get_possible_actions(vehicle, self.number_of_neighbours)),
     )
     self.assertLess(
         len(
             bigger_state.get_possible_actions(
                 vehicle, self.number_of_neighbours, divide=2
             )
         ),
         len(
             bigger_state.get_possible_actions(
                 vehicle, self.number_of_neighbours, divide=4
             )
         ),
     )
Example #4
0
    def test_filtering_neighbours(self):
        state = get_initial_state(100, 10)

        best_neighbours_with_random = filtering_neighbours(
            state, number_of_neighbours=3, number_of_random_neighbours=1)

        # test if the number of neighbours is the same, even though one is random
        self.assertEqual(len(best_neighbours_with_random), 3)

        sorted_neighbours = state.get_neighbours(state.current_location,
                                                 is_sorted=True)[:3]
        for cluster in state.clusters:
            if cluster in sorted_neighbours:
                cluster.ideal_state = 100
                for scooter in cluster.scooters:
                    scooter.battery = 0

        best_neighbours = filtering_neighbours(state, number_of_neighbours=3)

        # add one scooter to vehicle inventory so filtering neighbours uses the right filtering method
        state.vehicle.pick_up(Scooter(0, 0, 0.9, 0))

        # check if clusters are closest and with the highest deviation -> best neighbours
        for neighbour in best_neighbours:
            self.assertTrue(neighbour in sorted_neighbours)
Example #5
0
 def setUp(self) -> None:
     self.world = classes.World(
         shift_duration=BATTERY_INVENTORY * SWAP_TIME_PER_BATTERY + 1,
         sample_size=100,
         number_of_clusters=10,
         initial_state=get_initial_state(500),
     )
     self.world.state.current_location = self.world.state.depots[0]
Example #6
0
    def test_state_and_flow_between_clusters():
        # state and flow between clusters visualization
        state = get_initial_state(sample_size=100, number_of_clusters=6)

        state.visualize()

        flows, trips, _ = system_simulate(state)

        visualize_cluster_flow(state, flows)
Example #7
0
    def test_state_and_flow_between_clusters():
        # state and flow between clusters visualization
        world = classes.World(
            5, None, get_initial_state(sample_size=100, number_of_clusters=6)
        )

        flows, trips, _ = system_simulate(world.state)

        visualize_cluster_flow(world.state, flows)
Example #8
0
    def test_scooter_trips():
        # scooter trips visualization
        world = classes.World(
            5, None, get_initial_state(sample_size=20, number_of_clusters=5)
        )

        next_world = copy.deepcopy(world)

        flows, scooter_trips, _ = next_world.system_simulate()

        world.state.visualize_system_simulation(scooter_trips)
Example #9
0
 def test_analysis():
     # test the analysis plot
     run_analysis(
         [
             classes.World(
                 21,
                 decision.SwapAllPolicy(),
                 get_initial_state(sample_size=100, number_of_clusters=10),
             )
         ],
         runs_per_policy=1,
     )
Example #10
0
    def test_scooter_trips():
        # scooter trips visualization

        current_state = get_initial_state(sample_size=20, number_of_clusters=5)

        next_state = copy.deepcopy(current_state)

        flows, scooter_trips, _ = next_state.system_simulate()

        current_state.visualize_system_simulation(scooter_trips)

        current_state.visualize_flow(flows)
Example #11
0
    def test_number_of_actions_clusters(self):
        initial_state = get_initial_state(sample_size=100,
                                          number_of_clusters=6,
                                          initial_location_depot=False)
        # Modify initial state. 5 battery swaps and 2 drop-offs possible
        initial_state.vehicle.scooter_inventory = []
        initial_state.current_location.scooters = []

        # Get all possible actions
        actions = initial_state.get_possible_actions(number_of_neighbours=5)

        # Test number of actions possible
        self.assertEqual(len(actions), 5)
Example #12
0
 def test_number_of_actions(self):
     bigger_state = get_initial_state(sample_size=1000,
                                      initial_location_depot=False)
     bigger_state.current_location = random.choice([
         cluster for cluster in bigger_state.clusters
         if cluster.number_of_scooters() > 0
     ])
     self.assertLess(
         len(bigger_state.get_possible_actions(divide=2)),
         len(bigger_state.get_possible_actions()),
     )
     self.assertLess(
         len(bigger_state.get_possible_actions(divide=2)),
         len(bigger_state.get_possible_actions(divide=3)),
     )
Example #13
0
    def test_number_of_actions_clusters(self):
        initial_state = get_initial_state(
            sample_size=100, number_of_clusters=6, initial_location_depot=False
        )
        vehicle = initial_state.vehicles[0]
        # Modify initial state. 1 battery swap and 0 drop-offs possible
        vehicle.scooter_inventory_capacity = 0
        vehicle.current_location.scooters = vehicle.current_location.scooters[:1]

        # Get all possible actions
        actions = initial_state.get_possible_actions(
            vehicle,
            number_of_neighbours=2,
        )

        # Test number of actions possible
        self.assertEqual(2, len(actions))
Example #14
0
 def __init__(
     self,
     shift_duration: int,
     sample_size=100,
     number_of_clusters=20,
     initial_state=None,
     policy="RandomRolloutPolicy",
     initial_location_depot=True,
     verbose=False,
 ):
     self.shift_duration = shift_duration
     if initial_state:
         self.state = initial_state
     else:
         self.state = clustering_scripts.get_initial_state(
             sample_size=sample_size,
             number_of_clusters=number_of_clusters,
             initial_location_depot=initial_location_depot,
         )
     self.stack = []
     self.time = 0
     self.rewards = []
     self.cluster_flow = {
         (start, end): 0
         for start in np.arange(len(self.state.clusters))
         for end in np.arange(len(self.state.clusters))
         if start != end
     }
     self.policy = get_policy(policy)
     self.metrics = World.WorldMetric()
     self.verbose = verbose
     if verbose:
         self.progress_bar = IncrementalBar(
             "Running World",
             check_tty=False,
             max=round(shift_duration / ITERATION_LENGTH_MINUTES) + 1,
             color=WHITE,
             suffix="%(percent)d%% - ETA %(eta)ds",
         )
Example #15
0
 def setUp(self) -> None:
     self.state_mid = get_initial_state(500, cache=False, save=False)
     self.state_small = get_initial_state(100)
     self.state_big = get_initial_state(2000)
Example #16
0
 def test_visualize_clusters():
     current_state = get_initial_state(sample_size=1000, number_of_clusters=20)
     current_state.visualize_clustering()
Example #17
0
 def setUp(self) -> None:
     self.initial_state = get_initial_state(
         sample_size=100, number_of_clusters=2, initial_location_depot=False
     )
     self.vehicle = self.initial_state.vehicles[0]
     self.number_of_neighbours = 4
Example #18
0
 def test_current_cluster_is_depot(self):
     for vehicle in get_initial_state(100).vehicles:
         self.assertIsInstance(vehicle.current_location, Depot)
Example #19
0
 def setUp(self) -> None:
     self.state_mid = get_initial_state(500)
     self.state_small = get_initial_state(100)
     self.state_big = get_initial_state(2000)
Example #20
0
 def setUp(self) -> None:
     self.state_mid = get_initial_state(500)
     self.state_small = get_initial_state(100)
     self.state_big = get_initial_state(2000, initial_location_depot=False)
     self.all_states = [self.state_small, self.state_mid, self.state_big]
Example #21
0
 def setUp(self) -> None:
     self.state = get_initial_state(500)
Example #22
0
 def setUp(self) -> None:
     self.world = classes.World(1, None, get_initial_state(2500, 50))
Example #23
0
 def setUp(self) -> None:
     self.initial_state = get_initial_state(sample_size=100,
                                            number_of_clusters=2,
                                            initial_location_depot=False)