Elements and set assignments

Problems with syntax of GAMS
Post Reply
j_Albert
User
User
Posts: 6
Joined: 2 years ago

Elements and set assignments

Post by j_Albert »

Hello,

I am new at GAMS, so this might seems trivial, but I haven´t been able to solve this problem.

I have the following code to retrieve data from a .gdx file.

Code: Select all

*1st
Parameters
evfb_1(endw, acts, reg)		!!this is the variable that contains the data
evfb_la(aez, acts, reg)
 ;
loop(endw$aez(endw),
            evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
            );
where the set aez(endw) is a subset of endw. Basically I want to make the variable evfb_la(aez, acts, reg) with only a subset of data from evfb_1(endw, acts, reg). But for some reason, in the new variable it copies all but one region.

Instead, if I do like this

Code: Select all

*2nd
loop((aez,acts,reg),
evfb_la("AEZ1", acts, reg) = evfb_1("AEZ1", acts, reg);
evfb_la("AEZ2", acts, reg) = evfb_1("AEZ2", acts, reg);
.
.
.
)
;
it does copies all observations for all the new parameters, but it is really cumbersome as there are many elements in both sets "endw" and "acts".
I have tried the following alternatives (below)

---> Alternatives 1,2,3,4,5,6 copies all but one region. Even even though the set reg is the same for both parameters. I tried doing $onEps to maybe avoid some 0 values being the problem, but to no avail, it drops one region completely

---> Alternatives 7,8,9 show error 149

---> I also tried implicit set definition, by not declaring set aez as a subset of set endw, and then doing "evfb_la(aez<,acts,reg)". But the problem persists. In the .gdx the set endw shows all the aez elements, but I keep having the same problem with the parameter, that is not copying all values.

Code: Select all

*1st try to make it shorter
$offtext
$ontext

loop(endw$(aez(endw)),
            evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg);
            )
            ;
;

$ontext
*$offtext
*2nd try*****************************************************

loop((reg,acts,endw),
            evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
            );
$ontext
*3rd try*****************************************************

loop(reg,
    loop(acts,
        loop(endw$(aez(endw)),
            evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)

)))
;
*$offtext
*4th try*****************************************************
loop(endw,
if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
)
)
;
$ontext
*$offtext
*5th try*****************************************************
loop((reg,acts,endw),
if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
)
)
;
*$offtext
*6th try*****************************************************
loop(endw,
if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
)
)
;
*$offtext
*7th try*****************************************************
*$offtext

if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg);
);

*$offtext
*8th try*****************************************************
*$offtext
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg);

*$offtext
*9th try*****************************************************
*$offtext

$offtext
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)

I would appreciate any insights on why it copies all data but one region and in general on how to avoid these types of problems.

Thank you
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Elements and set assignments

Post by bussieck »

What about this one:

Code: Select all

* 1st
Parameters
evfb_1(endw, acts, reg)		!!this is the variable that contains the data
evfb_la(aez, acts, reg)
 ;
loop(aez,
            evfb_la(aez, acts, reg) = evfb_1(aez, acts, reg)
            );
If that doesn't work provide a self-contained example and you exact expectations for evfb_la.

-Michael
j_Albert
User
User
Posts: 6
Joined: 2 years ago

Re: Elements and set assignments

Post by j_Albert »

dat.gdx
(22.82 KiB) Downloaded 111 times
Thank you Michael. Unfortunately the way you proposed shows Error 171.

I will attach a small code and the data set. If you run it like this, it will produce the variable evfb_la(aez,acts,reg) as I want it, by using the method of writing each element of aez(endw) in the loop.

Below are the alternatives. I made sure that they replicate the errors I am getting.
The data is in the attached file. Note that in it there is a version of evfb_la(aez,acts,reg) already in the form I need it

I appreciate any insights on what the problem might be.

Best,
JA

Code: Select all

$oneolcom
$onEps
$ONEMPTY

$gdxin 'DAT.GDX'        


set aez   "Land types"/
AEZ1*AEZ18
/;     

set
   acts           "Activities"/
pdr
wht
gro
v_f
osd
c_b
pfb
ocr
pcr
ctl
oap
rmk
wol
cmt
omt
frs
Extraction
ProcFood
TextWapp
LightMnfc
HeavyMnfc
Util_Cons
TransComm
OthServices
/;

set
   endw           "Endowments"/
AEZ1
AEZ2
AEZ3
AEZ4
AEZ5
AEZ6
AEZ7
AEZ8
AEZ9
AEZ10
AEZ11
AEZ12
AEZ13
AEZ14
AEZ15
AEZ16
AEZ17
AEZ18
UnSkLab
SkLab
Capital
NatRes
/
;
set
    reg             "Regions"/
Brz
EU_28
China
USA
ROW
/;
Parameters
 
    evfb_1   (endw, acts, reg)
    evfb_la  (aez,acts,reg)
;

execute_loaddc 'BRZFDAT.GDX',
    evfb_1
;
    

