简体   繁体   English

PULP在求解后添加更多变量。 如何在不重建的情况下使用它们更新obj和约束?

[英]PULP Adding more variables after solving. How to update obj and constraints with them without having to rebuild?

if I want to add more variables to a problem already in memory in PULP, do I need to redefine/re-declare the objective function and constraints to include/use these new variables? 如果我想为已经存在于PULP中的问题添加更多变量,是否需要重新定义/重新声明目标函数和约束以包括/使用这些新变量? Or is there some streamlined method to add them and have the prob do that for me? 还是有一些简化的方法可以添加它们并让问题帮我解决?

Now, I see in PULP documentation that the LpVariable class has a paremeter option: "e – Used for column based modelling: relates to the variable's existence in the objective function and constraints" 现在,我在PULP文档中看到LpVariable类具有一个paremeter选项:“ e –用于基于列的建模:与变量在目标函数和约束中的存在有关”

And I also see in the doc that there is a command: "addVariableToConstraints(e)" 而且我还在文档中看到了一个命令:“ addVariableToConstraints(e)”

Does anyone have an example of the usage of these? 有没有人举这些用法的例子? I am not very good at using source code to deduce usage. 我不太擅长使用源代码来推断用法。 Also, I don't see a similar function for adding a variable to objective, and when I tried to do it with prob += lpSum(...) , I got the message "Overwriting previously set objective" 另外,我没有看到类似的函数将变量添加到目标,并且当我尝试使用prob + = lpSum(...)进行操作时,收到消息“覆盖先前设置的目标”

So in PuLP the constraints of your problem are stored in an Ordered Dictionary object. 因此,在PuLP中,您的问题的约束存储在有序词典对象中。 Of course, this means you can alter it as you required. 当然,这意味着您可以根据需要进行更改。 Your question is a little unclear, but here's all the ways you can modify your current problem with the different situations it would work in. For the sake of explanation, let's assume the two variables you're trying to add are called var1 and var2. 您的问题还不清楚,但是这里有各种方法可以根据您要解决的不同情况来修改当前问题。为便于说明,我们假设要添加的两个变量称为var1和var2。

1. Alter the objective 1.改变目标

In this case, it seems like your objective was a lpSum() method, although I'm not sure since you've not been specific enough in your question. 在这种情况下,您的目标似乎是lpSum()方法,尽管由于您对问题的了解不够具体,所以我不确定。

problem += problem.objective + 2*var1 - 3*var2 

This ensures you don't have to recalculate the current problem objective and call lpSum() again, since it's a heavy calculation overhead if you're doing it over and over again. 这样可以确保您不必重新计算当前问题的目标并再次调用lpSum(),因为如果您要一遍又一遍地进行操作,这将占用大量的计算资源。 Of course, anytime you alter the objective, you would get a notification saying that you're overwriting the previously set objective. 当然,只要您更改目标,就会收到一条通知,说您正在覆盖先前设定的目标。

2. Alter an existing constraint 2.更改现有约束

Again, assume this was your original constraint: var3 + 5*var4 >= 20 . 同样,假设这是您的原始约束: var3 + 5*var4 >= 20 And let's assume you want to change the constraint to: var3 + 5*var4 + var1 - 2*var2 >= 20 并假设您要将约束更改为: var3 + 5*var4 + var1 - 2*var2 >= 20

Then the simplest way to do that would be to previously label all the constraints as you add them in. So while creating the original constraint, you would have done the following: 然后,最简单的方法是在添加所有约束时预先标记它们。因此,在创建原始约束时,您将需要执行以下操作:

problem += var3 + 5*var4 >= 20, "ConstraintName #1" 

Then to change the constraint and add in additional variables, you'd do: 然后,要更改约束并添加其他变量,请执行以下操作:

problem.constraints["ConstraintName #1"] = var3 + 5*var4 + var1 - 2*var2 >= 20

Note: The method you proposed above in your question, where you add a new constraint to the original problem, instead of altering an existing constraint may not always work. 注意:您在问题中上面提出的方法,其中向原始问题添加新约束,而不是更改现有约束可能并不总是有效。 This is because if the Older Constraint was more constricting/restricting, and you planned to loosen the constraint or make it more elastic by adding in additional flexibility with the new variable, then it wold fail. 这是因为,如果“较旧的约束”更具约束/约束作用,并且您计划通过使用新变量增加附加的灵活性来放松约束或使其具有更大的弹性,则它将失败。 Because the solver would obtain both old & new constraints, and then choose the more constraining one (ie. the Older Constraint). 因为求解器将同时获得新旧约束,然后选择约束更严格的约束(即旧约束)。 And therefore, nothing would change. 因此,什么都不会改变。

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

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