## Change dimension Topic is solved

Problems with syntax of GAMS
jonathanl91
User
Posts: 5
Joined: 3 months ago

### Change dimension

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?

bussieck
Moderator
Posts: 502
Joined: 4 years ago

### Re: Change dimension

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

jonathanl91
User
Posts: 5
Joined: 3 months ago

### Re: Change dimension

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?

bussieck
Moderator
Posts: 502
Joined: 4 years ago

### Re: Change dimension

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