Skip to content

Commit

Permalink
Added a computation of the number of all visits to the CFR JSON analy…
Browse files Browse the repository at this point in the history
…sis notebook.
  • Loading branch information
ondrasej committed Nov 8, 2023
1 parent 5c5492c commit e5a853a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/cfr/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ParkingLocationData:
the solution. Note that parking locations that are not visited by any
vehicle do not appear in the solution and by consequence, do not appear in
this set.
num_visits_to_parking: The number of times each parking is visited.
vehicles_by_parking: For each parking tag, contains a mapping from vehicle
indices to the list of indices of the visits made by this vehicle.
consecutive_visits: The per-vehicle list of consecutive visits to a parking
Expand Down Expand Up @@ -49,9 +50,12 @@ class ParkingLocationData:
to the "departure from parking" virtual shipment. When there are multiple
visits to the same parking location (a "parking ping-pong"), each delivery
round has its own entry in the list.
num_all_visits_to_parking: The number of times any parking location in the
solution is visited.
"""

all_parking_tags: Set[str]
num_visits_to_parking: Mapping[str, int]
vehicles_by_parking: Mapping[str, Mapping[int, Sequence[int]]]
consecutive_visits: Mapping[int, Sequence[tuple[str, int]]]
non_consecutive_visits: Mapping[int, Sequence[tuple[str, int]]]
Expand All @@ -60,6 +64,10 @@ class ParkingLocationData:
int, Sequence[tuple[two_step_routing.ParkingTag | None, int, int]]
]

@functools.cached_property
def num_all_visits_to_parking(self):
return sum(self.num_visits_to_parking.values())


@dataclasses.dataclass
class Scenario:
Expand Down Expand Up @@ -187,6 +195,7 @@ def get_parking_location_aggregate_data(

routes = scenario.routes
all_parking_tags = set()
num_visits_to_parking = collections.defaultdict(int)
# The number of visits to each parking location by each vehicle.
visits_by_vehicle = collections.defaultdict(
functools.partial(collections.defaultdict, int)
Expand Down Expand Up @@ -226,6 +235,7 @@ def get_parking_location_aggregate_data(
f" {current_parking_tag!r}, found {departure_tag!r}."
)
all_parking_tags.add(departure_tag)
num_visits_to_parking[departure_tag] += 1
global_visits.append(
(current_parking_tag, current_parking_arrival_visit, visit_index)
)
Expand Down Expand Up @@ -273,6 +283,7 @@ def get_parking_location_aggregate_data(

return ParkingLocationData(
all_parking_tags=all_parking_tags,
num_visits_to_parking=num_visits_to_parking,
vehicles_by_parking=vehicles_by_parking,
consecutive_visits=vehicle_consecutive_visits,
non_consecutive_visits=vehicle_non_consecutive_visits,
Expand Down
2 changes: 2 additions & 0 deletions python/cfr/analysis/cfr-json-analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@
"\n",
" return [{\n",
" \"# distinct visited parkings\": len(parking_data.all_parking_tags),\n",
" \"# visits to parking\": parking_data.num_all_visits_to_parking,\n",
" \"# parkings served by multiple vehicles\": sum(\n",
" int(len(parking_vehicles) > 1)\n",
" for parking_vehicles in parking_data.vehicles_by_parking.values()\n",
Expand Down Expand Up @@ -1456,6 +1457,7 @@
"\n",
" data.append({\n",
" \"parking_tag\": parking_tag,\n",
" \"# visits\": parking_data.num_visits_to_parking[parking_tag],\n",
" \"# CFR shipments\": num_cfr_shipments,\n",
" \"# actual shipments\": num_actual_shipments,\n",
" \"# vehicles\": len(parking_vehicles),\n",
Expand Down

0 comments on commit e5a853a

Please sign in to comment.