How to add a Condition with Variables GAMS

880 views Asked by At

I'm beginner in GAMS. So, my question relates to use of variables in conditions. I supposed, that it's Programming Flow Control Features, but I cannot to find an example with variables, not parameters.

In my task I have only one set (t) at the start of the code. In the "Equations" block GAMS executes variable D(t) for several values of (t), for example, for t=1..5. I would like to extract only positive values of D(t). For example, I have executed D(t): 3, -2, 5, -4, 1 I want to have D2(t): 3, 0, 5, 0, 1

I know, that it's not allowed to use dollar condition with variables. Also I cannot use loop+if construction for the same reason. For example, I tried to write something like this, but got a lot of errors (D2(t) was declared as it should):

Equation1(t)..    loop (t, 
     if ((D(t) < 0), 
     D2(t) = D(t) - D(t); 
     ); 
);

So, how can I add my condition and where should I place the code?

1

There are 1 answers

7
Raquel Aguiar On

(not an expert but I have to answer because I do not have enough reputation to just comment)

I assume that you have two different mathematical programs and you and to run the second program with some of the results of the first. If that is the case, the piece of code you showed above is between the two runs and there is no need for the "equation" part.

Also, after obtaining a solution, you access its values with .l (the level of the variable in the solution). Then, you might also state that the value of the variable D2 is equal to that of the variable D. If you just attribute a value (level) to the variable D2 before the second run, the model will be allowed to change it during the course of the search (if that is possible in your case). You can avoid it by fixing the value of the variable D2 by instead of using D2.l(t) using D2.fx(t).

loop (t, 
     if ((D.l(t) < 0), 
     D2.l(t) = D.l(t) - D.l(t); 
     else 
       D2.l(t) = D.l(t);
     ); 
);   

or 

loop (t, 
     if ((D.l(t) < 0), 
     D2.fx(t) = D.l(t) - D.l(t); 
     else 
       D2.fx(t) = D.l(t);
     ); 
);  

I hope this answer has helped :)