且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果条件在放大器中

更新时间:2023-09-09 22:59:28

您不能在约束的全部"部分(在AMPL中,{...}内部的部分)中包含决策变量.取而代之的是,您需要在约束本身中构建表示约束仅在X[p,r] = 0处于活动状态的逻辑.这样做的方式取决于约束的类型:> =,=或< =.我将分别编写每种情况,并以通用的方式而不是针对您的问题进行处理.

You cannot include a decision variable in the "for all" part of the constraint (in AMPL, the part inside the {...}). Instead, you need build into the constraint itself the logic that says the constraint is only active if X[p,r] = 0. The way to do that depends on the type of constraint: >=, =, or <=. I'll write each case separately, and I'll do it in a generic way instead of specific to your problem.

在下面的说明中,我假设约束条件写为

In the explanation below, I assume that the constraint is written as

a[1]y[1] + ... + a[n]y[n] >=/=/<= b, 

其中,a[i]b是常量,而y[i]是决策变量.我还假设如果x = 0希望约束保持不变,其中x是二进制决策变量,并且我们不关心如果x = 1约束是否成立.

where a[i] and b are constants and y[i] are decision variables. I also assume we want the constraint to hold if x = 0, where x is a binary decision variable, and we don't care whether the constraint holds if x = 1.

M为等于大数的新参数(常量).

Let M be a new parameter (constant) that equals a large number.

大于或等于约束:

约束为a[1]y[1] + ... + a[n]y[n] >= b.将其重写为

a[1]y[1] + ... + a[n]y[n] >= b - Mx.

然后,如果x = 0成立,则约束成立,如果x = 1成立,则该约束无效,因为右侧非常负.

Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the right-hand side is very negative.

(如果所有a[i]均为非负数,则可以改用

(If all of the a[i] are nonnegative, you can instead use

a[1]y[1] + ... + a[n]y[n] >= bx,

更严格.)

小于或等于约束:

约束为a[1]y[1] + ... + a[n]y[n] <= b.将其重写为

a[1]y[1] + ... + a[n]y[n] <= b + Mx.

然后,如果x = 0,则约束成立,而如果x = 1,则由于RHS很大而无效.

Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the RHS is very large.

平等约束:

约束为a[1]y[1] + ... + a[n]y[n] = b.将其重写为

a[1]y[1] + ... + a[n]y[n] <= b + Mx
a[1]y[1] + ... + a[n]y[n] >= b - Mx.

然后,如果x = 0,则相等约束成立,如果x = 1,则约束无效.

Then, if x = 0, the equality constraint holds, and if x = 1, the constraints have no effect.

注意:如果您的模型相对较大,即解决它花费的时间不可忽略,那么您需要小心使用大型M型公式.特别是,您希望M尽可能小,同时仍要强制执行上述约束的逻辑.

Note: If your model is relatively large, i.e., it takes a non-negligible amount of time to solve, then you need to be careful with big-M-type formulations. In particular, you want M to be as small as possible while still enforcing the logic of the constraints above.