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

Fix #140 – indicator constraints not being removed #144

Merged
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
14 changes: 11 additions & 3 deletions optlang/cplex_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ def get_linear_coefficients(self, variables):
def _get_expression(self):
if self.problem is not None:
cplex_problem = self.problem.problem
cplex_row = cplex_problem.linear_constraints.get_rows(self.name)
try:
cplex_row = cplex_problem.linear_constraints.get_rows(self.name)
except CplexSolverError as e:
if 'CPLEX Error 1219:' not in str(e):
raise e
else:
cplex_row = cplex_problem.indicator_constraints.get_linear_components(self.name)
variables = self.problem._variables
expression = add(
[mul((symbolics.Real(cplex_row.val[i]), variables[ind])) for i, ind in
Expand Down Expand Up @@ -640,7 +646,7 @@ def __init__(self, problem=None, *args, **kwargs):
)
try:
objective_name = self.problem.objective.get_name()
except cplex.exceptions.CplexSolverError as e:
except CplexSolverError as e:
if 'CPLEX Error 1219:' not in str(e):
raise e
else:
Expand Down Expand Up @@ -885,7 +891,9 @@ def _add_constraints(self, constraints, sloppy=False):
def _remove_constraints(self, constraints):
super(Model, self)._remove_constraints(constraints)
for constraint in constraints:
if constraint.is_Linear:
if constraint.indicator_variable is not None:
self.problem.indicator_constraints.delete(constraint.name)
elif constraint.is_Linear:
self.problem.linear_constraints.delete(constraint.name)
elif constraint.is_Quadratic:
self.problem.quadratic_constraints.delete(constraint.name)
Expand Down
2 changes: 0 additions & 2 deletions optlang/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,6 @@ def _add_constraints(self, constraints, sloppy=False):
constraint._problem = self

def _remove_constraints(self, constraints):
for constraint in constraints: # TODO: remove this hack that fixes problems with lazy solver expressions.
constraint.expression
keys = [constraint.name for constraint in constraints]
if len(constraints) > 350: # Need to figure out a good threshold here
self._constraints = self._constraints.fromkeys(set(self._constraints.keys()).difference(set(keys)))
Expand Down
6 changes: 6 additions & 0 deletions optlang/tests/abstract_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ def test_indicator_constraint_support(self):
model = self.interface.Model()
model.add(constraint)
model.update()
self.assertEqual(constraint.problem, model)
self.assertIn(constraint, model.constraints)
model.remove(constraint.name)
model.update()
self.assertEqual(constraint.problem, None)
self.assertNotIn(constraint, model.constraints)
else:
self.assertRaises(
optlang.exceptions.IndicatorConstraintsNotSupported,
Expand Down