condition to define a constraint

Problems with modeling
Post Reply
montanhe
User
User
Posts: 9
Joined: 7 years ago

condition to define a constraint

Post by montanhe »

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
Manassaldi
User
User
Posts: 118
Joined: 7 years ago
Location: Rosario - Argentina

Re: condition to define a constraint

Post by Manassaldi »

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
User avatar
dirkse
Moderator
Moderator
Posts: 215
Joined: 7 years ago
Location: Fairfax, VA

Re: condition to define a constraint

Post by dirkse »

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:

Code: Select all

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
montanhe
User
User
Posts: 9
Joined: 7 years ago

Re: condition to define a constraint

Post by montanhe »

Dear all,

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

Kind regards,
Tiago
User avatar
bussieck
Moderator
Moderator
Posts: 1038
Joined: 7 years ago

Re: condition to define a constraint

Post by bussieck »

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
Post Reply