纸浆:向LpVariable.dicts

问题描述

假设我有这本字典:

cars = ["car1","car2","car3","car4","car5"]

x = LpVariable.dicts("car",cars,cat='Integer',lowBound=0,upBound=800)

请问有什么方法可以向每辆车添加不同的lowBound和upBounds吗?

注意

简单代码版本如下:

car1 = LpVariable("car1",40)   
car2 = LpVariable("car2",1000) 

请注意,car1 upBound为40,car 2 upBound为1000。

解决方法

最后, 我已经使用他的出色代码做到了: How do I generate PuLP variables and constrains without using exec? 非常感谢DSM,兄弟!

prob = LpProblem("problem",LpMaximize)

# Setting LP variables
lpVars =["car1","car2","car3"]
upbounds=[40,80,30]
xs = [LpVariable("car{}".format(i+1),lowBound = 0,upBound = upbounds[i],cat='Integer'  ) for i in range(len(lpVars))]

 # add objective
    margin = [3,2,3]
    total_prof = sum(x * value for x,value in zip(xs,margin))
    prob += total_prof

# add constraint
    labour = [2,1,4]
    total_labour = sum(x * w for x,w in zip(xs,labour))
    prob += total_labour <= 100

 # Solve the problem
    prob.solve()

下一步是从前端应用程序获取数组变量(上行,边距,人工等。),谢谢,兄弟,偷看我的github

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...