简体   繁体   English

如何在Gurobi中编码线性组合

[英]How to code a linear combination in Gurobi

I have found the following example as on how to code the constraint "the sum over all a_{i,j}*x_{i,j} with j element of J is <=5 for all i in I". 我发现了以下关于如何对约束进行编码的示例“对于I中的所有i,所有具有j的j元素的a_ {i,j} * x_ {i,j}的总和<= 5”。 (sorry, I don't know how to properly write this in code, here is a first try): (对不起,我不知道如何用代码正确地编写此代码,这是第一次尝试):

\sum{a_{i,j}*x_{i,j}} <= 5 

in Gurobi python: 在Gurobi python中:

m.addConstr(x.prod(a,i,'*') <=5 for i in I)

I would really appreciate if someone could explain this expression to me! 如果有人可以向我解释此表达,我将不胜感激!

Here is what I understand: m is usually used as a variable to store the model, in a program we would start with m = Model() to create an empty model. 这是我的理解:m通常用作存储模型的变量,在程序中,我们将从m = Model()开始创建一个空模型。 We add one constraint with addConstr() and x must have been introduced as a variable. 我们使用addConstr()添加一个约束,并且x必须作为变量引入。 the x.prod() method creates a linear combination of its parameters. x.prod()方法创建其参数的线性组合。 However, I don't know how to interpret the three parameters a, i, '*' i is part of the for loop, it starts with 1 and then gets incremented by one until the last element of I is reached. 但是,我不知道如何解释三个参数a, i, '*' i是for循环的一部分,它以1开头,然后递增1,直到到达I的最后一个元素。 '*' is used as a wildcard and will address just any element. “ *”用作通配符,将仅处理任何元素。 But what does this combination of a, i, '*' tell me? 但是a, i, '*'告诉我什么?

a is a parameter and has two index i,j. a是一个参数,具有两个索引i,j。 ' * ' means for all j. '*'表示所有j。 Using ' * ', you don't need "for loop" for j like, "for loop" for i. 使用'*',就不需要j的“ for循环”,i则不需要“ for循环”。 You can see that in explicit form at the slayt which has two "for loop". 您可以在带有两个“ for循环”的slayt上以显式形式看到它。

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

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