Skip to content

Commit

Permalink
trying it out
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathf committed Apr 19, 2024
1 parent e8fdeed commit 7431231
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
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 @@ -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"));
Expand Down
10 changes: 9 additions & 1 deletion 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,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:
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 7431231

Please sign in to comment.