Skip to content

Commit

Permalink
Small fixes (#68)
Browse files Browse the repository at this point in the history
* fix: bug when changing variable names

Only update the _variables_to_constriants_mapping dict when new_name !=
old_name

* feat: allow infeasible primals to be retrieved

Only _round_primals_to_bounds if solution is optimal

* feat: add a tolerance option to scipy_interface

Default is set to 1e-9
  • Loading branch information
KristianJensen authored Feb 6, 2017
1 parent 02a3300 commit f94090e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion optlang/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ def primal(self):
if primal is not None:
if self.type in ("integer", "binary"):
primal = round(primal)
primal = self._round_primal_to_bounds(primal)
if self.problem.status == OPTIMAL:
primal = self._round_primal_to_bounds(primal)
return primal
else:
return None
Expand Down
17 changes: 13 additions & 4 deletions optlang/scipy_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ def get_constraint_slack(self, name):
index = self._get_constraint_index(name)
return self._slacks[index]

def optimize(self, method="simplex", verbosity=False, **kwargs):
def optimize(self, method="simplex", verbosity=False, tolerance=1e-9, **kwargs):
"""Run the linprog function on the problem. Returns None."""
c = np.array([self.objective.get(name, 0) for name in self._variables])
if self.direction == "max":
c *= -1

bounds = list(six.itervalues(self.bounds))
solution = linprog(c, self.A, self.upper_bounds, bounds=bounds, method=method,
options={"maxiter": 10000, "disp": verbosity}, **kwargs)
options={"maxiter": 10000, "disp": verbosity, "tol": tolerance}, **kwargs)
self._solution = solution
self._status = solution.status
if SCIPY_STATUS[self._status] == interface.OPTIMAL:
Expand Down Expand Up @@ -484,9 +484,10 @@ def set_linear_coefficients(self, coefficients):

@six.add_metaclass(inheritdocstring)
class Configuration(interface.MathematicalProgrammingConfiguration):
def __init__(self, verbosity=0, *args, **kwargs):
def __init__(self, verbosity=0, tolerance=1e-9, *args, **kwargs):
super(Configuration, self).__init__(*args, **kwargs)
self._verbosity = verbosity
self.tolerance = tolerance

@property
def verbosity(self):
Expand Down Expand Up @@ -514,6 +515,14 @@ def timeout(self, value):
if value is not None:
raise ValueError("Scipy interface does not support timeout")

@property
def tolerance(self):
return self._tolerance

@tolerance.setter
def tolerance(self, value):
self._tolerance = value


@six.add_metaclass(inheritdocstring)
class Model(interface.Model):
Expand Down Expand Up @@ -578,7 +587,7 @@ def _remove_constraints(self, constraints):
super(Model, self)._remove_constraints(constraints)

def _optimize(self):
self.problem.optimize(verbosity=bool(self.configuration.verbosity))
self.problem.optimize(verbosity=bool(self.configuration.verbosity), tolerance=self.configuration.tolerance)
status = self.problem.status
return status

Expand Down

0 comments on commit f94090e

Please sign in to comment.