Python Mathematical Modeling PuLP Library Linear Programming Advanced Dictionary based Detailed Explanation

  • 2021-12-09 09:32:57
  • OfStack

Directory 1, dictionary-based creation planning problem 2, linear programming problem case 3, model building (1) decision variables (2) objective function (3) constraint conditions (4) variable value range 4, PuLP program 1: use LpVariable to define variables one by one 5, PuLP program 2: use dict to define decision variables and constraints 6, Python program and running results

1. Dictionary-based creation planning

In the previous section, we introduced the use of LpVariable pairs to define each decision variable one by one, setting the name, type and upper and lower bounds. Similarly, we need to set model parameters one by one for constraints. In large-scale planning problems, it is very cumbersome and inefficient to define variables and set model parameters one by one. The Pulp library provides a shortcut that combines the loops and containers of the Python language and uses dictionaries to create questions.

-(1) Use a shortcut to create a planning problem. You can create multiple variables with dictionary type (dict), such as:

name = ['Scrap 1', 'Scrap 2', 'Scrap 3', 'Scrap 4', 'Nickel', 'Chromium', 'Molybdenum']
# A dictionary of the costs of each of the Ingredients is created
mass = pulp. LpVariable. dicts ("feedstock", material, lowBound = 0, cat = 'Continuous')

-(2) Use the dictionary type (dict) to set the parameters of the target function and constraint, such as:

cost = {
'Scrap 1': 16,
'Scrap 2': 10,
'Scrap 3': 8,
'Scrap 4': 9,
'Nickel': 48,
'Chromium': 60,
'Molybdenum': 53}

-(3) Use the traversal loop structure to set objective functions and constraints, such as:

AlloyModel + = pulp. lpSum ([cost [item] * mass [item] for item in material]), "Total Production Cost"
AlloyModel + = pulp. lpSum ([mass [item] for item in material]) = = 1000, "Quality Constraints"

2. Cases of linear programming problems

In this paper, taking the feeding problem of alloy steel production as an example, the shortcut method of creating problems based on list and dictionary is analyzed.

Problem description:
An iron and steel plant produces alloys that meet the chemical composition requirements by smelting recycled metal scrap and adding a certain amount of new materials. It is planned to produce 1000 kg of alloys.
The main component of all metal scrap is iron, and different metal scrap also contains various trace elements.
The content ratio, available quantity and unit cost of each component of metal waste and new materials are shown in the following table. The content requirements of each component in the generated alloy are also shown in the table.
How to arrange the feeding ratio to minimize the material cost under the condition of meeting the requirements of alloy component content?

材料 可用量 成本
废料1 0.80 18.0 12.0 0.0 75 16
废料2 0.70 3.2 1.1 0.1 250 10
废料3 0.85 0 0 0 不限 8
废料4 0.40 0 0 0 不限 9
0 100 0 0 不限 48
0 0 100 0 不限 60
0 0 0 100 不限 53
合金下限 0.65 3.0 1.0 1.1 / /
合金上限 0.75 3.5 1.2 1.3 / /

3. Build a model

(1) Decision variables

x1: Scrap 1 Amount (kg)
x2: Scrap 2 Amount (kg)
x3: Scrap 3 Amount (kg)
x4: Scrap 4 Amount (kg)
x5: Raw Nickel Content (kg)
x6: Chromium content (kg)
x7: Molybdenum dosage (kg)

(2) Objective function

min cost = 16*x1 + 10*x2 + 8*x3 + 9*x4 + 48*x5 + 60*x6 + 53*x7

(3) Constraints

0.8*x1 + 0.7*x2 + 0.85*x3 + 0.40*x4 > = 0.65*1000
0.8*x1 + 0.7*x2 + 0.85*x3 + 0.40*x4 < = 0.75*1000
18.0*x1 + 3.2*x2 + 100.0*x5 > = 3.0*1000
18.0*x1 + 3.2*x2 + 100.0*x5 < = 3.5*1000
12.0*x1 + 1.1*x2 + 100.0*x6 > = 1.0*1000
12.0*x1 + 1.1*x2 + 100.0*x6 > = 1.2*1000
0.1*x2 + 100.0*x7 > = 1.1*1000
0.1*x2 + 100.0*x7 > = 1.3*1000

(4) Variable value range

xi > = 0, i = 1, 2, … 7
x1 < = 75, x2 < = 250

4. PuLP Program 1: Defining variables one by one using LpVariable

