Skip to content

Commit

Permalink
Fix recursion error (#42)
Browse files Browse the repository at this point in the history
* fix: use sloppy keyword on Objective constructor

If sloppy, expressions will not be validated.

* chore: make is_Linear faster for canonical quadratic expressions

* fix: pep8
  • Loading branch information
KristianJensen authored Nov 15, 2016
1 parent 0e446f3 commit 9b80766
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions optlang/cplex_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ def __iadd__(self, other):

@six.add_metaclass(inheritdocstring)
class Objective(interface.Objective):
def __init__(self, *args, **kwargs):
super(Objective, self).__init__(*args, **kwargs)
def __init__(self, expression, sloppy=False, **kwargs):
super(Objective, self).__init__(expression, **kwargs)
self._expression_expired = False
if not (self.is_Linear or self.is_Quadratic):
if not (sloppy or self.is_Linear or self.is_Quadratic):
raise ValueError("Cplex only supports linear and quadratic objectives.")

@property
Expand Down
6 changes: 3 additions & 3 deletions optlang/glpk_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ def set_linear_coefficients(self, coefficients):

@six.add_metaclass(inheritdocstring)
class Objective(interface.Objective):
def __init__(self, *args, **kwargs):
super(Objective, self).__init__(*args, **kwargs)
def __init__(self, expression, sloppy=False, **kwargs):
super(Objective, self).__init__(expression, **kwargs)
self._expression_expired = False
if not self.is_Linear:
if not (sloppy or self.is_Linear):
raise ValueError(
"GLPK only supports linear objectives. %s is not linear." % self)

Expand Down
2 changes: 1 addition & 1 deletion optlang/gurobi_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class Objective(interface.Objective):
def __init__(self, expression, sloppy=False, *args, **kwargs):
super(Objective, self).__init__(expression, *args, **kwargs)
self._expression_expired = False
if not sloppy and not self.is_Linear: # or self.is_Quadratic: # QP is not yet supported
if not (sloppy or self.is_Linear): # or self.is_Quadratic: # QP is not yet supported
raise ValueError("The given objective is invalid. Must be linear or quadratic (not yet supported")

@property
Expand Down
14 changes: 8 additions & 6 deletions optlang/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,15 @@ def _canonicalize(self, expression):
def is_Linear(self):
"""Returns True if expression is linear (a polynomial with degree 1 or 0) (read-only)."""
coeff_dict = self.expression.as_coefficients_dict()
if all((len(key.free_symbols) < 2 and (key.is_Add or key.is_Mul or key.is_Atom) for key in coeff_dict.keys())):
return True
else:
poly = self.expression.as_poly(*self.variables)
if poly is not None:
return poly.is_linear
for key in coeff_dict.keys():
if len(key.free_symbols) < 2 and (key.is_Add or key.is_Mul or key.is_Atom):
pass
else:
return False
if key.is_Pow and key.args[1] != 1:
return False
else:
return True

@property
def is_Quadratic(self):
Expand Down Expand Up @@ -1464,6 +1465,7 @@ def from_json(cls, json_obj):
model.update()
return model


if __name__ == '__main__':
# Example workflow

Expand Down

0 comments on commit 9b80766

Please sign in to comment.