Translating a LP from Excel to Python Pulp

huangapple go评论83阅读模式
英文:

Translating a LP from Excel to Python Pulp

问题

I've translated the relevant portions of your code and explanation into English below:

Mining and metals

In the manufacture of steel with raw materials, we want to reduce the cost of producing this steel to earn more money while still respecting the important characteristics of quality steel.

# Characteristics of the steel to be made

Element          % Minimum   % Max
Carbon           2           3
Copper           0.4         0.6
Manganese        1.2         1.65

# Characteristics, stocks, and purchase price of alloys in KILOS

Alloy            C %         Cu %    Mn %    Stocks kg    Price /kg
Iron alloy 1     2.50        0.00    1.30    4000         1.20
Iron alloy 2     3.00        0.00    0.80    3000         1.50
Iron alloy 3     0.00        0.30    0.00    6000         0.90
Copper alloy 1   0.00        90.00   0.00    5000         1.30
Copper alloy 2   0.00        96.00   4.00    2000         1.45
Aluminum alloy 1 0.00        0.40    1.20    3000         1.20
Aluminum alloy 2 0.00        0.60    0.00    2500         1.00

# Import the PuLP library
from pulp import *

# Create the problem variable
prob = LpProblem("MinimizeSteelAlloy", LpMinimize)

# The 7 variables have a lower limit of zero (expressed in KILOS)
x1 = LpVariable("Iron alloy 1", 0)
x2 = LpVariable("Iron alloy 2", 0)
x3 = LpVariable("Iron alloy 3", 0)
x4 = LpVariable("Copper alloy 1", 0)
x5 = LpVariable("Copper alloy 2", 0)
x6 = LpVariable("Aluminum alloy 1", 0)
x7 = LpVariable("Aluminum alloy 2", 0)

# The objective function is to minimize the total cost of the alloys in EUROS
prob += 1.20 * x1 + 1.50 * x2 + 0.90 * x3 + 1.30 * x4 + 1.45 * x5 + 1.20 * x6 + 1.00 * x7, "AlloyCost"

# Quantity constraint in KILOS
prob += x1 + x2 + x3 + x4 + x5 + x6 + x7 == 5000, "RequestedQuantity"

# Carbon constraint
prob += (2.50 * x1 + 3.00 * x2 + x3 + x4 + x5 + x6 + x7) / 5000 <= 3, "MaxCarbon"
prob += (2.50 * x1 + 3.00 * x2 + x3 + x4 + x5 + x6 + x7) / 5000 >= 2, "MinCarbon"

# Copper constraint
prob += (x1 + x2 + 0.30 * x3 + 90 * x4 + 96 * x5 + 0.40 * x6 + 0.60 * x7) / 5000 <= 0.6, "MaxCopper"
prob += (x1 + x2 + 0.30 * x3 + 90 * x4 + 96 * x5 + 0.40 * x6 + 0.60 * x7) / 5000 >= 0.4, "MinCopper"

# Manganese constraint
prob += (1.30 * x1 + 0.80 * x2 + x3 + x4 + 4 * x5 + 1.20 * x6 + x7) / 5000 <= 1.65, "MaxManganese"
prob += (1.30 * x1 + 0.80 * x2 + x3 + x4 + 4 * x5 + 1.20 * x6 + x7) / 5000 >= 1.2, "MinManganese"

# Maximum constraints based on available stock, by alloy
prob += x1 <= 4000, "MaxStock1"
prob += x2 <= 3000, "MaxStock2"
prob += x3 <= 6000, "MaxStock3"
prob += x4 <= 5000, "MaxStock4"
prob += x5 <= 2000, "MaxStock5"
prob += x6 <= 3000, "MaxStock6"
prob += x7 <= 2500, "MaxStock7"

# Write the problem data to an .lp file
prob.writeLP("steel.lp")

# Solve the problem
prob.solve()

# The status of the solution
print("Status:", LpStatus[prob.status])

# Display the optimal values of each variable
for v in prob.variables():
    print(v.name, "=", v.varValue)

# The result of the objective function
print("Total payable in euros:", value(prob.objective))

This code attempts to optimize the production of steel alloys based on specified constraints and objectives. If you encounter issues or need further assistance with your code, please feel free to ask.