loop((aez,acts,reg),
evfb_la("AEZ1", acts, reg) = evfb_1("AEZ1", acts, reg);
evfb_la("AEZ2", acts, reg) = evfb_1("AEZ2", acts, reg);
evfb_la("AEZ3", acts, reg) = evfb_1("AEZ3", acts, reg);
evfb_la("AEZ4", acts, reg) = evfb_1("AEZ4", acts, reg);
evfb_la("AEZ5", acts, reg) = evfb_1("AEZ5", acts, reg);
evfb_la("AEZ6", acts, reg) = evfb_1("AEZ6", acts, reg);
evfb_la("AEZ7", acts, reg) = evfb_1("AEZ7", acts, reg);
evfb_la("AEZ8", acts, reg) = evfb_1("AEZ8", acts, reg);
evfb_la("AEZ9", acts, reg) = evfb_1("AEZ9", acts, reg);
evfb_la("AEZ10", acts, reg) = evfb_1("AEZ10", acts, reg);
evfb_la("AEZ11", acts, reg) = evfb_1("AEZ11", acts, reg);
evfb_la("AEZ12", acts, reg) = evfb_1("AEZ12", acts, reg);
evfb_la("AEZ13", acts, reg) = evfb_1("AEZ13", acts, reg);
evfb_la("AEZ14", acts, reg) = evfb_1("AEZ14", acts, reg);
evfb_la("AEZ15", acts, reg) = evfb_1("AEZ15", acts, reg);
evfb_la("AEZ16", acts, reg) = evfb_1("AEZ16", acts, reg);
evfb_la("AEZ17", acts, reg) = evfb_1("AEZ17", acts, reg);
evfb_la("AEZ18", acts, reg) = evfb_1("AEZ18", acts, reg);
)
;


**********************************************************
* Here are the alternative formulations that dont copy region "brz" or give error 171, or 149.
$ontext

*1st try*****************************************************
loop(endw$(aez(endw)),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
)
;

*$offtext
*2nd try*****************************************************

loop((reg,acts,endw),       
            evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)   
            );
$ontext
*$offtext
*3rd try*****************************************************

loop(reg,
    loop(acts,
        loop(endw$(aez(endw)),
            evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)

)))
;
$ontext
*$offtext
*4th try*****************************************************
loop(endw,
if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
)
)
;
$ontext
*$offtext
*5th try*****************************************************
loop((reg,acts,endw),
if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
)
)
;
$ontext
*$offtext
*6th try*****************************************************
loop(endw,
if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg)
)
)
;
$ontext
*$offtext
*7th try*****************************************************
*$offtext

if(aez(endw),
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg);
);
$ontext
*$offtext
*8th try*****************************************************
*$offtext
evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg);

*$offtext
*9th try*****************************************************
*$offtext


*evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg);

$offtext
execute_unload "results.gdx"

GAMS WF example.gms
(3.46 KiB) Downloaded 118 times
j_Albert
User
User
Posts: 6
Joined: 2 years ago

Re: Elements and set assignments

Post by j_Albert »

*** after declaring the variables, the line for uploading the var evfb_1 should read

Code: Select all

execute_loaddc 'DAT.GDX',
    evfb_1
;
j_Albert
User
User
Posts: 6
Joined: 2 years ago

UPDATE

Post by j_Albert »

I managed to solve it like this:

Code: Select all

loop((endw,aez)$(sameas(endw,aez)),
    evfb_la(aez, acts, reg) = evfb_1(endw, acts, reg) ;
)
;
Still, I am left wondering why in some cases it would appear to work fine, meaning that it copies over all sets, with the exception of one region (one element of set reg).
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Elements and set assignments

Post by bussieck »

The proper way to solve this is to move the declaration of aez after the declaration of endw and make aez a subset of aez (i.e. parameter aez(endw)) and then use "my" solution. Proper planing of set hierarchy is very important in the relational data model GAMS uses.

-Michael
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Elements and set assignments

Post by bussieck »

If there nothing else you want to do in the loop, why use a loop in the first place. GAMS shines when it can execute parallel statements, so why not just do `evfb_la(aez, acts, reg) = evfb_1(aez, acts, reg);` without the loop?

-Michael
j_Albert
User
User
Posts: 6
Joined: 2 years ago

Re: Elements and set assignments

Post by j_Albert »

Hi!

I took your advise and redid the whole set hierarchy appropriate to the model structure, and as predicted, it worked smoothly with only declaring the parameters over the subset, without the loop.

"evfb_la(aez, acts, reg) = evfb_1(aez, acts, reg);"

The lesson is clear, be careful when modifying existing models and work the set hierarchy from the uppermost to the lowest parameter and variable.

Thank you for your advise!
j_Albert
User
User
Posts: 6
Joined: 2 years ago

Re: Elements and set assignments

Post by j_Albert »

Hi!

I took your advise and redid the whole set hierarchy appropriate to the model structure, and as predicted, it worked smoothly with only declaring the parameters over the subset, without the loop.

"evfb_la(aez, acts, reg) = evfb_1(aez, acts, reg);"

The lesson is clear, be careful when modifying existing models and work the set hierarchy from the uppermost to the lowest parameter and variable.

Thank you for your advise!
Post Reply