假设我有这本字典:
cars = ["car1","car2","car3","car4","car5"]
x = LpVariable.dicts("car",cars, cat='Integer', lowBound=0, upBound=800)
请问有什么方法可以向每辆车添加不同的lowBound和upBounds吗?
注意
简单代码版本如下:
car1 = LpVariable("car1", 0, 40)
car2 = LpVariable("car2", 0, 1000)
请注意,car1 upBound为40,car 2 upBound为1000。
答案 0 :(得分:0)
最后, 我已经使用他的出色代码做到了: 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