Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change return type of Solver.solve from tuple to Solution instance #30

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ilpy/wrapper.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Solution:
def resize(self, size: int) -> None: ...
def get_value(self) -> float: ...
def set_value(self, value: float) -> None: ...
def get_status(self) -> str: ...

class Objective:
def __init__(self, size: int = 0) -> None: ...
Expand Down Expand Up @@ -93,4 +94,4 @@ class Solver:
def set_optimality_gap(self, gap: float, absolute: bool = False) -> None: ...
def set_num_threads(self, num_threads: int) -> None: ...
def set_verbose(self, verbose: bool) -> None: ...
def solve(self) -> tuple[Solution, str]: ...
def solve(self) -> Solution: ...
10 changes: 7 additions & 3 deletions ilpy/wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ cpdef enum Preference:
cdef class Solution:

cdef decl.Solution* p
cdef string _status

def __cinit__(self, size):
self.p = new decl.Solution(size)
self._status = ""

def __dealloc__(self):
del self.p
Expand All @@ -67,6 +69,9 @@ cdef class Solution:
def set_value(self, value):
self.p.setValue(value)

def get_status(self) -> str:
return self._status.decode("UTF-8")

cdef class Objective:

cdef decl.Objective* p
Expand Down Expand Up @@ -209,6 +214,5 @@ cdef class Solver:

def solve(self):
solution = Solution(self.num_variables)
cdef string message
deref(self.p).solve(solution.p[0], message)
return solution, message
deref(self.p).solve(solution.p[0], solution._status)
return solution
5 changes: 3 additions & 2 deletions tests/test_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def test_simple_solver(preference: ilpy.Preference, as_expression: bool) -> None
solver.set_objective(objective)
solver.add_constraint(constraint)

solution, _ = solver.solve()
solution = solver.solve()
assert isinstance(solution.get_status(), str)

assert solution[5] == 1

Expand Down Expand Up @@ -100,7 +101,7 @@ def test_quadratic_solver(preference: ilpy.Preference, as_expression: bool) -> N

solver.add_constraint(constraint)

solution, _ = solver.solve()
solution = solver.solve()

assert solution[5] == -2 # jan please check

Expand Down