简体   繁体   English

线性编程中布尔值之间的乘法(Python,Pulp库)

[英]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: 一些提示:

  • binary variables already have bounds 0 and 1 二进制变量已经具有边界0和1
  • Pulp can do equality constraints (writing <= and >= constraints is silly) 纸浆可以做相等约束(写<=和> =约束很愚蠢)
  • try to make things more readable (closer to the mathematical representation) 尝试使事物更具可读性(更接近数学表示形式)
  • for a different approach see wedding.py 有关其他方法,请参见wedding.py

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

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