纸浆添加约束,即至少一个LpAffineExpression等于1 [英] Pulp add constraint that at least one LpAffineExpression is equal to one
问题描述
说我有一个这样定义的PuLP模型:
model = pulp.LpProblem('',pulp.LpMaximize)
Say I have a PuLP model defined as thus:
model = pulp.LpProblem('',pulp.LpMaximize)
我添加了一个目标函数(没关系)
And I have added an objective function (it does not matter)
现在我有一个LpAffineExpression
对象的列表:
lps = [l1, l2, l3, ...]
Now I have a list of LpAffineExpression
objects:
lps = [l1, l2, l3, ...]
我想将这些LpAffineExpression
之一评估为1的约束添加到模型中.我将如何做?
I want to add to the model the constraint that one of these LpAffineExpression
evaluates to 1. How would I do so?
基本上,我要寻找的是语法正确的方式来执行以下操作:
Essentially what I'm looking for is the syntactically correct way to do the following:
model += (l1 == 1 OR l2 == 1 OR l3 == 1 OR ...)
请注意,我不知道lps
列表中有多少个LpAffineExpression
对象,因此无法对其进行硬编码.
Note that I do not know how many how many LpAffineExpression
objects are in the lps
list, so I cannot hardcode it in.
换句话说,我正在寻找一种在模型中对一组LpAffineExpression
进行分组的方法,这样就只需要实现一个即可.
In other words, I am looking for a way to group a set of LpAffineExpression
in the model, such that only one has to be fulfilled.
推荐答案
在线性或整数编程中没有或".但是,您可以使用二进制变量来模拟这种OR构造. (或者,如果不希望使用big-M,则使用SOS1变量;我不确定Pulp支持SOS1变量的扩展范围.)
There is no "OR" in linear or integer programming. However, you can use binary variables to simulate such an OR construct. (Or SOS1 variables if big-M's are undesirable; I am not sure to what extend Pulp supports SOS1 variables).
这个想法是:
1 - M * (1-δ(i)) <= L(i) <= 1 + M * (1-δ(i))
sum(i, δ(i)) >= 1
δ(i) ∈ {0,1}
- 显然
L(i)
是您的l1,l2,l3,...
-
δ(i)
是一个二进制变量,指示是否L(i)=1
.我们有δ(i)=1 ⇒ L(i)=1
. - 常量
M
可以设置为L(i)
的上下限. - Obviously the
L(i)
are yourl1,l2,l3,...
δ(i)
is a binary variable indicating ifL(i)=1
. We haveδ(i)=1 ⇒ L(i)=1
.- The constants
M
can be set to lower and upper bounds ofL(i)
.
这篇关于纸浆添加约束,即至少一个LpAffineExpression等于1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!