Skip to content

Commit

Permalink
Disable _round_primal_to_bounds (#107)
Browse files Browse the repository at this point in the history
This requires a more accurate tolerance and should already be handled
by the solver
  • Loading branch information
KristianJensen authored Apr 27, 2017
1 parent aab6f90 commit 9ee8832
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 44 deletions.
9 changes: 0 additions & 9 deletions optlang/cplex_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,15 +721,6 @@ def _set_objective_direction(self, direction):
{'min': self.problem.objective.sense.minimize, 'max': self.problem.objective.sense.maximize}[
direction])

@property
def primal_values(self):
# round primals
primal_values = [variable._round_primal_to_bounds(primal)
for variable, primal in zip(self.variables, self._get_primal_values())]
return collections.OrderedDict(
zip(self._get_variables_names(), primal_values)
)

def _get_primal_values(self):
try:
primal_values = self.problem.solution.get_values()
Expand Down
7 changes: 0 additions & 7 deletions optlang/glpk_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,6 @@ def objective(self, value):
)
value.problem = self

@property
def primal_values(self):
# round primals
return collections.OrderedDict(
(var.name, var._round_primal_to_bounds(primal)) for var, primal in zip(self.variables, self._get_primal_values())
)

def _get_primal_values(self):
return get_col_primals(self.problem)

Expand Down
53 changes: 25 additions & 28 deletions optlang/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,8 @@ def primal(self):
"""The primal of variable (None if no solution exists)."""
if self.problem:
primal = self._get_primal()
if primal is not None:
if self.type in ("integer", "binary"):
primal = round(primal)
if self.problem.status == OPTIMAL:
primal = self._round_primal_to_bounds(primal)
if primal is not None and self.type in ("integer", "binary"):
primal = round(primal)
return primal
else:
return None
Expand Down Expand Up @@ -368,29 +365,29 @@ def from_json(cls, json_obj):
"""
return cls(json_obj["name"], lb=json_obj["lb"], ub=json_obj["ub"], type=json_obj["type"])

def _round_primal_to_bounds(self, primal, tolerance=1e-5):
"""Rounds primal value to lie within variables bounds.
Raises if exceeding threshold.
Parameters
----------
primal : float
The primal value to round.
tolerance : float (optional)
The tolerance threshold (default: 1e-5).
"""
if (self.lb is None or primal >= self.lb) and (self.ub is None or primal <= self.ub):
return primal
else:
if (primal <= self.lb) and ((self.lb - primal) <= tolerance):
return self.lb
elif (primal >= self.ub) and ((self.ub - primal) >= -tolerance):
return self.ub
else:
raise AssertionError(
'The primal value %s returned by the solver is out of bounds for variable %s (lb=%s, ub=%s)' % (
primal, self.name, self.lb, self.ub))
# def _round_primal_to_bounds(self, primal, tolerance=1e-5):
# """Rounds primal value to lie within variables bounds.
#
# Raises if exceeding threshold.
#
# Parameters
# ----------
# primal : float
# The primal value to round.
# tolerance : float (optional)
# The tolerance threshold (default: 1e-5).
# """
# if (self.lb is None or primal >= self.lb) and (self.ub is None or primal <= self.ub):
# return primal
# else:
# if (primal <= self.lb) and ((self.lb - primal) <= tolerance):
# return self.lb
# elif (primal >= self.ub) and ((self.ub - primal) >= -tolerance):
# return self.ub
# else:
# raise AssertionError(
# 'The primal value %s returned by the solver is out of bounds for variable %s (lb=%s, ub=%s)' % (
# primal, self.name, self.lb, self.ub))


# noinspection PyPep8Naming
Expand Down

0 comments on commit 9ee8832

Please sign in to comment.