This procedure is the same as the previous method, using LpVariable to define variables one by one. The complete program code is as follows:


    import pulp      #  Import  pulp Library 
    # 1. Establish optimization problems  AlloyLP:  Find the minimum (LpMinimize)
    AlloyLP = pulp.LpProblem(" Optimization of alloy production materials ", sense=pulp.LpMinimize)    #  Define the problem and find the minimum value 
    # 2. Define decision variables  x1~x7
    x1 = pulp.LpVariable(' Waste 1#', lowBound=0, upBound=75.0, cat='Continuous')  #  Definition  x1
    x2 = pulp.LpVariable(' Waste 2#', lowBound=0, upBound=250., cat='Continuous')  #  Definition  x2
    x3 = pulp.LpVariable(' Waste 3#', lowBound=0, cat='Continuous')  #  Definition  x3
    x4 = pulp.LpVariable(' Waste 4#', lowBound=0, cat='Continuous')  #  Definition  x4
    x5 = pulp.LpVariable(' Raw nickel ', lowBound=0, cat='Continuous')  #  Definition  x5
    x6 = pulp.LpVariable(' Raw material chromium ', lowBound=0, cat='Continuous')  #  Definition  x6
    x7 = pulp.LpVariable(' Molybdenum ', lowBound=0, cat='Continuous')  #  Definition  x7
    # 3. Define the objective function  cost
    AlloyLP += (16*x1 + 10*x2 + 8*x3 + 9*x4 + 48*x5 + 60*x6 + 53*x7)  #  Feeding cost 
    # 4. Set constraints 
    AlloyLP += (x1 + x2 + x3 + x4 + x5 + x6 + x7 == 1000)  #  Equality constraint 
    AlloyLP += (0.8*x1 + 0.7*x2 + 0.85*x3 + 0.4*x4 >= 0.65*1000)  #  Inequality constraint 
    AlloyLP += (0.8*x1 + 0.7*x2 + 0.85*x3 + 0.4*x4 <= 0.75*1000)  #  Inequality constraint 
    AlloyLP += (18.0*x1 + 3.2*x2 + 100.0*x5 >= 3.0*1000)  #  Inequality constraint 
    AlloyLP += (18.0*x1 + 3.2*x2 + 100.0*x5 <= 3.5*1000)  #  Inequality constraint 
    AlloyLP += (12.0*x1 + 1.1*x2 + 100.0*x6 >= 1.0*1000)  #  Inequality constraint 
    AlloyLP += (12.0*x1 + 1.1*x2 + 100.0*x6 <= 1.2*1000)  #  Inequality constraint 
    AlloyLP += (0.1*x2 + 100.0*x7 >= 1.1*1000)  #  Inequality constraint 
    AlloyLP += (0.1*x2 + 100.0*x7 <= 1.3*1000)  #  Inequality constraint 
    AlloyLP += (x1 + x2 + x3 + x4 + x5 + x6 + x7 == 1000)  #  Equality constraint 
    # 5. Solving linear programming problems 
    AlloyLP.solve()
    # 6. Output optimization results 
    print(AlloyLP)  #  Output problem setting parameters and conditions 
    # print(" Solving state :", pulp.LpStatus[AlloyLP.status])  #  Output solution status 
    for v in AlloyLP.variables():
        print(v.name, " = ", v.varValue)  #  Output the optimal value of each variable 
    print(" Minimum material cost  = ", pulp.value(AlloyLP.objective))  #  Output the objective function value of the optimal solution 
    # =  Attention  Youcans Share the original series  https://blog.csdn.net/youcans =

5. PuLP Program 2: Defining Decision Variables and Constraints Using dict

This program uses dict to define variables, objective functions and constraint parameters, which is convenient for parameter setting of complex problems.


    import pulp      #  Import  pulp Library 
    # 1. Establish optimization problems  AlloyLP:  Find the minimum (LpMinimize)
    AlloyLP = pulp.LpProblem(" Optimization of alloy production materials ", sense=pulp.LpMinimize)    #  Define the problem and find the minimum value 
    # 2. Define decision variables  x1~x7
    x1 = pulp.LpVariable(' Waste 1#', lowBound=0, upBound=75.0, cat='Continuous')  #  Definition  x1
    x2 = pulp.LpVariable(' Waste 2#', lowBound=0, upBound=250., cat='Continuous')  #  Definition  x2
    x3 = pulp.LpVariable(' Waste 3#', lowBound=0, cat='Continuous')  #  Definition  x3
    x4 = pulp.LpVariable(' Waste 4#', lowBound=0, cat='Continuous')  #  Definition  x4
    x5 = pulp.LpVariable(' Raw nickel ', lowBound=0, cat='Continuous')  #  Definition  x5
    x6 = pulp.LpVariable(' Raw material chromium ', lowBound=0, cat='Continuous')  #  Definition  x6
    x7 = pulp.LpVariable(' Molybdenum ', lowBound=0, cat='Continuous')  #  Definition  x7
    # 3. Define the objective function  cost
    AlloyLP += (16*x1 + 10*x2 + 8*x3 + 9*x4 + 48*x5 + 60*x6 + 53*x7)  #  Feeding cost 
    # 4. Set constraints 
    AlloyLP += (x1 + x2 + x3 + x4 + x5 + x6 + x7 == 1000)  #  Equality constraint 
    AlloyLP += (0.8*x1 + 0.7*x2 + 0.85*x3 + 0.4*x4 >= 0.65*1000)  #  Inequality constraint 
    AlloyLP += (0.8*x1 + 0.7*x2 + 0.85*x3 + 0.4*x4 <= 0.75*1000)  #  Inequality constraint 
    AlloyLP += (18.0*x1 + 3.2*x2 + 100.0*x5 >= 3.0*1000)  #  Inequality constraint 
    AlloyLP += (18.0*x1 + 3.2*x2 + 100.0*x5 <= 3.5*1000)  #  Inequality constraint 
    AlloyLP += (12.0*x1 + 1.1*x2 + 100.0*x6 >= 1.0*1000)  #  Inequality constraint 
    AlloyLP += (12.0*x1 + 1.1*x2 + 100.0*x6 <= 1.2*1000)  #  Inequality constraint 
    AlloyLP += (0.1*x2 + 100.0*x7 >= 1.1*1000)  #  Inequality constraint 
    AlloyLP += (0.1*x2 + 100.0*x7 <= 1.3*1000)  #  Inequality constraint 
    AlloyLP += (x1 + x2 + x3 + x4 + x5 + x6 + x7 == 1000)  #  Equality constraint 
    # 5. Solving linear programming problems 
    AlloyLP.solve()
    # 6. Output optimization results 
    print(AlloyLP)  #  Output problem setting parameters and conditions 
    # print(" Solving state :", pulp.LpStatus[AlloyLP.status])  #  Output solution status 
    for v in AlloyLP.variables():
        print(v.name, " = ", v.varValue)  #  Output the optimal value of each variable 
    print(" Minimum material cost  = ", pulp.value(AlloyLP.objective))  #  Output the objective function value of the optimal solution 
    # =  Attention  Youcans Share the original series  https://blog.csdn.net/youcans =

