如何解决纸浆:向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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。