Python Mathematical Modeling PuLP Library Linear Programming Practical Case Programming Detailed Explanation
- 2021-12-09 09:31:06
- OfStack
1. Problem description
A factory produces two kinds of beverages, A and B, and every 100 boxes of A beverages need 6 kg of raw materials and 10 workers, with a profit of 100,000 yuan; Every 100 boxes of B drinks need 5 kg of raw materials and 20 workers, with a profit of 90,000 yuan.
Today, the factory has 60 kg of raw materials and 150 workers, and the output of A beverage is not more than 800 boxes due to other conditions.
(1) Ask how to arrange the production plan, that is, how much is produced for each of the two beverages to maximize the profit?
(2) If the investment of RMB 8,000 can increase the raw material by 1 kg, should this investment be made? How much is reasonable to invest?
(3) If the profit per 100 cases of A drinks can increase by 10,000 yuan, should the production plan be changed?
(4) If the profit per 100 boxes of A drinks can increase by 10,000 yuan, and if the investment of 0.8 million yuan can increase the raw materials by 1 kg, should this investment be made? How much is reasonable to invest?
(5) If loose boxes are not allowed (produced according to the whole hundred boxes), how to arrange the production plan, that is, how much is produced for each of the two beverages to maximize the profit?
2. Solving linear programming with PuLP library
2.1 Question 1
(1) Mathematical modeling
Problem modeling:
Decision variables:
x1: A beverage output (unit: 100 boxes)
x2: Output of B beverage (unit: 100 boxes)
Objective function:
max fx = 10*x1 + 9*x2
Constraints:
6*x1 + 5*x2 < = 60
10*x1 + 20*x2 < = 150
Value range:
Given conditions: x1, x2 > = 0, x1 < = 8
Derivation conditions: x1, x2 > = 0 and 10*x1+20*x2 < = 150: 0 < =x1 < = 15; 0 < =x2 < =7.5
Therefore, 0 < = x1 < = 8, 0 < = x2 < =7.5
(2) Python programming
import pulp # Import pulp Library
ProbLP1 = pulp.LpProblem("ProbLP1", sense=pulp.LpMaximize) # Definition problem 1 Find the maximum value
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # Definition x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # Definition x2
ProbLP1 += (10*x1 + 9*x2) # Set the target function f(x)
ProbLP1 += (6*x1 + 5*x2 <= 60) # Inequality constraint
ProbLP1 += (10*x1 + 20*x2 <= 150) # Inequality constraint
ProbLP1.solve()
print(ProbLP1.name) # Output solution status
print("Status:", pulp.LpStatus[ProbLP1.status]) # Output solution status
for v in ProbLP1.variables():
print(v.name, "=", v.varValue) # Output the optimal value of each variable
print("F1(x)=", pulp.value(ProbLP1.objective)) # Output the objective function value of the optimal solution
# = Attention Youcans Share the original series https://blog.csdn.net/youcans =
(3) Running results
ProbLP1
x1=6.4285714
x2=4.2857143
F1(X)=102.8571427
2.2 Question 2
(1) Mathematical modeling(2) Python programmingProblem modeling:
Decision variables:
x1: A beverage output (unit: 100 boxes)
x2: Output of B beverage (unit: 100 boxes)
x3: Increase investment (unit: 10,000 yuan)
Objective function:
max fx = 10*x1 + 9*x2-x3
Constraints:
6*x1 + 5*x2 < = 60 + x3/0.8
10*x1 + 20*x2 < = 150
Value range:
Given conditions: x1, x2 > = 0, x1 < = 8
Derivation conditions: x1, x2 > = 0 and 10*x1+20*x2 < = 150: 0 < =x1 < = 15; 0 < =x2 < =7.5
Therefore, 0 < = x1 < = 8, 0 < = x2 < =7.5
import pulp # Import pulp Library
ProbLP2 = pulp.LpProblem("ProbLP2", sense=pulp.LpMaximize) # Definition problem 2 Find the maximum value
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # Definition x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # Definition x2
x3 = pulp.LpVariable('x3', cat='Continuous') # Definition x3
ProbLP2 += (10*x1 + 9*x2 - x3) # Set the target function f(x)
ProbLP2 += (6*x1 + 5*x2 - 1.25*x3 <= 60) # Inequality constraint
ProbLP2 += (10*x1 + 20*x2 <= 150) # Inequality constraint
ProbLP2.solve()
print(ProbLP2.name) # Output solution status
print("Status:", pulp.LpStatus[ProbLP2.status]) # Output solution status
for v in ProbLP2.variables():
print(v.name, "=", v.varValue) # Output the optimal value of each variable
print("F2(x)=", pulp.value(ProbLP2.objective)) # Output the objective function value of the optimal solution
(3) Running results
ProbLP2
x1=8.0
x2=3.5
x3=4.4
F2(X)=107.1
2.3 Question 3
(1) Mathematical modeling(2) Python programmingProblem modeling:
Decision variables:
x1: A beverage output (unit: 100 boxes)
x2: Output of B beverage (unit: 100 cases)
Objective function:
max fx = 11*x1 + 9*x2
Constraints:
6*x1 + 5*x2 < = 60
10*x1 + 20*x2 < = 150
Value range:
Given conditions: x1, x2 > = 0, x1 < = 8
Derivation conditions: x1, x2 > = 0 and 10*x1+20*x2 < = 150: 0 < =x1 < = 15; 0 < =x2 < =7.5
Therefore, 0 < = x1 < = 8, 0 < = x2 < =7.5
import pulp # Import pulp Library
ProbLP3 = pulp.LpProblem("ProbLP3", sense=pulp.LpMaximize) # Definition problem 3 Find the maximum value
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # Definition x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # Definition x2
ProbLP3 += (11 * x1 + 9 * x2) # Set the target function f(x)
ProbLP3 += (6 * x1 + 5 * x2 <= 60) # Inequality constraint
ProbLP3 += (10 * x1 + 20 * x2 <= 150) # Inequality constraint
ProbLP3.solve()
print(ProbLP3.name) # Output solution status
print("Status:", pulp.LpStatus[ProbLP3.status]) # Output solution status
for v in ProbLP3.variables():
print(v.name, "=", v.varValue) # Output the optimal value of each variable
print("F3(x) =", pulp.value(ProbLP3.objective)) # Output the objective function value of the optimal solution
(3) Running results
ProbLP3
x1=8.0
x2=2.4
F3(X) = 109.6
2.4 Question 4
(1) Mathematical modeling(2) Python programmingProblem modeling:
Decision variables:
x1: A beverage output (unit: 100 boxes)
x2: Output of B beverage (unit: 100 boxes)
x3: Increase investment (unit: 10,000 yuan)
Objective function:
max fx = 11*x1 + 9*x2-x3
Constraints:
6*x1 + 5*x2 < = 60 + x3/0.8
10*x1 + 20*x2 < = 150
Value range:
Given conditions: x1, x2 > = 0, x1 < = 8
Derivation conditions: x1, x2 > = 0 and 10*x1+20*x2 < = 150: 0 < =x1 < = 15; 0 < =x2 < =7.5
Therefore, 0 < = x1 < = 8, 0 < = x2 < =7.5
import pulp # Import pulp Library ProbLP4 = pulp.LpProblem("ProbLP4", sense=pulp.LpMaximize) # Definition problem 2 Find the maximum value
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # Definition x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # Definition x2
x3 = pulp.LpVariable('x3', cat='Continuous') # Definition x3
ProbLP4 += (11 * x1 + 9 * x2 - x3) # Set the target function f(x)
ProbLP4 += (6 * x1 + 5 * x2 - 1.25 * x3 <= 60) # Inequality constraint
ProbLP4 += (10 * x1 + 20 * x2 <= 150) # Inequality constraint
ProbLP4.solve()
print(ProbLP4.name) # Output solution status
print("Status:", pulp.LpStatus[ProbLP4.status]) # Output solution status
for v in ProbLP4.variables():
print(v.name, "=", v.varValue) # Output the optimal value of each variable
print("F4(x) = ", pulp.value(ProbLP4.objective)) # Output the objective function value of the optimal solution
# = Attention Youcans Share the original series https://blog.csdn.net/youcans =
(3) Running results
ProbLP4
x1=8.0
x2=3.5
x3=4.4
F4(X) = 115.1
2.5 Problem 5: Integer Programming Problem
(1) Mathematical modelingProblem modeling:
Decision variables:
x1: A beverage output, positive integer (unit: 100 boxes)
x2: Output of B beverage, positive integer (unit: 100 boxes)
Objective function:
max fx = 10*x1 + 9*x2
Constraints:
6*x1 + 5*x2 < = 60
10*x1 + 20*x2 < = 150
Value range:
Given conditions: x1, x2 > = 0, x1 < = 8, x1, x2 are integers
Derivation conditions: x1, x2 > = 0 and 10*x1+20*x2 < = 150: 0 < =x1 < = 15; 0 < =x2 < =7.5
Therefore, 0 < = x1 < = 8, 0 < = x2 < =7
Note: In this topic, the beverage vehicle is required to be a whole hundred boxes, that is, the decision variables x1 and x2 are integers, so it is an integer programming problem. PuLP provides integer programmed
(2) Python programming
import pulp # Import pulp Library
ProbLP5 = pulp.LpProblem("ProbLP5", sense=pulp.LpMaximize) # Definition problem 1 Find the maximum value
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Integer') # Definition x1 Variable type: Integer
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Integer') # Definition x2 Variable type: Integer
ProbLP5 += (10 * x1 + 9 * x2) # Set the target function f(x)
ProbLP5 += (6 * x1 + 5 * x2 <= 60) # Inequality constraint
ProbLP5 += (10 * x1 + 20 * x2 <= 150) # Inequality constraint
ProbLP5.solve()
print(ProbLP5.name) # Output solution status
print("Status:", pulp.LpStatus[ProbLP5.status]) # Output solution status
for v in ProbLP5.variables():
print(v.name, "=", v.varValue) # Output the optimal value of each variable
print("F5(x) =", pulp.value(ProbLP5.objective)) # Output the objective function value of the optimal solution
(3) Running results
ProbLP5
x1=8.0
x2=2.0
F5(X) = 98.0
The above is the Python mathematical modeling PuLP library linear programming actual case programming details, more about the PuLP library linear programming actual programming case information please pay attention to other related articles on this site!