LP 3 - Solving MILPs With PuLP
LP 3 - Solving MILPs With PuLP
Rob Broekmeulen
2 LP with Pulp
1
2
25-2-2024
If GLPK (or GLPK_CMD) is not listed, run the following command from the Anaconda
Prompt:
conda install –c conda-forge glpk
3 LP with Pulp
2. Identify the decision variables and identify any upper or lower bounds.
x1 = LpVariable(name="x1", lowBound=0)
x2 = LpVariable(name="x2", lowBound=0)
4 LP with Pulp
2
4
25-2-2024
5 LP with Pulp
Now, we can solve the model (with GLPK) and check the status:
model.solve(GLPK(msg=False))
print(f"Status: {model.status}, {LpStatus[model.status]}")
Variables (values):
print("Variables")
for var in model.variables():
print(f"{var.name}: {var.value()}")
Constraints (slack):
print("Constraints: slack")
for name, constraint in model.constraints.items():
print(f"{name}: {constraint.value()}")
For the full listing of the code, see lp_example_base.py
7 LP with Pulp
∀𝒊: 𝒙𝒊 ≥ 𝟎
Instead of adding variables and constraints individually, we can use the list data structure and
the lpSum function of PuLP.
In the Blue Ridge problem, we have the following data (as lists):
• A = [[1, 1], [6, 9], [12, 16]]
• B = [200, 1560, 2880]
• C = [-350, -300] (profits are negative costs).
8 Introduction LP
4
8
25-2-2024
9 LP with Pulp
2. Identify the decision variables and identify any upper or lower bounds.
x = {i: LpVariable(name=f"x{i}", lowBound=0) for i in set_i}
Note that we have now created x0 and x1, instead of x1 and x2 (Python convention).
An alternative way to create a dictionary of variables is:
x = LpVariable.dicts("x", indices=set_i, lowBound=0)
10 LP with Pulp
5
10
25-2-2024
11 LP with Pulp
11
VARIABLES
x0 Continuous
x1 Continuous
12 LP with Pulp
6
12
25-2-2024
With these variables y and the original x2, we can create a new semi-continuous variable with
a minimum of 100 Hydro-Luxes (minimal order quantity), by adding the following two
constraints to the non-canonical model:
BIGM = 200
model += (100*y-x2 <= 0, “lowSC")
model += (x2-BIGM*y <= 0, “highSC")
The value 200 for BIGM is sufficient, due to the limited number of pumps.
13 LP with Pulp
13
• Do not enter the Branch & Bound phase (relaxed solution only): --nomip
14 LP with Pulp
7
14