condition to define a constraint

Dear all,

I have the following setting

set i /1*10/
alias(i, j);

equations myconstraint(i,j);

myconstraint(i,j) … function(i,j) =e= 0;


and would like to define myconstraint(i,j) only if i, j = 1:10 when i <> j (for example when
defining the distance between points i and j). What is the correct way to define it? I tried
several variants with the $ conditional with no success

Kind regards,
Tiago

Hi, I think you can define a parameter that contains the distance between the two points (distance (i, j)).
For example, the following equation is defined only if the distance between points is greater than or equal to 3

myconstraint(i,j)$(distancie(i,j) ge 3)… function(i,j) =e= 0;

I hope this can help you

The quick way to do this is with a dollar condition in the constraint:

set i /1*10/
alias(i, j);
equations myconstraint(i,j);
myconstraint(i,j)$[not sameas(i,j)] … function(i,j) =e= 0;

The logic is all part of the myconstraint definition. Since it’s simple, that works well.

For more complicated conditions, it’s better to create a dynamic set and use that. For example, I could add this code:

set sub(i,j);
sub(i,j) = [not sameas(i,j)] and [(ord(i) - 2) <= ord(j)] and [(ord(i) + 2) >= ord(j)];
execute_unload 'lookAtMe', i,j, sub;
equation con2(i,j);
con2(sub(i,j)) .. function(i,j) =e= 0;

If I look at the set sub(i,j) in the GDX browser of the IDE (just open the file lookAtMe.gdx), I will get a nice picture of the data and will know what’s right or wrong.

HTH,

-Steve

Dear all,

I fixed my problem using the dollar condition in addition to the ord function.

Kind regards,
Tiago

You can’t redeclare an equation. What you can do it work with slacks to turn an =e= into a =l=:

positive variable slack(i,j);

myconstraint(i,j) … function(i,j) =E= 1 - slackl(i,j);

First you solve with all slacks fixed to 0:

slack.fx(i,j) = 0;

Next you set upper bound for the (i,j) pairs to INF where x.l(i,j)>1e-6 (never compare “>0” with floating point numbers):

slack.up(i,j)$(x.l(i,j)>1e-6) = INF;

Now the =e= has turned into a =l= for the (i,j) with x.l(i,j)>1e-6 from the previous solve.

-Michael