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

Improve performance for large models #750

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
14 changes: 8 additions & 6 deletions pulp/apis/cplex_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ def buildSolverModel(self, lp):
lp.solverModel.objective.set_sense(
lp.solverModel.objective.sense.maximize
)
if lp.objective is None:
raise PulpSolverError("No objective set")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in theory it's possible to not set an objective function afaik and just model a constraint satisfaction problem. Isn't that the case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I added this because the next line calls get on lp.objective which will raise an exception if the objective is None which the type checker was saying was an option.

obj = [float(lp.objective.get(var, 0.0)) for var in model_variables]

def cplex_var_lb(var):
Expand Down Expand Up @@ -372,15 +374,16 @@ def cplex_var_types(var):
rows = []
senses = []
rhs = []
rownames = []
for name, constraint in lp.constraints.items():
rownames = list(lp.constraints.keys())
for constraint in lp.constraints.values():
# build the expression
expr = [(var.name, float(coeff)) for var, coeff in constraint.items()]
if not expr:
if len(constraint) == 0:
# if the constraint is empty
rows.append(([], []))
else:
rows.append(list(zip(*expr)))
expr1 = [var.name for var in constraint.keys()]
expr2 = [float(coeff) for coeff in constraint.values()]
rows.append((expr1, expr2))
if constraint.sense == constants.LpConstraintLE:
senses.append("L")
elif constraint.sense == constants.LpConstraintGE:
Expand All @@ -389,7 +392,6 @@ def cplex_var_types(var):
senses.append("E")
else:
raise PulpSolverError("Detected an invalid constraint type")
rownames.append(name)
rhs.append(float(-constraint.constant))
lp.solverModel.linear_constraints.add(
lin_expr=rows, senses=senses, rhs=rhs, names=rownames
Expand Down
Loading
Loading