6. Python program and running results

Program 1 and Program 2 run the same result as follows:


Welcome to the CBC MILP Solver 
Version: 2.9.0 
Build Date: Feb 12 2015 
 Steel production problems :
MINIMIZE
16* Raw materials _ Waste 1 + 10* Raw materials _ Waste 2 + 8* Raw materials _ Waste 3 + 9* Raw materials _ Waste 4 + 53* Raw materials _ Molybdenum  + 60* Raw materials _ Chromium  + 48* Raw materials _ Nickel  + 0
SUBJECT TO
 Quality constraint :  Raw materials _ Waste 1 +  Raw materials _ Waste 2 +  Raw materials _ Waste 3 +  Raw materials _ Waste 4 +  Raw materials _ Molybdenum  +  Raw materials _ Chromium  +  Raw materials _ Nickel  = 1000
 Minimum proportion of carbon : 0.8  Raw materials _ Waste 1 + 0.7  Raw materials _ Waste 2 + 0.85  Raw materials _ Waste 3 + 0.4  Raw materials _ Waste 4 >= 650
 Maximum proportion of carbon : 0.8  Raw materials _ Waste 1 + 0.7  Raw materials _ Waste 2 + 0.85  Raw materials _ Waste 3 + 0.4  Raw materials _ Waste 4 <= 750
 Minimum proportion of nickel : 18  Raw materials _ Waste 1 + 3.2  Raw materials _ Waste 2 + 100  Raw materials _ Nickel  >= 3000
 Maximum proportion of nickel : 18  Raw materials _ Waste 1 + 3.2  Raw materials _ Waste 2 + 100  Raw materials _ Nickel  <= 3500
 Minimum proportion of chromium : 12  Raw materials _ Waste 1 + 1.1  Raw materials _ Waste 2 + 100  Raw materials _ Chromium  >= 1000
 Maximum proportion of chromium : 12  Raw materials _ Waste 1 + 1.1  Raw materials _ Waste 2 + 100  Raw materials _ Chromium  <= 1200
 Minimum proportion of molybdenum : 0.1  Raw materials _ Waste 2 + 100  Raw materials _ Molybdenum  >= 1100
 Maximum proportion of molybdenum : 0.1  Raw materials _ Waste 2 + 100  Raw materials _ Molybdenum  <= 1300
 Waste 1 Available dosage :  Raw materials _ Waste 1 <= 75
 Waste 2 Available dosage :  Raw materials _ Waste 2 <= 250
VARIABLES
 Raw materials _ Waste 1 Continuous
 Raw materials _ Waste 2 Continuous
 Raw materials _ Waste 3 Continuous
 Raw materials _ Waste 4 Continuous
 Raw materials _ Molybdenum  Continuous
 Raw materials _ Chromium  Continuous
 Raw materials _ Nickel  Continuous
 Optimized state : Optimal
 Raw materials _ Waste 1 = 75.0
 Raw materials _ Waste 2 = 90.909091
 Raw materials _ Waste 3 = 672.28283
 Raw materials _ Waste 4 = 137.30808
 Raw materials _ Molybdenum  = 10.909091
 Raw materials _ Chromium  = 0.0
 Raw materials _ Nickel  = 13.590909
 Optimal total cost  =  9953.671725000002

The above is the Python mathematical modeling PuLP library linear programming based on the detailed dictionary explanation of the details, more information about mathematical modeling PuLP library linear programming please pay attention to other related articles on this site!


Related articles: