Skip to content

Commit

Permalink
Remove sideOfRoad in the local model when travelMode is not DRIVE.
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrasej committed Jan 12, 2024
1 parent 2a2029f commit c974247
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"latitude": 48.86482,
"longitude": 2.34932
}
}
},
"sideOfRoad": true
},
"duration": "1072s",
"tags": [
Expand Down Expand Up @@ -64,7 +65,8 @@
"latitude": 48.86482,
"longitude": 2.34932
}
}
},
"sideOfRoad": true
},
"duration": "863s",
"tags": [
Expand All @@ -91,7 +93,8 @@
"latitude": 48.86482,
"longitude": 2.34932
}
}
},
"sideOfRoad": true
},
"duration": "863s",
"tags": [
Expand Down Expand Up @@ -119,7 +122,8 @@
"latitude": 48.86482,
"longitude": 2.34932
}
}
},
"sideOfRoad": true
},
"duration": "600s",
"tags": [
Expand Down Expand Up @@ -151,7 +155,8 @@
"latitude": 48.86482,
"longitude": 2.34932
}
}
},
"sideOfRoad": true
},
"duration": "449s",
"tags": [
Expand Down Expand Up @@ -182,7 +187,8 @@
"latitude": 48.86482,
"longitude": 2.34932
}
}
},
"sideOfRoad": true
},
"duration": "449s",
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"arrivalWaypoint": {
"location": {
"latLng": { "latitude": 48.86482, "longitude": 2.34932 }
}
},
"sideOfRoad": true
},
"duration": "1176s",
"tags": ["P001"],
Expand All @@ -86,7 +87,8 @@
"arrivalWaypoint": {
"location": {
"latLng": { "latitude": 48.86482, "longitude": 2.34932 }
}
},
"sideOfRoad": true
},
"duration": "449s",
"tags": ["P002", "parking: P002"]
Expand All @@ -102,7 +104,8 @@
"arrivalWaypoint": {
"location": {
"latLng": { "latitude": 48.86482, "longitude": 2.34932 }
}
},
"sideOfRoad": true
},
"duration": "750s",
"tags": ["P002", "parking: P002"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1
"travelMode": 2
},
{
"costPerHour": 300,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1
"travelMode": 2
},
{
"costPerHour": 300,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1
"travelMode": 2
},
{
"costPerHour": 300,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1,
"travelMode": 2,
"fixedCost": 10000,
"costPerHour": 300,
"costPerKilometer": 60,
Expand Down Expand Up @@ -248,7 +248,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1,
"travelMode": 2,
"fixedCost": 10000,
"costPerHour": 300,
"costPerKilometer": 60,
Expand Down Expand Up @@ -286,7 +286,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1,
"travelMode": 2,
"fixedCost": 10000,
"costPerHour": 300,
"costPerKilometer": 60,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1,
"travelMode": 2,
"startTags": ["P001"],
"endTags": ["P001"]
},
Expand All @@ -200,7 +200,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1,
"travelMode": 2,
"startTags": ["P001"],
"endTags": ["P001"]
},
Expand All @@ -222,7 +222,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1,
"travelMode": 2,
"startTags": ["P001"],
"endTags": ["P001"]
},
Expand All @@ -244,7 +244,7 @@
}
},
"travelDurationMultiple": 1.1,
"travelMode": 1,
"travelMode": 2,
"startTags": ["P001"],
"endTags": ["P001"]
},
Expand Down
15 changes: 13 additions & 2 deletions python/cfr/two_step_routing/testdata/small/parking.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
{
"parking_locations": [
{
"coordinates": { "latitude": 48.86482, "longitude": 2.34932 },
"waypoint": {
"location": {
"latLng": { "latitude": 48.86482, "longitude": 2.34932 }
},
"sideOfRoad": true
},
"tag": "P001",
"travel_mode": 2,
"travel_duration_multiple": 1.1,
"delivery_load_limits": { "ore": 2 },
"max_round_duration": "1800s"
},
{
"coordinates": { "latitude": 48.86482, "longitude": 2.34932 },
"waypoint": {
"location": {
"latLng": { "latitude": 48.86482, "longitude": 2.34932 }
},
"sideOfRoad": true
},
"tag": "P002",
"travel_mode": 2,
"delivery_load_limits": { "ore": 2 },
Expand Down
29 changes: 25 additions & 4 deletions python/cfr/two_step_routing/two_step_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import copy
import dataclasses
import enum
import functools
import math
import re
from typing import Any, TypeAlias, TypeVar, cast
Expand Down Expand Up @@ -209,6 +210,26 @@ def __post_init__(self, coordinates: cfr_json.LatLng | None):
self, "waypoint", {"location": {"latLng": coordinates}}
)

@functools.cached_property
def waypoint_for_local_model(self) -> cfr_json.Waypoint:
"""Returns a waypoint for the parking to be used in local models.
Local models typically use with travel modes other than DRIVE, which is not
compatible with sideOfRoad. To allow using `sideOfRoad` in parking location
waypoints (so that it is used in the global model), we need to remove it
from the waypoint when it is used in a local model.
Returns:
The waypoint of the parking location. When the travel mode of the parking
location is not DRIVE, `sideOfRoad` is removed from the waypoint. The
returned object is cached and must not be mutated.
"""
if self.travel_mode == 1:
return self.waypoint
waypoint = copy.deepcopy(self.waypoint)
waypoint.pop("sideOfRoad", None)
return waypoint


def load_parking_from_json(
parking_json: Any,
Expand Down Expand Up @@ -682,7 +703,7 @@ def get_non_existent_tag(base: str) -> str:
for consecutive_visit_sequence in consecutive_visit_sequences:
parking = self._parking_locations[consecutive_visit_sequence.parking_tag]

parking_waypoint = parking.waypoint
parking_waypoint = parking.waypoint_for_local_model

refinement_vehicle_index = len(refinement_vehicles)
refinement_vehicle_label = (
Expand Down Expand Up @@ -1028,7 +1049,7 @@ def add_parking_location_shipment(
shipment: cfr_json.Shipment = {
"label": f"{parking.tag} {arrival_or_departure}",
"deliveries": [{
"arrivalWaypoint": parking.waypoint,
"arrivalWaypoint": parking.waypoint_for_local_model,
"duration": "0s",
}],
# TODO(ondrasej): Vehicle costs and allowed vehicle indices.
Expand Down Expand Up @@ -1261,8 +1282,8 @@ def _make_local_model_vehicle(
vehicle: cfr_json.Vehicle = {
"label": label,
# Start and end waypoints.
"endWaypoint": parking.waypoint,
"startWaypoint": parking.waypoint,
"endWaypoint": parking.waypoint_for_local_model,
"startWaypoint": parking.waypoint_for_local_model,
# Limits and travel speed.
"travelDurationMultiple": parking.travel_duration_multiple,
"travelMode": parking.travel_mode,
Expand Down

0 comments on commit c974247

Please sign in to comment.