Page 1 of 1

LOOP Problem

Posted: Wed Dec 13, 2017 3:42 pm
by Ali
Hi,

I want to code the following BASIC program into GAMS. This program computes variable Z based on variable L and assumes that L in the last period is zero (T= 0,...,10). Reversely, it computes L variable up to the first time. Below please find my loop command but it doesn't give me the correct loop answer.

Thanks for the help,

0 REM PROGRAM 1.1: MINE PROBLEM
20 DIM X(11},Y(11).Z(11).L(11}
30 L(10) = 0
40 FOR T = 9 TO 0 STEP -1
50 Z(T} = (1- L(T+1))
60 L(T) = L(T+1) + Z(T)^2
70 NEXT T
80 X(0) = 1000
90 FOR T = 0 TO 9
100 Y(T) = X(T} * Z(T)
110 X(T+1) = X(T) - Y(T}
120 NEXT T
130 LPRINT " T X(T} Y(T) L(T)"
140 LPRINT "------------------------------------------------"
150 LPRINT 0,X(0),Y(0)
160 FOR T = 1 TO 10
170 LPRINT T,X(T),Y(T),L(T}
180 NEXT T
190 END


set t Time periods /0*10/;

variable L(t);

L.up(t) = 0;

loop(t, L.l(t) = L.l(t+1)+((1-L.l(t+1)/2)**2));

display L.l;

Re: LOOP Problem

Posted: Wed Dec 13, 2017 10:54 pm
by dirkse
Have you had a chance to look over the rules for this forum (see the Rules tab)? It's good form to provide your name and affiliation. That increases the chance you'll get a helpful answer.

In this case, divide and conquer is a good strategy. The first loop in the BASIC code computes L and Z, but it does so with a backwards loop. Your loop is forwards, so that won't work. Also, you can just use parameters here, since you are doing computations. Some example code is below. If you run it in the IDE with gdx=lookAtMe and then view the lookAtMe.gdx file in the GDX browser of the IDE you can have a good look at not only the output L and Z but also the interesting sets here: tLast, revt, etc.

-Steve

Code: Select all

$ontext
 0 REM PROGRAM 1.1: MINE PROBLEM
20 DIM X(11},Y(11).Z(11).L(11}
30 L(10) = 0
40 FOR T = 9 TO 0 STEP -1
50 Z(T} = (1- L(T+1)) / 2
60 L(T) = L(T+1) + Z(T)^2
70 NEXT T
$offtext

set t / 0 * 10 /;
singleton set tLast(t);
tLast(t) = card(t) eq ord(t);
alias (t,rt);
set revt(t,rt) 'reverse t';
revt(t,t+[card(t)-2*ord(t)+1]) = yes;
display revt;

parameters L(t), Z(t);
L(t) = na;
L(tLast) = 0;
loop {revt(t,rt)$[not tLast(rt)],
  Z(rt) = (1 - L(rt+1)) / 2;
  L(rt) = L(rt+1) + sqr(Z(rt));
};
display L;

Re: LOOP Problem

Posted: Thu Dec 14, 2017 7:55 pm
by Ali
Dear Steve,
Hi

Thanks for your kind reply. But I got an error message for "singleton set" by GMAS IDE. Would you please let me know, where is the problem ?

Regards,
Ali

Re: LOOP Problem

Posted: Fri Dec 15, 2017 9:21 am
by bussieck
You are probably using an old version of GAMS. Singleton sets where introduced in 24.6 (7/2014). You probably should update your GAMS system.

-Michael