英文:

I need to produce 5000 kgs of steel by mixing 7 alloys parts .
I need to reduce the cost, so i need to pick up the best parts.

The result must respect the main steel caracteristics, for example, the carbon level must be between 2% and 3 %, no more, no less .

The Excel linear solver program already exists ,and is originated from a professional book.

I'm trying to translate it to a PULP code, now .

My problem is : How to create the copper, carbone, and manganèse constraints ? There are 2 arrays, so I don't know how to do.

It is all in percents, and I don't know how to do . My result is actually wrong, I left the bad constraints I've done for information . It seems that I need to divide by 5000 at one moment, but how should I do ?

Let me try to explain to you what I can not understand :

I need 5000 kgs of steel to have 0.60 % of copper in it, but my Copper alloy parts contains 90 % and 96% of copper.
Do you see what I mean, and why it is so difficult to describe my constraints ?

&quot;&quot; &quot;
Mining and metals
We make steel with raw materials, we want to reduce the cost of producing this steel
to make more money, but still respecting the minimum characteristics of quality steel
&quot;&quot; &quot;
# Minimize the cost of metal alloys.
# Characteristics of the steel to be made
&quot;&quot; &quot;Element      %Minimum %Max   %Real ( it is a var)
Carbon       2         3     2.26
Copper       0.4       0.6   0.60
Manganese    1.2       1.65  1.20
&quot;&quot; &quot;
# Characteristics, stocks and purchase price of alloys
&quot;&quot; &quot;
Alloy          C%   Cu%   Mn%     Stocks kg Price € / kg
Iron alloy     2.50 0.00  1.30    4000      1.20
Iron alloy     3.00 0.00  0.80    3000      1.50
Iron alloy     0.00 0.30  0.00    6000      0.90
Copper alloy   0.00 90.00 0.00    5000      1.30
Copper alloy   0.00 96.00 4.00    2000      1.45
Aluminum alloy 0.00 0.40  1.20    3000      1.20
Aluminum alloy 0.00 0.60  0.00   2,500      1.00
&quot;&quot; &quot;
# Import the PuLP lib
from pulp import *
# Create the problem variable
prob = LpProblem (&quot;MinimiserLpAlliage&quot;, LpMinimize)
# The 7 vars have a zero limit
x1 = LpVariable (&quot;Iron alloy 1&quot;, 0)
x2 = LpVariable (&quot;Iron alloy 2&quot;, 0)
x3 = LpVariable (&quot;Iron alloy 3&quot;, 0)
x4 = LpVariable (&quot;Copper alloy 1&quot;, 0)
x5 = LpVariable (&quot;Copper alloy 2&quot;, 0)
x6 = LpVariable (&quot;Aluminum alloy 1&quot;, 0)
x7 = LpVariable (&quot;Aluminum alloy 2&quot;, 0)
# The objective function is to minimize the total cost of the alloys in EUROS for a given quantity in KGS
prob + = 1.20 * x1 + 1.50 * x2 + 0.90 * x3 + 1.30 * x4 + 1.45 * x5 + 1.20 * x6 + 1.00 * x7, &quot;AlliageCost&quot;
# Quantity constraint in KGS.
prob + = x1 + x2 + x3 + x4 + x5 + x6 + x7 == 5000, &quot;RequestedQuantity&quot;
# MIN constraints of% carbon, by alloy  // ITS NOT WHAT I NEED
prob + = x1&gt; = 2.5, &quot;MinCarboneRequirement1&quot;
prob + = x2&gt; = 3, &quot;MinCarboneRequirement2&quot;
prob + = x3&gt; = 0, &quot;MinCarboneRequirement3&quot;
prob + = x4&gt; = 0, &quot;MinCarboneRequirement4&quot;
prob + = x5&gt; = 0, &quot;MinCarboneRequirement5&quot;
prob + = x6&gt; = 0, &quot;MinCarboneRequirement6&quot;
prob + = x7&gt; = 0, &quot;MinCarboneRequirement7&quot;
# MIN constraints of% copper, by alloy // ITS WRONG ITS NOT WHAT I NEED
prob + = x1&gt; = 0, &quot;MinCuivreRequirement1&quot;
prob + = x2&gt; = 0, &quot;MinCuivreRequirement2&quot;
prob + = x3&gt; = 0.3, &quot;MinCuivreRequirement3&quot;
prob + = x4&gt; = 90, &quot;MinCuivreRequirement4&quot;
prob + = x5&gt; = 96, &quot;MinCuivreRequirement5&quot;
prob + = x6&gt; = 0.4, &quot;MinCuivreRequirement6&quot;
prob + = x7&gt; = 0.6, &quot;MinCuivreRequirement7&quot;
# MIN constraints of% of Manganese, by alloy // ITS WRONG ITS NOT WHAT I NEED
prob + = x1&gt; = 1.3, &quot;MinManganeseRequirement1&quot;
prob + = x2&gt; = 0.8, &quot;MinManganeseRequirement2&quot;
prob + = x3&gt; = 0, &quot;MinManganeseRequirement3&quot;
prob + = x4&gt; = 0, &quot;MinManganeseRequirement4&quot;
prob + = x5&gt; = 4, &quot;MinManganeseRequirement5&quot;
prob + = x6&gt; = 1.2, &quot;MinManganeseRequirement6&quot;
prob + = x7&gt; = 0, &quot;MinManganeseRequirement7&quot;
# MAX constraints of% of Manganese, by alloy // ITS WRONG ITS NOT WHAT I NEED
prob + = x1 &lt;= 1.3, &quot;MaxManganeseRequirement1&quot;
prob + = x2 &lt;= 0.8, &quot;MaxManganeseRequirement2&quot;
prob + = x3 &lt;= 0, &quot;MaxManganeseRequirement3&quot;
prob + = x4 &lt;= 0, &quot;MaxManganeseRequirement4&quot;
prob + = x5 &lt;= 4, &quot;MaxManganeseRequirement5&quot;
prob + = x6 &lt;= 1.2, &quot;MaxManganeseRequirement6&quot;
prob + = x7 &lt;= 0, &quot;MaxManganeseRequirement7&quot;
# 5. MAX constraints from available stock, by alloy // I THINK IT IS OK
prob + = x1 &lt;= 4000, &quot;MaxStock&quot;
prob + = x2 &lt;= 3000, &quot;MaxStock1&quot;
prob + = x3 &lt;= 6000, &quot;MaxStock2&quot;
prob + = x4 &lt;= 5000, &quot;MaxStock3&quot;
prob + = x5 &lt;= 2000, &quot;MaxStock4&quot;
prob + = x6 &lt;= 3000, &quot;MaxStock5&quot;
prob + = x7 &lt;= 2500, &quot;MaxStock6&quot;
# The problem data is written to an .lp file
prob.writeLP ( &quot;WhiskasModel.lp&quot;)
# We use the solver
prob.solve ()
# The status of the solution
print (&quot;Status:&quot;, LpStatus [prob.status])
# We magnify and display the optimums of each var
for v in prob.variables ():
print (v.name, &quot;=&quot;, v.varValue)
# The result of the objective function is here
print (&quot;Total&quot;, value (prob.objective))

