纸浆Lp:无法访问变量

2024-09-27 21:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python纸浆构建一个lp:

   # Import the PuLP lib
    from pulp import *

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

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

    uses  = {
            "Plastic":      [2.0, 4.0],
            "Wood":         [1.0, 1.0],
            "Steel":        [1.0, 2.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 ,"MaxPlasticStock" // This is not working



     prob.solve()

这是关于上次for循环MaxPlasticStock约束的错误:

KeyError: 0

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

KeyError: 0.

另一个错误,我无法调用任何决策变量,尝试调用“循环”:

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

给出:

KeyError: 1

如果我像这样更改最后一行,lp运行良好(但约束当然不起作用):

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

你知道如何使用uses对象来约束我吗

编辑:请不要麻烦,我正在这样更改lp:

plasticAmount =  {car : 2.0, cycle : 4.0 }

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

它很长,我必须把东西分开,但至少,它是有效的


Tags: inforisthiscarproductslpkeyerror

热门问题