How can I force GAMS to compile this code in its written order?

Problems with syntax of GAMS
Post Reply
moosavi_69
User
User
Posts: 31
Joined: 6 years ago

How can I force GAMS to compile this code in its written order?

Post by moosavi_69 »

Dear friends,

When I run the following code, GAMS does not compile this code in its written order. Could you pls help me out how I can force it to do so?

$onmulti
Variable Z;

$GDXIN Name1.gdx
$LOAD Z *** the value of Z here is equal to a***
$GDXIN
Display Z.l;

$GDXIN Name2.gdx
$LOAD Z *** the value of Z here is equal to b***
$GDXIN
Display Z.l;


But GAMS displays these values as follows:
Z => b
Z => b
cladelpino
User
User
Posts: 108
Joined: 7 years ago

Re: How can I force GAMS to compile this code in its written order?

Post by cladelpino »

GAMS makes two different "passes" of the file when "preparing" the model, they are usually referred to as "compile" and "execution" phase or time.

"compile" comes first, then "execution".

As you can see in https://www.gams.com/latest/docs/UG_GDX.html $gdxin works at "compile" time, while "display" is interpreted at "execution" time.

Therefore, your code is actually executed in this way by gams:

Code: Select all

$GDXIN Name1.gdx 
$LOAD Z *** the value of Z here is equal to a***
$GDXIN

$GDXIN Name2.gdx
$LOAD Z *** the value of Z here is equal to b***
$GDXIN

Display Z.l;
Display Z.l;
This is the WHY of the behaviour you observe. How to obtain a different behaviour depends on what you are trying to achieve, if you let us know then we can suggest a different design.

Best
Claudio
moosavi_69
User
User
Posts: 31
Joined: 6 years ago

Re: How can I force GAMS to compile this code in its written order?

Post by moosavi_69 »

Dear Claudio,

First of all, I have to thank you very much for your time.

In fact, I want the first display Z.l shows the value "a" and the second display Z.l shows the value of "b". For more clarification, pls look at the following code:


$GDXIN Name1.gdx
$LOAD Z *** the value of Z here is equal to a***
$GDXIN

$GDXIN Name2.gdx
$LOAD Z *** the value of Z here is equal to b***
$GDXIN

Display Z.l; ==> shows the value of a
Display Z.l; ==> shows the value of b



If my clarification is not clear, pls let me know. If you think it would be helpful, I will upload both the GDX files and written code.

Plus, if I want to write these values in an Excel file, I encounter the same problem. Considering the rule you beautifully mentioned, i.e., "compile" comes first, then "execution". , I think the solution to the problem mentioned earlier would solve the later one.

Anyway, THX a Galaxy
User avatar
Gideon Kruseman
User
User
Posts: 24
Joined: 6 years ago

Re: How can I force GAMS to compile this code in its written order?

Post by Gideon Kruseman »

$gdxin is a compile time statement
display is an execute time statement

trying to link them is bound to give you problems, unless this is the only thing you are doing in the model.

The two $gdxin statements basically mean that that the second overrules the first.
unless you assign the values of z to a different GAMS item, which you obviously will have to declare first. Since I have no clue of the dimensions, I leave it up to you to figure that one out.

$GDXIN Name1.gdx
$LOAD a=Z *** the value of Z here is equal to a***
$GDXIN

$GDXIN Name2.gdx
$LOAD b=Z *** the value of Z here is equal to b***
$GDXIN

Display a.l; ==> shows the value of a
Display b.l; ==> shows the value of b
Gideon Kruseman
ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData
cladelpino
User
User
Posts: 108
Joined: 7 years ago

Re: How can I force GAMS to compile this code in its written order?

Post by cladelpino »

moosavi_69 wrote: 6 years ago In fact, I want the first display Z.l shows the value "a" and the second display Z.l shows the value of "b".
To make it more clear from my post and Gideon's: you can't.

I think you missed my line opening the code example:
Therefore, your code is actually executed in this way by gams:
i.e. What we've tried to portray by reordering your code is that "$gdxin" statements will be always executed first than "display" statements, no matter where they are actually written in the file.

Best
Post Reply