optimizationmodelgams-math

How can I use IF and ELSE IF in looping and display 2 statements in GAMS?


I am a beginner level in this program. I try to improve this loop according to this condition. The details are as follows:

When CUTI(k) = CUTI(k)-4 then,

1)If the result shows this CUTI(k) value greater than 0, then print this CUTI(k) value.

2)If the result shows CUTI(k) value less than 0, then print this CUTI(k) value is added 12 with showing a word "*" after the number in display, e.g. 10*, 9*

I am not sure this loop is correct and enough to add this condition. Look forward to seeing your recoomendation. :)

set k /1*20/;
parameter
CUTI(k)/1 6, 2 2, 3 8, 4 5, 5 1, 6 3, 7 7, 8 8, 9 6, 10 8,11 1, 12 2, 13 4, 14 7,
        15 5, 16 2, 17 8, 18 9, 19 2, 20 10/;

loop(k,
if(CUTI(k)-4 > 0,
    CUTI(k) = CUTI(k)-4;
else 
    CUTI(k) = (CUTI(k)-4)+12 ;    
)
);  
display CUTI;

Solution

  • Your logic looks correct. However, instead of the loop/if/else you could simplify this to one assignment:

    CUTI(k) = CUTI(k)-4+12$(CUTI(k)<=4);
    

    However, modifying the display statement by adding a * to some elements is not possible. If you need to distinguish the cases in such a statement, you might assign the values to two different parameters and display them individually.