Skip to content

Commit

Permalink
Display the number of shipments and skipped shipments with a time win…
Browse files Browse the repository at this point in the history
…dow.
  • Loading branch information
ondrasej committed Nov 6, 2023
1 parent 737a038 commit 3107090
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions python/cfr/analysis/cfr-json-analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,20 @@
"# @title Aggregated shipment statistics\n",
"\n",
"\n",
"def _has_time_window(shipment):\n",
" for delivery in shipment.get(\"deliveries\", ()):\n",
" if \"timeWindows\" in delivery:\n",
" return True\n",
" return False\n",
"\n",
"\n",
"def get_basic_shipment_stats(scenario):\n",
" include_arrival_and_departure_virtual_shipments = False # @param {type: \"boolean\"}\n",
"\n",
" skipped_shipments = scenario.skipped_shipments\n",
" skipped_shipment_indices = set(\n",
" skipped_shipment.get(\"index\", 0) for skipped_shipment in skipped_shipments\n",
" )\n",
" shipments = scenario.shipments\n",
" if not include_arrival_and_departure_virtual_shipments:\n",
" filtered_shipments = []\n",
Expand All @@ -724,15 +734,34 @@
" shipment.get(\"label\", \"\").count(\",\") + 1\n",
" )\n",
"\n",
" shipments_with_time_window = [\n",
" shipment for shipment in shipments if _has_time_window(shipment)\n",
" ]\n",
" skipped_shipments_with_time_window = [\n",
" shipment\n",
" for shipment_index, shipment in enumerate(shipments)\n",
" if _has_time_window(shipment)\n",
" and shipment_index in skipped_shipment_indices\n",
" ]\n",
" return {\n",
" \"# CFR shipments\": len(shipments),\n",
" \"# actual shipments\": sum(\n",
" shipment[\"label\"].count(\",\") + 1 for shipment in shipments\n",
" ),\n",
" \"# CFR shipments w/TW\": len(shipments_with_time_window),\n",
" \"# actual shipments w/TW\": sum(\n",
" shipment[\"label\"].count(\",\") + 1\n",
" for shipment in shipments_with_time_window\n",
" ),\n",
" \"# skipped CFR shipments\": len(skipped_shipments),\n",
" \"# skipped actual shipments\": sum(\n",
" shipment[\"label\"].count(\",\") + 1 for shipment in skipped_shipments\n",
" ),\n",
" \"# skipped CFR shipments w/TW\": len(skipped_shipments_with_time_window),\n",
" \"# skipped actual shipments w/TW\": sum(\n",
" shipment[\"label\"].count(\",\") + 1\n",
" for shipment in skipped_shipments_with_time_window\n",
" ),\n",
" \"# CFR shipments from parking\": num_cfr_shipments_from_parking,\n",
" \"# actual shipments from parking\": num_actual_shipments_from_parking,\n",
" }\n",
Expand Down Expand Up @@ -1030,6 +1059,64 @@
"show_table_from_all_scenarios(get_skipped_shipment_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Im3WX2b6sP-V"
},
"outputs": [],
"source": [
"# @title Shipments by time window\n",
"\n",
"\n",
"def get_num_shipments_by_time_window(scenario):\n",
" data = []\n",
" shipments_by_time_window = collections.defaultdict(list)\n",
" skipped_shipments_by_time_window = collections.defaultdict(list)\n",
" # TODO(ondrasej): Make this work for pickups too.\n",
" for shipment_index, shipment in enumerate(scenario.shipments):\n",
" allowed_vehicles = \", \".join(\n",
" scenario.vehicles[vehicle_index].get(\"label\", \"\")\n",
" for vehicle_index in shipment.get(\"allowedVehicleIndices\", ())\n",
" )\n",
" for delivery in shipment.get(\"deliveries\", ()):\n",
" time_windows = delivery.get(\"timeWindows\", ())\n",
" str_time_window = human_readable_time_windows(time_windows)\n",
" shipments_by_time_window[allowed_vehicles, str_time_window].append(\n",
" shipment\n",
" )\n",
" if shipment_index in scenario.skipped_shipment_indices:\n",
" skipped_shipments_by_time_window[\n",
" allowed_vehicles, str_time_window\n",
" ].append(shipment)\n",
"\n",
" for (allowed_vehicles, time_window), shipments in sorted(\n",
" shipments_by_time_window.items()\n",
" ):\n",
" skipped_shipments = skipped_shipments_by_time_window.get(\n",
" (allowed_vehicles, time_window), ()\n",
" )\n",
" data.append({\n",
" \"allowed vehicles\": allowed_vehicles,\n",
" \"time window\": time_window or \"(none)\",\n",
" \"# CFR shipments\": len(shipments),\n",
" \"# actual shipments\": sum(\n",
" shipment.get(\"label\", \"\").count(\",\") + 1 for shipment in shipments\n",
" ),\n",
" \"# skipped CFR shipments\": len(skipped_shipments),\n",
" \"# skipped actual shipments\": sum(\n",
" shipment.get(\"label\", \"\").count(\",\") + 1\n",
" for shipment in skipped_shipments\n",
" ),\n",
" })\n",
"\n",
" return data\n",
"\n",
"\n",
"show_table_from_all_scenarios(get_num_shipments_by_time_window)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down

0 comments on commit 3107090

Please sign in to comment.