if condition in ampl

你说的曾经没有我的故事 提交于 2019-11-29 18:00:28

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, 

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.

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

Greater-than-or-equal-to constraints:

The constraint is a[1]y[1] + ... + a[n]y[n] >= b. Rewrite it as

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

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

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

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

which is tighter.)

Less-than-or-equal-to constraints:

The constraint is a[1]y[1] + ... + a[n]y[n] <= b. Rewrite it as

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

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

Equality constraints:

The constraint is a[1]y[1] + ... + a[n]y[n] = b. Rewrite it as

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!