This is the answer, but of course, it is wrong, cause I dont know how to do the constraints :

Status: Optimal
Aluminum_alloy_1 = 1.2
Aluminum_alloy_2 = 0.6
Copper_alloy_1 = 90.0
Alloy_of_copper_2 = 96.0
Alloy_of_fer_1 = 2.5
Alloy_of_fer_2 = 3.0
Iron_alloy_3 = 4806.7
Total 4,591.76,999,999,999,995

EDIT Hello !
This is the improved version 2 of my code, sorry, it is in french, but i bet you can see what i mean , it still doesn't work , thought... but closer to what I need :

Mining and metals
In the manufacture of steel with permeable materials, sur wants to reduce the cost of producing this steel
to earn more money but still respecting the important characteristics of quality steel
# Characteristics of the steel to be made
&quot;&quot;&quot; El&#233;ment 	% minimal 	% Max 	
Carbone 	        2 		  3	
Cuivre	            0.4 	 0.6 	
Mangan&#232;se	        1.2 	 1.65 
&quot;&quot;&quot;
# Characteristics, stocks and purchase price of alloys at KILO
&quot;&quot;&quot; 
Alliage	            C %	    Cu %	Mn %	Stocks kg	Prix €/kg
Alliage de fer 1	2,50	0,00	1,30	4000	    1,20
Alliage de fer 2	3,00	0,00	0,80	3000	    1,50
Alliage de fer 3	0,00	0,30	0,00	6000	    0,90
Alliage de cuivre 1	0,00	90,00	0,00	5000	    1,30
Alliage de cuivre 2	0,00	96,00	4,00	2000	    1,45
Alliage d&#39;alu 1	    0,00	0,40	1,20	3000	    1,20
Alliage d&#39;alu 2	    0,00	0,60	0,00	2500	    1,00 
&quot;&quot;&quot;
# Importer la lib PuLP 
from pulp import *
#Cr&#233;er la variable du probl&#232;me
prob = LpProblem(&quot;MinimiserLpAlliage&quot;,LpMinimize)
# The 7 vars have a zero limit, these decision variables are expressed in KILOS
x1 = LpVariable(&quot;Alliage de fer 1&quot;,0)
x2 = LpVariable(&quot;Alliage de fer 2&quot;,0)
x3 = LpVariable(&quot;Alliage de fer 3&quot;,0)
x4 = LpVariable(&quot;Alliage de cuivre 1&quot;,0)
x5 = LpVariable(&quot;Alliage de cuivre 2&quot;,0)
x6 = LpVariable(&quot;Alliage d&#39;alu 1&quot;,0)
x7 = LpVariable(&quot;Alliage d&#39;alu 2&quot;,0)
# The objective function is to minimize the total cost of the alloys in EUROS
prob += 1.20 * x1 + 1.50 * x2 + 0.90 * x3 + 1.30 * x4 + 1.45 * x5 + 1.20 * x6 + 1.00 * x7, &quot;CoutAlliages&quot;
# Quantity constraint in KGS.
prob += x1 + x2 + x3 + x4 + x5 + x6 + x7 == 5000, &quot;Quantit&#233;Demand&#233;e&quot;
# Carbon stress.
prob += (2.50 * x1  + 3.00 * x2 + x3 + x4 + x5 + x6 + x7 ) / 5000 &lt;= 3,&quot;carBmax&quot;
prob += (2.50 * x1  + 3.00 * x2 + x3 + x4 + x5 + x6 + x7 ) / 5000 &gt;= 2,&quot;carBmin&quot;
# Constraint cu  .
prob += (x1 + x2 + 0.30 * x3 +  90 * x4  +  96 * x5 + 0.40 * x6 + 0.60 * x7) / 5000 &lt;= 0.6,&quot;cuBmax&quot;
prob += (x1 + x2 + 0.30 * x3 +  90 * x4  +  96 * x5 + 0.40 * x6 + 0.60 * x7) / 5000 &gt;= 0.4,&quot;cuBmin&quot;
# Constraint Mangan&#232;se.
prob += (1.30 * x1 + 0.80 * x2 + x3 + x4  + 4 *  x5  + 1.20 * x6 + x7 ) / 5000 &lt;= 1.65,&quot;mgBmax&quot;
prob += (1.30 * x1 + 0.80 * x2 + x3 + x4  + 4 *  x5  + 1.20 * x6 + x7 ) / 5000 &gt;= 1.2,&quot;mgBmin&quot;
# 5. MAX constraints from available stock, by alloy
prob += x1 &lt;= 4000 , &quot;MaxStock&quot;
prob += x2 &lt;= 3000 , &quot;MaxStock1&quot;  
prob += x3 &lt;= 6000  , &quot;MaxStock2&quot;  
prob += x4 &lt;= 5000 , &quot;MaxStock3&quot;   
prob += x5 &lt;= 2000 , &quot;MaxStock4&quot; 
prob += x6 &lt;= 3000  , &quot;MaxStock5&quot;
prob += x7 &lt;= 2500  , &quot;MaxStock6&quot;
# The problem data is written to an .lp file
prob.writeLP(&quot;acier.lp&quot;)
# On utilise le solveur
prob.solve()
# The status of the solution
print (&quot;Status:&quot;, LpStatus[prob.status])
# We magnify and display the optimums of each var
for v in prob.variables():
print (v.name, &quot;=&quot;, v.varValue)
# The result of the objective function is here
print (&quot;Total payable in euros&quot;, value(prob.objective))
&quot;&quot;&quot; Status: Infeasible
Alliage_d&#39;alu_1 = 0.0
Alliage_d&#39;alu_2 = 0.0
Alliage_de_cuivre_1 = 0.0
Alliage_de_cuivre_2 = 0.0
Alliage_de_fer_1 = 0.0
Alliage_de_fer_2 = 0.0
Alliage_de_fer_3 = 10000.0
Total &#224; payer en euros 9000.0 &quot;&quot;&quot;

> The book says the result with the excel solver is :
>
> iron_1 : 4000 kgs
> iron_2 : 0 kgs
> iron_3 : 397.76kgs
> cu_1 : 0 kgs
> cu_2 : 27.61kgs
> al_1 : 574.62kgs
> al_2 : 0kgs
>
> Cost in euros 5887.57
> Steel contains 2% carb, 0.6 % cu, 1.2 %

manganese

Excel tab :
Translating a LP from Excel to Python Pulp

Solver pic :
Translating a LP from Excel to Python Pulp

答案1

得分: 3

你的问题的一部分是你如何理解/应用百分比。我的建议是尽早将百分比[0-100]转换为分数[0-1.0]。

在Excel中,当一个单元格显示50%时,实际上该单元格的数值是0.5。以这种方式处理百分比意味着你不必一直除以100,可以将一个百分比与另一个百分比相乘,一切都可以正常工作。

以下是下面的代码:

"""
采矿和金属
我们使用原材料制造钢铁,我们希望降低生产这种钢铁的成本
以赚更多钱,但仍然要遵守质量钢铁的最低特性
"""
# 最小化金属合金的成本。
# 钢材的特性
"""
Element      %Minimum  %Max   %Real (it is a var)
Carbon       2         3      2.26
Copper       0.4       0.6    0.60
Manganese    1.2       1.65   1.20
"""
# 特性、库存和合金的采购价格
"""
Alloy          C%   Cu%   Mn%     Stocks kg Price € / kg
Iron alloy     2.50 0.00  1.30    4000      1.20
Iron alloy     3.00 0.00  0.80    3000      1.50
Iron alloy     0.00 0.30  0.00    6000      0.90
Copper alloy   0.00 90.00 0.00    5000      1.30
Copper alloy   0.00 96.00 4.00    2000      1.45
Aluminum alloy 0.00 0.40  1.20    3000      1.20
Aluminum alloy 0.00 0.60  0.00    2500      1.00
"""
# 导入PuLP库
from pulp import *
# 创建问题变量
prob = LpProblem("MinimiserLpAlliage", LpMinimize)
# 问题数据
input_mats = ["iron_1", "iron_2", "iron_3",
"cu_1", "cu_2",
"al_1", "al_2"]
input_costs = {"iron_1": 1.20, "iron_2": 1.50, "iron_3": 0.90,
"cu_1":   1.30, "cu_2": 1.45,
"al_1":   1.20, "al_2":   1.00}
# C%     Cu%   Mn%
input_composition = {"iron_1": [0.025, 0.000,  0.013],
"iron_2": [0.030, 0.000,  0.008],
"iron_3": [0.000, 0.003,  0.000],
"cu_1":   [0.000, 0.900,  0.000],
"cu_2":   [0.000, 0.960,  0.040],
"al_1":   [0.000, 0.004,  0.012],
"al_2":   [0.000, 0.006,  0.000]}
input_stock = {"iron_1": 4000, "iron_2": 3000, "iron_3": 6000,
"cu_1": 5000, "cu_2":  2000,
"al_1": 3000, "al_2": 2500}
request_quantity = 5000
Carbon_min = 0.02
Carbon_max = 0.03
Cu_min = 0.004
Cu_max = 0.006
Mn_min = 0.012
Mn_max = 0.0165
# 问题变量 - 每种输入的千克数量
x = LpVariable.dicts("input_mat", input_mats, 0)
# 目标函数是最小化给定数量的合金的总成本(以欧元计)
prob += lpSum([input_costs[i]*x[i] for i in input_mats]), "AlliageCost"
# 千克数量的约束
prob += lpSum([x[i] for i in input_mats]) == request_quantity, "RequestedQuantity"
# 最终钢铁中碳的MIN/MAX约束
prob += lpSum([x[i]*input_composition[i][0] for i in input_mats]) >= Carbon_min*request_quantity, "MinCarbon"
prob += lpSum([x[i]*input_composition[i][0] for i in input_mats]) <= Carbon_max*request_quantity, "MaxCarbon"
# 最终钢铁中铜的MIN/MAX约束
prob += lpSum([x[i]*input_composition[i][1] for i in input_mats]) >= Cu_min*request_quantity, "MinCu"
prob += lpSum([x[i]*input_composition[i][1] for i in input_mats]) <= Cu_max*request_quantity, "MaxCu"
# 最终钢铁中锰的MIN/MAX约束
prob += lpSum([x[i]*input_composition[i][2] for i in input_mats]) >= Mn_min*request_quantity, "MinMn"
prob += lpSum([x[i]*input_composition[i][2] for i in input_mats]) <= Mn_max*request_quantity, "MaxMn"
# 可用库存的最大约束
for i in input_mats:
prob += x[i] <= input_stock[i], ("MaxStock_" + i)
# 解决问题
prob.solve()
# 解决方案的状态
print ("状态:", LpStatus[prob.status])
# 显示每个变量的最优解
for v in prob.variables ():
print (v.name, "=", v.varValue)
# 显示材料成分
Carbon_value = sum([x[i].varValue*input_composition[i][0] for i in input_mats])/request_quantity
Cu_value = sum([x[i].varValue*input_composition[i][1] for i in input_mats])/request_quantity
Mn_value = sum([x
<details>
<summary>英文:</summary>
Part of your problem is how you are understanding/applying percentages. My recommendation would be to convert percentages [0-100] to fractional numbers [0-1.0] as early as possible.
In excel when a cell says `50%` the numeric value of the cell is actually `0.5`. Working with percentages in this way means you don&#39;t have to keep dividing out by 100, and can multiply one percentage with another and it all just works.
The code below does what you want:
&quot;&quot;&quot;
Mining and metals
We make steel with raw materials, we want to reduce the cost of producing this steel
to make more money, but still respecting the minimum characteristics of quality steel
&quot;&quot;&quot;
# Minimize the cost of metal alloys.
# Characteristics of the steel to be made
&quot;&quot;&quot;Element      %Minimum  %Max   %Real (it is a var)
Carbon       2         3      2.26
Copper       0.4       0.6    0.60
Manganese    1.2       1.65   1.20
&quot;&quot;&quot;
# Characteristics, stocks and purchase price of alloys
&quot;&quot;&quot;
Alloy          C%   Cu%   Mn%     Stocks kg Price € / kg
Iron alloy     2.50 0.00  1.30    4000      1.20
Iron alloy     3.00 0.00  0.80    3000      1.50
Iron alloy     0.00 0.30  0.00    6000      0.90
Copper alloy   0.00 90.00 0.00    5000      1.30
Copper alloy   0.00 96.00 4.00    2000      1.45
Aluminum alloy 0.00 0.40  1.20    3000      1.20
Aluminum alloy 0.00 0.60  0.00    2500      1.00
&quot;&quot;&quot;
# Import the PuLP lib
from pulp import *
# Create the problem variable
prob = LpProblem (&quot;MinimiserLpAlliage&quot;, LpMinimize)
# Problem Data
input_mats = [&quot;iron_1&quot;, &quot;iron_2&quot;, &quot;iron_3&quot;,
&quot;cu_1&quot;, &quot;cu_2&quot;,
&quot;al_1&quot;, &quot;al_2&quot;]
input_costs = {&quot;iron_1&quot;: 1.20, &quot;iron_2&quot;: 1.50, &quot;iron_3&quot;: 0.90,
&quot;cu_1&quot;:   1.30, &quot;cu_2&quot;: 1.45,
&quot;al_1&quot;:   1.20, &quot;al_2&quot;:   1.00}
#                               C%     Cu%   Mn%
input_composition = {&quot;iron_1&quot;: [0.025, 0.000,  0.013],
&quot;iron_2&quot;: [0.030, 0.000,  0.008],
&quot;iron_3&quot;: [0.000, 0.003,  0.000],
&quot;cu_1&quot;:   [0.000, 0.900,  0.000],
&quot;cu_2&quot;:   [0.000, 0.960,  0.040],
&quot;al_1&quot;:   [0.000, 0.004,  0.012],
&quot;al_2&quot;:   [0.000, 0.006,  0.000]}
input_stock = {&quot;iron_1&quot;: 4000, &quot;iron_2&quot;: 3000, &quot;iron_3&quot;: 6000,
&quot;cu_1&quot;: 5000, &quot;cu_2&quot;:  2000,
&quot;al_1&quot;: 3000, &quot;al_2&quot;: 2500}
request_quantity = 5000
Carbon_min = 0.02
Carbon_max = 0.03
Cu_min = 0.004
Cu_max = 0.006
Mn_min = 0.012
Mn_max = 0.0165
# Problem variables - amount in kg of each input
x = LpVariable.dicts(&quot;input_mat&quot;, input_mats, 0)
# The objective function is to minimize the total cost of the alloys in EUROS for a given quantity in KGS
prob += lpSum([input_costs[i]*x[i] for i in input_mats]), &quot;AlliageCost&quot;
# Quantity constraint in KGS.
prob += lpSum([x[i] for i in input_mats]) == request_quantity, &quot;RequestedQuantity&quot;
# MIN/MAX constraint of carbon in resultant steel
prob += lpSum([x[i]*input_composition[i][0] for i in input_mats]) &gt;= Carbon_min*request_quantity, &quot;MinCarbon&quot;
prob += lpSum([x[i]*input_composition[i][0] for i in input_mats]) &lt;= Carbon_max*request_quantity, &quot;MaxCarbon&quot;
# MIN/MAX constraints of copper in resultant steel
prob += lpSum([x[i]*input_composition[i][1] for i in input_mats]) &gt;= Cu_min*request_quantity, &quot;MinCu&quot;
prob += lpSum([x[i]*input_composition[i][1] for i in input_mats]) &lt;= Cu_max*request_quantity, &quot;MaxCu&quot;
# MIN/MAX constraints of manganese in resultant steel
prob += lpSum([x[i]*input_composition[i][2] for i in input_mats]) &gt;= Mn_min*request_quantity, &quot;MinMn&quot;
prob += lpSum([x[i]*input_composition[i][2] for i in input_mats]) &lt;= Mn_max*request_quantity, &quot;MaxMn&quot;
# MAX constraints of available stock
for i in input_mats:
prob += x[i] &lt;= input_stock[i], (&quot;MaxStock_&quot; + i)
# Solve the problem
prob.solve()
# The status of the solution
print (&quot;Status:&quot;, LpStatus [prob.status])
# Dislay the optimums of each var
for v in prob.variables ():
print (v.name, &quot;=&quot;, v.varValue)
# Display mat&#39;l compositions
Carbon_value = sum([x[i].varValue*input_composition[i][0] for i in input_mats])/request_quantity
Cu_value = sum([x[i].varValue*input_composition[i][1] for i in input_mats])/request_quantity
Mn_value = sum([x[i].varValue*input_composition[i][2] for i in input_mats])/request_quantity
print (&quot;Carbon content: &quot; + str(Carbon_value))
print (&quot;Copper content: &quot; + str(Cu_value))
print (&quot;Manganese content: &quot; + str(Mn_value))
# The result of the objective function is here
print (&quot;Total&quot;, value (prob.objective))
From which I get:
Status: Optimal
input_mat_al_1 = 574.62426
input_mat_al_2 = 0.0
input_mat_cu_1 = 0.0
input_mat_cu_2 = 27.612723
input_mat_iron_1 = 4000.0
input_mat_iron_2 = 0.0
input_mat_iron_3 = 397.76302
Carbon content: 0.02
Copper content: 0.006000000036
Manganese content: 0.012000000008
Total 5887.57427835
</details>

huangapple
  • 本文由 发表于 2020年1月3日 21:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定