[PYTHON] Linear Programming with PuLP

I'm studying nonlinear programming to understand the kernel and so on. Before that, I wanted to actually move my hands with linear programming, so I tried using PuLP, which can be used free of charge.

This time, I solved the example in Chapter 6 of “Optimization Mathematics You Can Understand”, Linear Programming.

There are several similar examples, so I picked them up and modeled them with PuLP.

Example 6.2

Use machines M1 and M2 to make two types of containers A and B. You need to use M1 for 2 minutes and machine M2 for 4 minutes to make one container A. On the other hand, make one container B. However, it is necessary to use machine M1 for 8 minutes and machine M2 for 4 minutes. The profits for making containers A and B are 29 yen and 45 yen, respectively. How to plan to maximize the profits Should I do it?

When the profit is modeled by asking the objective function f and the number of containers by asking x and y, respectively, the formula is as follows. Constraints: 2x + 8y <= 60 4x + 4y <= 60 x >= 0, y >= 0

Objective function: f = 29x + 45y → max

So, below is the code that can solve these formulas by plunging them into PuLP. Since the number of containers is calculated this time, the solution is an integer and the variable is specified by Integer.

#Creat variables
x = pulp.LpVariable("x", cat = "Integer")
y = pulp.LpVariable("y", cat = "Integer")

#Create object function
problem = pulp.LpProblem("Container", pulp.LpMaximize)
problem += 29*x + 45*y  #(6.6)

#constrained condition
problem += 2*x + 8*y <= 60 #(6.4)
problem += 4*x + 4*y <= 60 #(6.4)
problem += x >= 0 #(6.5)
problem += y >= 0 #(6.5)

status = problem.solve()
print "Status", pulp.LpStatus[status]

print problem

print "Result"
print "x", x.value()
print "y", y.value()

Result is,

Status Optimal
Container:
MAXIMIZE
29*x + 45*y + 0
SUBJECT TO
_C1: 2 x + 8 y <= 60
_C2: 4 x + 4 y <= 60
_C3: x >= 0
_C4: y >= 0

VARIABLES
x free Integer
y free Integer

Result
x 10.0
y 5.0

Example 6.3

Two types of alloys A and B are made using two types of metals M1 and M2. Alloys A and B have a profit of 30,000 yen and 25,000 yen per ton, respectively. Alloy A mixes metals M1 and M2 in a ratio of 1: 1 and alloy B mixes metals M1 and M2 in a ratio of 1: 3. Metals M1 and M2 can be supplied at 10 tonnes and 15 tonnes per day, respectively. How to plan to make alloys A and B to maximize profits.

When modeled, Constraints: 0.5x + 0.25y <= 10 0.5x + 0.75y <= 15 x >= 0, y >= 0

Objective function: f = 30x + 25y → max

This time, in coding, the solution is a continuous value, so the variable is specified as Continuous.

#Creat variables
x = pulp.LpVariable("x", cat = "Continuous")
y = pulp.LpVariable("y", cat = "Continuous")

#Create object function
problem = pulp.LpProblem("Alloy", pulp.LpMaximize)
problem += 30*x + 25*y  #(6.9)

#constrained condition
problem += 0.5*x + 0.25*y <= 10 #(6.7)
problem += 0.5*x + 0.75*y <= 15 #(6.7)
problem += x >= 0 #(6.8)
problem += y >= 0 #(6.8)

status = problem.solve()
print "Status", pulp.LpStatus[status]

print problem

print "Result"
print "x", x.value()
print "y", y.value()

Result is,

Status Optimal
Alloy:
MAXIMIZE
30*x + 25*y + 0
SUBJECT TO
_C1: 0.5 x + 0.25 y <= 10
_C2: 0.5 x + 0.75 y <= 15
_C3: x >= 0
_C4: y >= 0

VARIABLES
x free Continuous
y free Continuous

Result
x 15.0
y 10.0

Example 6.7

If the objective function diverges and the optimal solution does not exist as in this example, "Status Unbounded" is displayed as shown below.

Constraints: -x - y <= -1 -2x + y <= 1 x - 2y <= 1 x >= 0, y >= 0

Objective function: f = x + y → max

#Creat variables
a = pulp.LpVariable("a", cat = "Continuous")
b = pulp.LpVariable("b", cat = "Continuous")

#Create object function
problem = pulp.LpProblem("Test", pulp.LpMaximize)
problem += a + b  #(6.16)

#constrained condition
problem += -a - b <= -2 #(6.14)
problem += -2*a + b <= 15 #(6.14)
problem += a - 2*b <= 15 #(6.14)
problem += a >= 0 #(6.15)
problem += b >= 0 #(6.15)

status = problem.solve()
print "Status", pulp.LpStatus[status]

print problem

Status Unbounded

Recommended Posts

Linear Programming with PuLP
Linear programming + hands-on of pulp
[Mathematical optimization problem] Linear programming using PuLP
Getting started on how to solve linear programming problems with PuLP
Linear regression with statsmodels
3. 3. AI programming with Python
Optimization learned with OR-Tools [Linear programming: multi-stage model]
Python programming with Atom
Competitive programming with python
Shader programming with pyOpenGL
Solve Sudoku with PuLP
Regression with linear model
Asynchronous programming with libev
Programming with Python Flask
Asynchronous programming with libev # 3
Optimization learned with OR-Tools [Linear programming: project management]
Optimization learned with OR-Tools [Linear programming: Let's refine oil]
Programming with Python and Tkinter
Try programming with a shell!
Try GUI programming with Hy
Coordinator and integer linear programming
Programming education game with SenseHAT
[Python] Linear regression with scikit-learn
Linear programming by Karmarkar's algorithm
Network programming with Python Scapy
Robust linear regression with scikit-learn
[Python] Object-oriented programming learned with Pokemon
Easy Python + OpenCV programming with Canopy
Linear regression with Student's t distribution
Programming for humans with a well-defined __repr__
Competitive programming with python Local environment settings
Algorithm learned with Python 9th: Linear search
Linear Algebra for Programming Chapter 4 (Eigenvalues, Eigenvectors)
Programming normally with Node-RED programming on Raspberry Pi 3
Do embedded programming with test-driven development with googletest
Sound programming with Go (super introductory level)
What you can do with programming skills