I'm trying to solve a MILP problem using PYOMO and gurobi solvers but I'm not sure about the formulation of my code. Can I have an example (code) of how to solve a simple MILP problem please ?
Thank you in advance
I'm trying to solve a MILP problem using PYOMO and gurobi solvers but I'm not sure about the formulation of my code. Can I have an example (code) of how to solve a simple MILP problem please ?
Thank you in advance
You can find many of those online!
Here is one:
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
model = pyo.ConcreteModel()
# define variables
model.x = pyo.Var(within=Integers, bounds=(0,10))
model.y = pyo.Var(bounds=(0,10))
# define objective: maximize x + y
model.obj = pyo.Objective(expr= model.x+model.y, sense=maximize)
# define constraints
model.C1 = pyo.Constraint(expr= -model.x+2*model.y<=7)
model.C2 = pyo.Constraint(expr= 2*model.x+model.y<=14)
model.C3 = pyo.Constraint(expr= 2*model.x-model.y<=10)
# solve with gurobi
opt = SolverFactory('gurobi')
opt.solve(model)
And print the result:
print(pyo.value(model.obj))
Gives
>>> 9.5
Here you can find more examples, specifically for pyomo.
Good luck! :)