Pulp Lp:无法达到变量

问题描述 投票:0回答:1

我正在使用Python Pulp构建lp:

   # Import the PuLP lib
    from pulp import *

    # Products list
    products = ["car", "cycle"]

    profit = {"car": 8, "cycle": 12}

    uses  = {
            "Plastic":      [2.0, 4.0, 5.0, 7.0 , 1.0, 4.0, 2.0],
            "Wood":         [1.0, 1.0, 2.0, 2.0 , 1.0, 5.0, 1.0],
            "Steel":        [1.0, 2.0, 3.0, 3.0 , 2.0, 2.0, 5.0]
    }

    # Problem variables 
    x = LpVariable.dicts("products ", products , 0)

    # Maximise profit
    prob += lpSum([profit[i] * x[i] for i in products ]), "Maximise" // This is working

    prob += lpSum([uses[0][i] * x[i] for i in  products]) <= 142 ,"MinPlasticStock" // This is not working



     prob.solve()

这是关于我的最后一个for循环MinPlasticStock约束的错误:

KeyError: 0

我也无法打印:(products [x])或使用任何类似x [0]的LP变量,我发现这很奇怪:

KeyError: 0.

另一个错误,我无法调用我的任何决策变量,试图调用“ cycle”:

 prob += x[1] * 5 <= 142 ,"MinPlasticStock"

给予:

KeyError: 1

如果我这样更改最后一行,则lp运行正常(但是约束当然不起作用:]:>

prob += lpSum([x[i] for i in  products]) <= 142 ,"MinPlasticStock"

对于我的约束,您对如何使用uses对象有任何想法吗?

[我正在使用Python Pulp构建lp:#从纸浆导入中导入PuLP库*#产品列表产品= [“ car”,“ cycle”]利润= {“ car”:8,“ cycle”:12}用途= {... ... >>>

uses是字典,不包含0作为键。 products是一个列表,因此products[0]有效(只要它不为空)

使用Plastic键进行塑性约束:

prob += lpSum([uses["Plastic"][i] * x[i] for i in products]) <= 142 ,"MinPlasticStock" will work

如果要遍历材料,可以执行:

for material, material_values in uses:
    prob += lpSum([material_values[i] * x[i] for i in  products]) <= 142 ,"Min{}Stock".format(material) // This will work
python pulp
1个回答
0
投票

uses是字典,不包含0作为键。 products是一个列表,因此products[0]有效(只要它不为空)

© www.soinside.com 2019 - 2024. All rights reserved.