[英]Multiplication between booleans in linear programming (python, Pulp library)
I'm looking for a solution to a linear programming problem and I need to define the following constraints: 我正在寻找线性编程问题的解决方案,并且需要定义以下约束:
gji = 1 if guest j is seated at table i, 0 otherwise
gki = 1 if guest k is seated at table i, 0 otherwise
pjik = gij * gik = 1 if guest j AND guest k are seated at table i, 0 otherwise
I wrote the first two costrains (using the Pulp library), but I don't know how to represent the multiplication of gji*gki
我写了前两个Costrain(使用Pulp库),但是我不知道如何表示gji*gki
的乘法
My code: 我的代码:
Gji = LpVariable.matrix("Gji",(range(0,number_guest),range(0,number_table)),lowBound=0, upBound=1, cat='binary')
Gki = LpVariable.matrix("Gki",(range(0,number_guest),range(0,number_table)),lowBound=0, upBound=1, cat='binary')
for row in range (0,number_guest):
prob += lpSum(Gji[row])<=1
prob += lpSum(Gji[row])>=1
for columns in range (0,number_table):
prob += lpSum(np.matrix(Gji).T[columns].tolist()) <= a
How can I write the costrain for Pjki
? 我该如何写Pjki的Pjki
?
Always first formulate a proper mathematical model, before implementing it in PuLp. 在PuLp中实施该模型之前,请务必先制定适当的数学模型。
Let 让
g(i,k) = 1 if guest i sits at table k
0 otherwise
and 和
p(i,j,k) = 1 if guests i and j sit at table k
0 otherwise
First you need some assignment constraints: 首先,您需要一些分配约束:
sum(i, g(i,k)) <= capacity(k) for all k
sum(k, g(i,k)) = 1 for all i
The binary multiplication 二进制乘法
p(i,j,k) = g(i,k) * g(j,k)
can be linearized as 可以线性化为
p(i,j,k) <= g(i,k)
p(i,j,k) <= g(j,k)
p(i,j,k) >= g(i,k)+g(j,k)-1
p(i,j,k) ∈ {0,1}
Usually we don't need all of these variables and equations, but that depends on the details of the model. 通常我们不需要所有这些变量和方程式,但这取决于模型的细节。 For sure we should only consider i<j
. 当然,我们应该只考虑i<j
。 Interestingly, this formulation is so tight, we can relax p(i,j,k) to be continuous between 0 and 1: they will be integer automatically. 有趣的是,该公式是如此紧密,我们可以将p(i,j,k)放宽到0和1之间的连续值,它们将自动为整数。
This mathematical description is easily transcribed into Python/Pulp. 这个数学描述很容易被转录成Python / Pulp。 You probably should redo your Python code as it has some nonsensical things. 您可能应该重做您的Python代码,因为它包含一些荒谬的内容。 Some hints: 一些提示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.