From 78afd6426b57392efa388646a41e4ac38b15ddba Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Tue, 9 Apr 2024 16:06:37 +0200 Subject: [PATCH 1/5] adding geometry flag --- src/bind/solution/solution.cpp | 6 +++--- src/vroom/solution/solution.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bind/solution/solution.cpp b/src/bind/solution/solution.cpp index 2d9ba82..18734de 100644 --- a/src/bind/solution/solution.cpp +++ b/src/bind/solution/solution.cpp @@ -99,11 +99,11 @@ void init_solution(py::module_ &m) { return arr; }) .def("_solution_json", - [](vroom::Solution solution) { + [](vroom::Solution solution, bool geometry) { py::scoped_ostream_redirect stream( std::cout, py::module_::import("sys").attr("stdout")); - vroom::io::write_to_json(solution, false, ""); - }) + vroom::io::write_to_json(solution, geometry, ""); + }, py::arg("solution"), py::arg("geometry") = false) .def_readwrite("code", &vroom::Solution::code) .def_readwrite("error", &vroom::Solution::error) .def_readonly("summary", &vroom::Solution::summary) diff --git a/src/vroom/solution/solution.py b/src/vroom/solution/solution.py index 7faea0b..c6247c1 100644 --- a/src/vroom/solution/solution.py +++ b/src/vroom/solution/solution.py @@ -83,15 +83,15 @@ def routes(self) -> pandas.DataFrame: frame.loc[frame[column] == NA_SUBSTITUTE, column] = pandas.NA return frame - def to_dict(self) -> Dict[str, Any]: + def to_dict(self, geometry: bool = False) -> Dict[str, Any]: """Convert solution into VROOM compatible dictionary.""" stream = io.StringIO() with redirect_stdout(stream): - self._solution_json() + self._solution_json(geometry) return json.loads(stream.getvalue()) - def to_json(self, filepath: Union[str, Path]) -> None: + def to_json(self, filepath: Union[str, Path], geometry: bool = False) -> None: """Store solution into VROOM compatible JSON file.""" with open(filepath, "w") as handler: with redirect_stdout(handler): - self._solution_json() + self._solution_json(geometry) From e8fdeed40ad6d7ca3d966c879db927ea3b1dc5b1 Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Tue, 9 Apr 2024 16:17:14 +0200 Subject: [PATCH 2/5] remove annotation --- src/bind/solution/solution.cpp | 6 +++--- src/vroom/solution/solution.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bind/solution/solution.cpp b/src/bind/solution/solution.cpp index 18734de..da0d1b9 100644 --- a/src/bind/solution/solution.cpp +++ b/src/bind/solution/solution.cpp @@ -99,11 +99,11 @@ void init_solution(py::module_ &m) { return arr; }) .def("_solution_json", - [](vroom::Solution solution, bool geometry) { + [](vroom::Solution solution) { py::scoped_ostream_redirect stream( std::cout, py::module_::import("sys").attr("stdout")); - vroom::io::write_to_json(solution, geometry, ""); - }, py::arg("solution"), py::arg("geometry") = false) + 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/solution/solution.py b/src/vroom/solution/solution.py index c6247c1..7faea0b 100644 --- a/src/vroom/solution/solution.py +++ b/src/vroom/solution/solution.py @@ -83,15 +83,15 @@ def routes(self) -> pandas.DataFrame: frame.loc[frame[column] == NA_SUBSTITUTE, column] = pandas.NA return frame - def to_dict(self, geometry: bool = False) -> Dict[str, Any]: + def to_dict(self) -> Dict[str, Any]: """Convert solution into VROOM compatible dictionary.""" stream = io.StringIO() with redirect_stdout(stream): - self._solution_json(geometry) + self._solution_json() return json.loads(stream.getvalue()) - def to_json(self, filepath: Union[str, Path], geometry: bool = False) -> None: + 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(geometry) + self._solution_json() From 7431231c0cbda64e10ef47e56f11f87d1afbcfa4 Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Fri, 19 Apr 2024 15:25:23 +0200 Subject: [PATCH 3/5] trying it out --- src/bind/solution/solution.cpp | 7 +++++++ src/vroom/input/input.py | 10 +++++++++- src/vroom/solution/solution.py | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/bind/solution/solution.cpp b/src/bind/solution/solution.cpp index da0d1b9..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; @@ -99,6 +100,12 @@ void init_solution(py::module_ &m) { return arr; }) .def("_solution_json", + [](vroom::Solution solution) { + py::scoped_ostream_redirect stream( + 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")); diff --git a/src/vroom/input/input.py b/src/vroom/input/input.py index bbf3f2e..d989ad1 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,12 +106,15 @@ def from_json( """ if geometry is None: geometry = servers is not None + if geometry: + self._set_geometry() instance = Input(servers=servers, router=router) with open(filepath) as handle: instance._from_json(handle.read(), geometry) return instance def set_geometry(self): + self._geometry = True return self._set_geometry() def set_amount_size(self, *amount_sizes: int) -> None: @@ -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() From df29bf00a44fa9cf4829ed5f69130b19e3412653 Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Fri, 19 Apr 2024 15:46:02 +0200 Subject: [PATCH 4/5] include distance in readme example --- README.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index cb84fbb..d972af7 100644 --- a/README.rst +++ b/README.rst @@ -53,6 +53,8 @@ Basic usage ... vroom.Job(1616, location=2), ... vroom.Job(1717, location=3)]) + >>> problem_instance.set_geometry() + >>> solution = problem_instance.solve(exploration_level=5, nb_threads=4) >>> solution.summary.cost @@ -63,7 +65,7 @@ Basic usage 'waiting_time', 'location_index', 'id', 'description'], dtype='object') - >>> solution.routes[["vehicle_id", "type", "arrival", "location_index", "id"]] + >>> solution.routes[["vehicle_id", "type", "arrival", "location_index", "id", "distance"]] vehicle_id type arrival location_index id 0 47 start 0 0 1 47 job 2104 1 1515 @@ -96,7 +98,7 @@ Usage with a routing engine >>> sol = problem_instance.solve(exploration_level=5, nb_threads=4) >>> print(sol.summary.duration) - 2698 + 2704 Installation ------------ From bd5f0f332bb2b670d1848020393b824e94ab6e15 Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Fri, 19 Apr 2024 16:14:19 +0200 Subject: [PATCH 5/5] fix set_geometry --- README.rst | 4 +--- src/vroom/input/input.py | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index d972af7..edaf6e8 100644 --- a/README.rst +++ b/README.rst @@ -53,8 +53,6 @@ Basic usage ... vroom.Job(1616, location=2), ... vroom.Job(1717, location=3)]) - >>> problem_instance.set_geometry() - >>> solution = problem_instance.solve(exploration_level=5, nb_threads=4) >>> solution.summary.cost @@ -65,7 +63,7 @@ Basic usage 'waiting_time', 'location_index', 'id', 'description'], dtype='object') - >>> solution.routes[["vehicle_id", "type", "arrival", "location_index", "id", "distance"]] + >>> solution.routes[["vehicle_id", "type", "arrival", "location_index", "id"]] vehicle_id type arrival location_index id 0 47 start 0 0 1 47 job 2104 1 1515 diff --git a/src/vroom/input/input.py b/src/vroom/input/input.py index d989ad1..26bc509 100644 --- a/src/vroom/input/input.py +++ b/src/vroom/input/input.py @@ -107,7 +107,7 @@ def from_json( if geometry is None: geometry = servers is not None if geometry: - self._set_geometry() + self._set_geometry(True) instance = Input(servers=servers, router=router) with open(filepath) as handle: instance._from_json(handle.read(), geometry) @@ -115,7 +115,7 @@ def from_json( def set_geometry(self): self._geometry = True - return self._set_geometry() + return self._set_geometry(True) def set_amount_size(self, *amount_sizes: int) -> None: """Add amount sizes."""