PuLP : getting started

PuLP installation:

pip install pulp

Simple PuLP Model (bonds_simple-PuLP.py)

$$ \begin{align} \max_{x_1,x_2} & \quad 4 x_1 + 3 x_2 \\ \text{s.t.} & \quad x_1 + x_2 \leq 100 \\ & \quad 2 x_1 + x_2 \leq 150 \\ & \quad 3 x_1 + 4 x_2 \leq 360 \\ & \quad x_1, x_2 \geq 0 \end{align} $$

In [ ]:
from pulp import LpProblem, LpVariable, lpSum, LpMaximize, value

In [ ]:
prob = LpProblem("Dedication Model", LpMaximize)

In [ ]:
X1 = LpVariable("X1", 0, None)
X2 = LpVariable("X2", 0, None)

In [ ]:
# Objective function
prob += 4*X1 + 3*X2              # Objectives are nothing more than expressions without a right hand side

# Constraints
prob +=   X1 +   X2 <= 100
prob += 2*X1 +   X2 <= 150
prob += 3*X1 + 4*X2 <= 360

In [ ]:
prob.solve()

In [ ]:
print("Optimal total cost is: ", value(prob.objective))

In [ ]:
print("X1 :", X1.varValue)
print("X2 :", X2.varValue)