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

Disable _round_primal_to_bounds #107

Merged
merged 1 commit into from
Apr 27, 2017
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
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