Skip to content

Commit

Permalink
Show distance column in solution only when required
Browse files Browse the repository at this point in the history
  • Loading branch information
SebMilardo committed May 23, 2024
1 parent 1ea9dad commit ee59f99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/vroom/input/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Input(_vroom.Input):
"""

_geometry: bool = False
_distances: bool = False

def __init__(
self,
Expand Down Expand Up @@ -308,6 +309,7 @@ def set_distances_matrix(
if not isinstance(matrix_input, _vroom.Matrix):
matrix_input = _vroom.Matrix(numpy.asarray(matrix_input, dtype="uint32"))
self._set_distances_matrix(profile, matrix_input)
self._distances = True

def set_costs_matrix(
self,
Expand Down Expand Up @@ -341,4 +343,5 @@ def solve(
)
)
solution._geometry = self._geometry
solution._distances = self._distances
return solution
9 changes: 6 additions & 3 deletions src/vroom/solution/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Solution(_vroom.Solution):
"""

_geometry: bool = False
_distances: bool = False

@property
def routes(self) -> pandas.DataFrame:
Expand Down Expand Up @@ -58,6 +59,8 @@ def routes(self) -> pandas.DataFrame:
The identifier for the task that was performed.
description:
Text description provided to this step.
distance:
Total route distance.
"""
array = numpy.asarray(self._routes_numpy())
frame = pandas.DataFrame(
Expand All @@ -84,15 +87,15 @@ def routes(self) -> pandas.DataFrame:
del frame[column]
else:
frame.loc[frame[column] == NA_SUBSTITUTE, column] = pandas.NA
if self._geometry:
if self._geometry or self._distances:
frame["distance"] = array["distance"]
return frame

def to_dict(self) -> Dict[str, Any]:
"""Convert solution into VROOM compatible dictionary."""
stream = io.StringIO()
with redirect_stdout(stream):
if self._geometry:
if self._geometry or self._distances:
self._geometry_solution_json()
else:
self._solution_json()
Expand All @@ -102,7 +105,7 @@ def to_json(self, filepath: Union[str, Path]) -> None:
"""Store solution into VROOM compatible JSON file."""
with open(filepath, "w") as handler:
with redirect_stdout(handler):
if self._geometry:
if self._geometry or self._distances:
self._geometry_solution_json()
else:
self._solution_json()
9 changes: 9 additions & 0 deletions test/test_libvroom_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def test_example_with_custom_matrix():
[197, 2256, 0, 1102],
[1299, 3153, 1102, 0]],
)
problem_instance.set_distances_matrix(
profile="car",
matrix_input=[[0, 21040, 1970, 12990],
[21030, 0, 22550, 31520],
[1970, 22560, 0, 11020],
[12990, 31530, 11020, 0]],
)
problem_instance.add_vehicle([vroom.Vehicle(7, start=0, end=0),
vroom.Vehicle(8, start=2, end=2)])
problem_instance.add_job([vroom.Job(id=1414, location=0),
Expand All @@ -37,3 +44,5 @@ def test_example_with_custom_matrix():
assert numpy.all(routes.arrival == [0, 2104, 4207, 4207,
0, 1102, 2204, 2204])
assert numpy.all(routes.location_index == [0, 1, 0, 0, 2, 3, 2, 2])
assert numpy.all(routes.distance == [0, 21040, 42070, 42070,
0, 11020, 22040, 22040])

0 comments on commit ee59f99

Please sign in to comment.