diff --git a/README.rst b/README.rst index cb84fbb..edaf6e8 100644 --- a/README.rst +++ b/README.rst @@ -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 ------------ diff --git a/src/bind/solution/solution.cpp b/src/bind/solution/solution.cpp index 2d9ba82..d392f1f 100644 --- a/src/bind/solution/solution.cpp +++ b/src/bind/solution/solution.cpp @@ -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; @@ -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) diff --git a/src/vroom/input/input.py b/src/vroom/input/input.py index bbf3f2e..26bc509 100644 --- a/src/vroom/input/input.py +++ b/src/vroom/input/input.py @@ -29,6 +29,8 @@ class Input(_vroom.Input): """ + _geometry: bool = False + def __init__( self, amount_size: Optional[int] = None, @@ -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.""" @@ -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 diff --git a/src/vroom/solution/solution.py b/src/vroom/solution/solution.py index 7faea0b..7a2c0ba 100644 --- a/src/vroom/solution/solution.py +++ b/src/vroom/solution/solution.py @@ -22,6 +22,8 @@ class Solution(_vroom.Solution): Frame outlining all routes for all vehicles. """ + _geometry: bool = False + @property def routes(self) -> pandas.DataFrame: """ @@ -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()