Page 1 of 1

Change dimension

Posted: Mon Jun 22, 2020 9:10 am
by jonathanl91
Consider the following parameter:

parameters test(HUD,IUD) /
HUD02 .IUD15 15
HUD10 .IUD15 20
HUD10 .IUD20 30
/;

I want to replace the numbers in first dimension (HUD) with the numbers from the second dimension (IUD).
That is, if "IUD" is "IUD15", then "HUD" should be "HUD15" and so on.

That is, I want my finale parameters to be:

parameters test(HUD,IUD) /
HUD15 .IUD15 15
HUD15 .IUD15 20
HUD20 .IUD20 30
/;

How do I do that in a smart way?

Re: Change dimension

Posted: Tue Jun 30, 2020 1:19 pm
by bussieck
If there are not so many IUD elements, make a map between the IUD and the corresponding HUD (himap) and use this to create the new parameter:

Code: Select all

set HUD /HUD02,HUD10,HUD15,HUD20/;
set IUD /IUD15,IUD20/;
set himap(HUD,IUD) / HUD15.IUD15,HUD20.IUD20 /;
parameters test(HUD,IUD) /
HUD02 .IUD15 15
HUD10 .IUD15 20
HUD10 .IUD20 30
/;
parameter test2(HUD,IUD);
alias (HUD,HUD2);
loop((HUD,HUD2,IUD)$(test(HUD,IUD) and himap(HUD2,IUD)), test2(HUD2,IUD) = test(HUD,IUD));

option test2:0:0:1; display test2;
If there are many many IUD you need to do this programmatically. Embedded Python code can be used for this. Look at the example https://www.gams.com/latest/datalib_ml/ ... Split.html.

-Michael

Re: Change dimension

Posted: Wed Jul 01, 2020 3:38 pm
by jonathanl91
Perfect! Thanks a lot!

However, I will only have two records in test2.
It seems like GAMS automatically remove:

Code: Select all

HUD15 .IUD15 15
because there is already a record with the same dimension - that is:

Code: Select all

HUD15 .IUD15 20
Is it possible - within the loop - to sum the value if the dimension is going to be the same. So my final results is:

Code: Select all

parameters test(HUD,IUD) /
HUD15 .IUD15 35
HUD20 .IUD20 30
/;
where the number 35 comes from 20 + 15?

Re: Change dimension

Posted: Thu Jul 02, 2020 6:01 am
by bussieck
Sure. But you should understand the GAMS code yourself. It you understand what it does the change is trivial:

Code: Select all

set HUD /HUD02,HUD10,HUD15,HUD20/;
set IUD /IUD15,IUD20/;
set himap(HUD,IUD) / HUD15.IUD15,HUD20.IUD20 /;
parameters test(HUD,IUD) /
HUD02 .IUD15 15
HUD10 .IUD15 20
HUD10 .IUD20 30
/;
parameter test2(HUD,IUD); test2(HUD,IUD) = 0;
alias (HUD,HUD2);
loop((HUD,HUD2,IUD)$(test(HUD,IUD) and himap(HUD2,IUD)), test2(HUD2,IUD) = test(HUD,IUD)+test2(HUD2,IUD));
option test2:0:0:1; display test2;
-Michael