简体   繁体   English

纸浆:在循环中添加多个 LpSum

[英]Pulp: Adding multiple LpSum in a loop

I have a list like:我有一个类似的列表:

list = [var_1, var_2, var_3]

Using Pulp library, I'm trying to define them as variables with a for loop doing:使用 Pulp 库,我试图将它们定义为带有 for 循环的变量:

LpVariable(list[i] for i in range(len(list)))

Then I'm trying to sum them all and asign it to the model:然后我试图将它们全部加起来并将其分配给 model:

model += LpVariable(list[i] for i in range(len(list)))

In both cases I'm getting an error that says expected string or bytes-like object在这两种情况下,我都会收到一个错误,指出预期的字符串或字节,如 object

Can anyone help me?谁能帮我?

Regards问候

Id did not check the syntax but lpSum can sum a list of LpVariables so do you want something that looks more like Id 没有检查语法,但 lpSum 可以对 LpVariables 列表求和,所以你想要看起来更像的东西吗

model += LpSum([LpVariable(i) for i in list])

this assumes that the elements of your list are name strings and you want to create new LpVariables.这假定您的列表元素是名称字符串,并且您想要创建新的 LpVariables。

if so, you may want to first save these new LpVariables in a list or dict so you can use them in constraints and then add them as a objective to the model如果是这样,您可能希望首先将这些新的 LpVariables 保存在列表或字典中,以便您可以在约束中使用它们,然后将它们作为目标添加到 model

if the elements of your list are already LpVariables then you just need如果列表中的元素已经是 LpVariables 那么你只需要

model += LpSum(list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM