Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Python Mathematical Modeling PuLP Library Linear programming Advanced based on Dictionary

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand Python mathematical modeling PuLP library linear programming advance based on dictionary", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to understand Python mathematical modeling PuLP library linear programming advanced dictionary-based" bar!

Catalogue

1. The problem of dictionary-based creation planning

2. Cases of linear programming problems

3. Establish a model

(1) decision variables

(2) objective function

(3) constraint conditions

(4) range of variables

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. The problem of 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, upper and lower bounds, and similarly setting model parameters one by one for the constraints. In large-scale planning problems, it is very tedious 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 problems.

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

Name = ['scrap 1', 'waste 2', 'waste 3', 'waste 4', 'nickel', 'chromium', 'molybdenum']

# A dictionary of the costs of each of the Ingredients is created

Mass = pulp.LpVariable.dicts ("raw material", material, lowBound=0, cat='Continuous')

-(2) use the dictionary type (dict) to set the parameters of the objective function and constraints, for example:

Cost = {

'scrap 1: 16

'scrap 2: 10

'scrap 3: 8

'scrap 4: 9

'nickel': 48

'Chrome': 60

'molybdenum': 53}

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

AlloyModel + = pulp.lpSum ([cost] * mass [item] for item in material]), "total production cost"

AlloyModel + = pulp.lpSum ([mass [item] for item in material]) = = 1000, "quality constraint"

2. Cases of linear programming problems

Taking the feeding problem of alloy steel production as an example, this paper analyzes the quick method of creating problems based on list and dictionary.

Problem description:

An iron and steel plant produces alloys that meet the requirements of chemical composition by smelting recovered metal scrap and adding some new materials, and plans to produce 1000 kg of alloys.

The main component of all metal scrap is iron, and different metal scraps also contain various trace elements.

The proportion of each component, available quantity and unit cost of metal scrap and new materials are shown in the following table. The content requirements of each component in the 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?

Material carbon, nickel, chromium and molybdenum available cost waste 10.8018.012.00.07516 scrap 20.703.21.10.125010 scrap 30.85000 unlimited 8 scrap 40.40000 unlimited 9 nickel 010000 unlimited 48 chromium 001000 unlimited 60 molybdenum 000100 unlimited 53 alloy lower limit 0.653.01.01.1 gram / alloy upper limit 0.753.51.21.3 Uniq3, establish the model (1) decision variable

X1: amount of waste 1 (kg)

X2: waste 2 dosage (kg)

X3: amount of waste 3 (kg)

X4: waste 4 dosage (kg)

X5: amount of raw material nickel (kg)

X6: amount of raw material chromium (kg)

X7: amount of raw material molybdenum (kg)

(2) objective function

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

(3) constraint conditions

0.8*x1 + 0.7*x2 + 0.85*x3 + 0.40*x4 > = 0.65 million

0.8*x1 + 0.7*x2 + 0.85*x3 + 0.40*x4 = 3.0mm 1000

18.0*x1 + 3.2*x2 + 100.0*x5 = 1.051000

12.0*x1 + 1.1*x2 + 100.0*x6 > = 1.2 million

0.1*x2 + 100.0*x7 > = 1.1mm 1000

0.1*x2 + 100.0*x7 > = 1.3 million

(4) range of variables

Xi > = 0, iMagol 1, 2, … seven

X1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report