Solving a linear programming problem with PuLP


In [1]:
import pulp

Objective function

$$z(max) = 5x_1 + 4x_2$$

Constraints:

  • $C_1 = x_1 + x_2 \leq 5$
  • $C_2 = 10*x_1 + 6*x_2 \leq 45$
  • $x_1,x_2 \geq 0$

In [2]:
z = pulp.LpProblem('problem x', pulp.LpMaximize)
x1 = pulp.LpVariable('x1', lowBound=0)
x2 = pulp.LpVariable('x2', lowBound=0)

In [3]:
z += 5*x1 + 4*x2

In [4]:
z += 1*x1 + 1*x2 <= 5
z += 10*x1 + 6*x2 <= 45

In [5]:
z


Out[5]:
problem x:
MAXIMIZE
5*x1 + 4*x2 + 0
SUBJECT TO
_C1: x1 + x2 <= 5

_C2: 10 x1 + 6 x2 <= 45

VARIABLES
x1 Continuous
x2 Continuous

In [6]:
pulp.LpStatus[z.solve()]


Out[6]:
'Optimal'

In [7]:
pulp.value(x1), pulp.value(x1), pulp.value(z.objective)


Out[7]:
(3.75, 3.75, 23.75)