-2

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

1 Answers1

0

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! :)

henniedh
  • 38
  • 7
  • Hello, Thank you very much for your help. In fact, I want to use 1 variable (within = binary). Therefore, in your example, I want to use the binary variable so that either x or y is 0 or 1 to find the optimal solution. Instead of using the integer domain, I want to use the binary domain. Can you please help me about how your example should be written (code) with binary domains ? Thank you in advance. – Gopee Yanandlall Mar 03 '22 at 15:07
  • Yes, you can change `within=Integer` to `within=Binary`, that's all – henniedh Mar 04 '22 at 15:20