Use Gurobi in Python to Solve Integer Programming Problems

Install the Gurobi library

This package only allows solving problems on a limited scale.

1
pip install gurobipy

Then, import it as follows:

1
import gurobipy as grb

Example

Max:2xyMax:2x-y

S.T.

x+2y<=204x+5y<=10x+2y>=5x>=0,Integery>=0,Integerx+2y<=20\\ -4x+5y<=10\\ -x+2y>=-5\\ x>=0, Integer\\ y>=0, Integer


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import gurobipy as grb

# Build a model
model = grb.Model('test')

# Define decision variables
x = model.addVar(vtype=grb.GRB.INTEGER, name="x")
y = model.addVar(vtype=grb.GRB.INTEGER, name="y")

# Add constrains
model.addConstr(x + 2 * y <= 20)
model.addConstr(-4 * x + 5 * y <= 10)
model.addConstr(-x + 2 * y >= -5)
model.addConstr(x >= 0)
model.addConstr(y >= 0)

# Set the objective function
model.setObjective(2 * x - y, sense=grb.GRB.MAXIMIZE)

# Solve the integer programming model
model.optimize()

print("\nThe value of the objective function: ", model.objVal)
for i in model.getVars():
print(i.varname, " = ", i.x)

The program output is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Optimize a model with 5 rows, 2 columns and 8 nonzeros
Model fingerprint: 0x4a44dc53
Variable types: 0 continuous, 2 integer (0 binary)
Coefficient statistics:
Matrix range [1e+00, 5e+00]
Objective range [1e+00, 2e+00]
Bounds range [0e+00, 0e+00]
RHS range [5e+00, 2e+01]
Found heuristic solution: objective 10.0000000
Presolve removed 2 rows and 0 columns
Presolve time: 0.00s
Presolved: 3 rows, 2 columns, 6 nonzeros
Variable types: 0 continuous, 2 integer (0 binary)
Found heuristic solution: objective 11.0000000

Root relaxation: objective 2.050000e+01, 1 iterations, 0.00 seconds (0.00 work units)

Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time

0 0 20.50000 0 1 11.00000 20.50000 86.4% - 0s
H 0 0 20.0000000 20.50000 2.50% - 0s
0 0 20.50000 0 1 20.00000 20.50000 2.50% - 0s

Explored 1 nodes (1 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 12 (of 12 available processors)

Solution count 3: 20 11 10

Optimal solution found (tolerance 1.00e-04)
Best objective 2.000000000000e+01, best bound 2.000000000000e+01, gap 0.0000%

The value of the objective function: 20.0
x = 12.0
y = 4.0

Alternatively, we can use matrices to represent the parameters of the model, as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import gurobipy as grb

# Build a model
model = grb.Model('test')

# Define decision variables
x = model.addVars(2, lb=0, ub=grb.GRB.INFINITY, vtype=grb.GRB.INTEGER, name='x')

# Build parameter matrices
obj_paras = [2, -1]
var_paras = [[1, 2], [-4, 5], [1, -2]]
r = [20, 10, 5]

# Add constrains
model.addConstrs(x.prod(var_paras[i]) <= r[i] for i in range(3))

# Set the objective function
model.setObjective(x.prod(obj_paras), grb.GRB.MAXIMIZE)

# Solve the integer programming model
model.optimize()

print("\nThe value of the objective function: ", model.objVal)
for i in model.getVars():
print(i.varname, " = ", i.x)

The program output is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Optimize a model with 3 rows, 2 columns and 6 nonzeros
Model fingerprint: 0x7252d5fe
Variable types: 0 continuous, 2 integer (0 binary)
Coefficient statistics:
Matrix range [1e+00, 5e+00]
Objective range [1e+00, 2e+00]
Bounds range [0e+00, 0e+00]
RHS range [5e+00, 2e+01]
Found heuristic solution: objective 10.0000000
Presolve time: 0.00s
Presolved: 3 rows, 2 columns, 6 nonzeros
Variable types: 0 continuous, 2 integer (0 binary)
Found heuristic solution: objective 11.0000000

Root relaxation: objective 2.050000e+01, 1 iterations, 0.00 seconds (0.00 work units)

Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time

0 0 20.50000 0 1 11.00000 20.50000 86.4% - 0s
H 0 0 20.0000000 20.50000 2.50% - 0s
0 0 20.50000 0 1 20.00000 20.50000 2.50% - 0s

Explored 1 nodes (1 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 12 (of 12 available processors)

Solution count 3: 20 11 10

Optimal solution found (tolerance 1.00e-04)
Best objective 2.000000000000e+01, best bound 2.000000000000e+01, gap 0.0000%

The value of the objective function: 20.0
x[0] = 12.0
x[1] = 4.0