Skip to content

Commit

Permalink
Merge branch 'export_geometry'
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathf committed May 2, 2024
2 parents 7b17786 + bd5f0f3 commit 590a7d8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Usage with a routing engine
>>> sol = problem_instance.solve(exploration_level=5, nb_threads=4)
>>> print(sol.summary.duration)
2698
2704
Installation
------------
Expand Down
7 changes: 7 additions & 0 deletions src/bind/solution/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void init_solution(py::module_ &m) {
ptr[idx].setup = step.setup;
ptr[idx].service = step.service;
ptr[idx].waiting_time = step.waiting_time;
ptr[idx].distance = step.distance;
ptr[idx].arrival = step.arrival;
ptr[idx].duration = step.duration;

Expand All @@ -104,6 +105,12 @@ void init_solution(py::module_ &m) {
std::cout, py::module_::import("sys").attr("stdout"));
vroom::io::write_to_json(solution, false, "");
})
.def("_geometry_solution_json",
[](vroom::Solution solution) {
py::scoped_ostream_redirect stream(
std::cout, py::module_::import("sys").attr("stdout"));
vroom::io::write_to_json(solution, true, "");
})
.def_readwrite("code", &vroom::Solution::code)
.def_readwrite("error", &vroom::Solution::error)
.def_readonly("summary", &vroom::Solution::summary)
Expand Down
12 changes: 10 additions & 2 deletions src/vroom/input/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Input(_vroom.Input):
"""

_geometry: bool = False

def __init__(
self,
amount_size: Optional[int] = None,
Expand Down Expand Up @@ -104,13 +106,16 @@ def from_json(
"""
if geometry is None:
geometry = servers is not None
if geometry:
self._set_geometry(True)
instance = Input(servers=servers, router=router)
with open(filepath) as handle:
instance._from_json(handle.read(), geometry)
return instance

def set_geometry(self):
return self._set_geometry()
self._geometry = True
return self._set_geometry(True)

def set_amount_size(self, *amount_sizes: int) -> None:
"""Add amount sizes."""
Expand Down Expand Up @@ -307,9 +312,12 @@ def solve(
exploration_level: int,
nb_threads: int,
) -> Solution:
return Solution(
solution = Solution(
self._solve(
exploration_level=exploration_level,
nb_threads=nb_threads,
)

)
solution._geometry = self._geometry
return solution
14 changes: 12 additions & 2 deletions src/vroom/solution/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Solution(_vroom.Solution):
Frame outlining all routes for all vehicles.
"""

_geometry: bool = False

@property
def routes(self) -> pandas.DataFrame:
"""
Expand Down Expand Up @@ -81,17 +83,25 @@ def routes(self) -> pandas.DataFrame:
del frame[column]
else:
frame.loc[frame[column] == NA_SUBSTITUTE, column] = pandas.NA
if self._geometry:
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):
self._solution_json()
if self._geometry:
self._geometry_solution_json()
else:
self._solution_json()
return json.loads(stream.getvalue())

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):
self._solution_json()
if self._geometry:
self._geometry_solution_json()
else:
self._solution_json()

0 comments on commit 590a7d8

Please sign in to comment.