Skip to content

Commit

Permalink
Adjust add_osrm_matrix script for multi-profile, fixes VROOM-Project#16.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed Aug 10, 2021
1 parent e8c8c38 commit 9622d7c
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/add_osrm_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
from utils.file import load_json
from utils.osrm import table

# Parse a json-formatted input instance, compute the matrix using
# OSRM, then add the matrix and all relevant indices to the input
# problem. Possible usage include checking that solving is consistent
# between both instances, or creating a "standalone" problem instance
# that can be further solved even without an OSRM server handy.
# Parse a json-formatted input instance, compute the matrix using OSRM
# for each required profile, then add the matrix and all relevant
# indices to the input problem. Possible usage include checking that
# solving is consistent between both instances, or creating a
# "standalone" problem instance that can be further solved even
# without an OSRM server handy.

ROUTING = {
"car": {"host": "0.0.0.0", "port": "5000"},
"bike": {"host": "0.0.0.0", "port": "5001"},
"foot": {"host": "0.0.0.0", "port": "5002"},
}


def round_to_cost(d):
Expand Down Expand Up @@ -46,7 +53,14 @@ def get_index(locations, locations_indices, loc):
locs = []
index_of_known_locations = {}

profiles = set()

for v in data["vehicles"]:
if "profile" in v:
profiles.add(v["profile"])
else:
profiles.add("car")

if ("start" not in v) and ("end" not in v):
sys.exit("Missing coordinates for vehicle.")

Expand Down Expand Up @@ -80,14 +94,20 @@ def get_index(locations, locations_indices, loc):
locs, index_of_known_locations, shipment["delivery"]["location"]
)

# Get table from OSRM.
matrix = table(locs)["durations"]
data["matrix"] = []
# Get matrices from OSRM.
data["matrices"] = {}
for p in profiles:
if p not in ROUTING:
print("Invalid profile: " + p)
exit(1)

matrix = table(locs, ROUTING[p]["host"], ROUTING[p]["port"])["durations"]
data["matrices"][p] = {"durations": []}

# Round all costs to the nearest integer (same behavior as in
# osrm_wrapper.h)
for line in matrix:
data["matrix"].append(map(lambda d: round_to_cost(d), line))
# Round all costs to the nearest integer (same behavior as in
# osrm_wrapper.h)
for line in matrix:
data["matrices"][p]["durations"].append([round_to_cost(d) for d in line])

with open(output_name, "w") as out:
print("Writing problem with matrix to " + output_name)
Expand Down

0 comments on commit 9622d7c

Please sign in to comment.