coin-or / pulp

A python Linear Programming API
http://coin-or.github.io/pulp/
Other
2.01k stars 376 forks source link

Add support for nonlinear variables #756

Closed Aliebc closed 2 weeks ago

Aliebc commented 3 weeks ago

Add support for nonlinear variable expressions

Currently, Pulp does not support nonlinear variable expressions, and it will raise an error for such expressions. ` import pulp as pl

x1 = pl.LpVariable("x1", lowBound=0, upBound=5) x2 = pl.LpVariable("x2", lowBound=0, upBound=5)

problem = pl.LpProblem("MILP", pl.LpMaximize)

problem += x1 x1 + x2 x2

problem += x1 + x2 <= 10

problem.solve()

for v in problem.variables(): print(v.name, v.varValue)

print("Obj: ", pl.value(problem.objective)) `

It gives the following error: TypeError: Non-constant expressions cannot be multiplied

However, GUROBI supports such expressions.

` from gurobipy import Model, GRB

model = Model("MILP")

x1 = model.addVar(vtype=GRB.CONTINUOUS, name="x1") x2 = model.addVar(vtype=GRB.CONTINUOUS, name="x2")

model.addConstr(x1 >= 0) model.addConstr(x1 <= 5) model.addConstr(x2 >= 0) model.addConstr(x2 <= 5)

model.setObjective(x1 x1 + x2 x2, GRB.MAXIMIZE)

model.addConstr(x1 + x2 <= 10, "c0")

model.optimize()

for v in model.getVars(): print('%s %g' % (v.varName, v.x))

print('Obj: %g' % model.objVal) `

Can Pulp support such nonlinear variable expressions with GUROBI?

pchtsp commented 3 weeks ago

Hello! That's a good point. Unfortunately we try to keep pulp valid for many solvers and sometimes this means not being able to provide the full functionality of each solver.

As a workaround, you can build most of the problem in pulp and then manipulate the GUROBI model just before running, as explained here : https://coin-or.github.io/pulp/guides/how_to_configure_solvers.html#using-solver-specific-functionality

The example uses cplex but it's valid for the GUROBI solver

---------- Forwarded message --------- From: AX @.> Date: Sun, Jun 23, 2024, 04:37 Subject: [coin-or/pulp] Add support for nonlinear variables (Issue #756) To: coin-or/pulp @.> Cc: Subscribed @.***>

Add support for nonlinear variable expressions

Currently, Pulp does not support nonlinear variable expressions, and it will raise an error for such expressions. ` import pulp as pl

x1 = pl.LpVariable("x1", lowBound=0, upBound=5) x2 = pl.LpVariable("x2", lowBound=0, upBound=5)

problem = pl.LpProblem("MILP", pl.LpMaximize)

problem += x1 x1 + x2 x2

problem += x1 + x2 <= 10

problem.solve()

for v in problem.variables(): print(v.name, v.varValue)

print("Obj: ", pl.value(problem.objective)) `

It gives the following error: TypeError: Non-constant expressions cannot be multiplied

However, GUROBI supports such expressions.

` from gurobipy import Model, GRB

model = Model("MILP")

x1 = model.addVar(vtype=GRB.CONTINUOUS, name="x1") x2 = model.addVar(vtype=GRB.CONTINUOUS, name="x2")

model.addConstr(x1 >= 0) model.addConstr(x1 <= 5) model.addConstr(x2 >= 0) model.addConstr(x2 <= 5)

model.setObjective(x1 x1 + x2 x2, GRB.MAXIMIZE)

model.addConstr(x1 + x2 <= 10, "c0")

model.optimize()

for v in model.getVars(): print('%s %g' % (v.varName, v.x))

print('Obj: %g' % model.objVal) `

Can Pulp support such nonlinear variable expressions with GUROBI?

— Reply to this email directly, view it on GitHub https://github.com/coin-or/pulp/issues/756, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJUZ4YETRSU2I3NQEKGTQ3ZIYYFRAVCNFSM6AAAAABJX6CYESVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM3DQMJWGUYDCMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Aliebc commented 2 weeks ago

OK. Thank you very much!