我是ampl的新手,我想在if条件中使用以下信息:
I am new in ampl and I want to use if condition in ampl with the following information:
我有一个二进制变量X [p,r],其中{p中的p,R中的r}. 现在,我想进行一个新的约束,使得在X [p,r] = 0的情况下使用变量R [p,r]. 我不知道如何编写它,或者即使放大器不能处理它,我也尝试了以下约束,但是它们没有用:
I have a binary variable X[p,r], where {p in P, r in R}. Now I want to make a new constraint such that the variable R[p,r] is used where X[p,r]=0. I do not know how I can write it or even if the ampl can handle it or not, I tried the following constraint but they did not work:
s.t. a1{r in R, p in P and X[p,r]=0}: s.t. a2{r in R p in P and X[p,r]=0}; s.t. a2{r in R ,p in P, and X[p,r]=0}; s.t. a2{r in R, p in P: and X[p,r]=0};推荐答案
您不能在约束的全部"部分(在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.
更多推荐
如果条件在放大器中
发布评论