1

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 Answers1

0

(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 :)

  • Thank you very much! I've got it that I need not to include the Loop to the Equations. I'm not sure, should I have one or two programs. I think, my task is very simple, but I don't see a quick solution for it. So, I thought that I can have only one program with the Loop contruct at the end. But in this case (I do as you advise) I get the error 141 (The parameter without data is used). How can I end the first program and to start another, if necessary? Also I saw Binary variable (ex. "absmip"), but I don't understand how it works. Maybe it's correct way? – Alex Lavrik Oct 07 '20 at 22:55
  • Well, in mathematical programming model you cannot use control flow features. ( I came from a background of normal programming so I believe I know what is going on with you). Go to this link and go through all the examples https://www.gams.com/latest/docs/UG_FlowControl.html#UG_FlowControl_IfExamples. You will see that what you want is not permitted and maybe you will get an idea of what flow control is in a software such as GAMS. – Raquel Aguiar Oct 08 '20 at 09:19
  • And equations in GAMS are for the mathematical programing model. The mathematical equations that define your problem. As you can see here: https://www.gams.com/latest/docs/UG_Equations.html. So, you can never have an "if" in the middle of mathematical model. There are however some ingenious strategies to go around those problems, as for example, big M constraints. – Raquel Aguiar Oct 08 '20 at 09:35
  • Or there is even the case in which D2(t) could just be an output you are interested in and calculate _after_ obtaining a solution to the mathematical model, including only D(t) as the variable. You can try it by making D2 a parameter and placing those loops after the solve statement. – Raquel Aguiar Oct 08 '20 at 09:56
  • Also, to answer your specific question, when you have a solve statement, the model you defined above is solved. After statement you can manipulate the solution and load new data to the same parameter and place a second solve statement that will solve the same model but with the different data. – Raquel Aguiar Oct 08 '20 at 10:01
  • 1
    Now I've founded the answer to my question. Perhaps, I did not explain the problem well enough. So, I should to declare D2 as Positive variable, and to use the equation D2(t) =g= D(t). Thanks a lot for your help! – Alex Lavrik Oct 14 '20 at 15:00
  • Sorry! I did not realize what was your problem, but I'm glad you figured it out :) – Raquel Aguiar Oct 15 '20 at 13:12