Error 121 - Issue with indexing gdx data Topic is solved

Problems with syntax of GAMS
Post Reply
User avatar
sanjadzeletovic
User
User
Posts: 6
Joined: 3 weeks ago

Error 121 - Issue with indexing gdx data

Post by sanjadzeletovic » 3 weeks ago

Hi,

I have difficulties in reading data that I need from .gdx file. I have succeeded in loading file from Excel, but I get an error when I try to access index that I need:

Code: Select all

Sets
   t time intervals       / t1*t12 /   [b] !! set definition[/b]
   i customers          / c1*c5 /
   r row labels         /r1*r97/
   c column labels     /c1,c2,c3,c4,c5/;

parameter p(r,c);  [b]!! reading the table from Excel[/b]
$CALL GDXXRW Curve.xlsx trace=3 par=p rng=Sheet1!a1:f97 rdim=1 cdim=1
$gdxin Curve.gdx
$load p
$gdxin

parameter h; [b] !! reading the scalar[/b]
$CALL GDXXRW load.xlsx trace=3 par=h rng=Sheet1!i2:i2 dim=0
$GDXIN load.gdx
$LOAD h
$GDXIN

[b]cost .. z =e= sum(t, maxLoad -  sum(i, u(i,t)*cons(i)*clpu(t)*p(h,i)));[/b]
current_load(t) ..  sum(i, u(i,t)*cons(i)*clpu(t)*p(i,h)) =l= maxLoad;

Code has multiple errors, but the first one I have difficulties with is when reading p(h,i) (line that is bold). I need data from gdx that is in the h column that I read also from other gdx, and i-th row that is the same as i that I am using for other variables.
I am not sure if the code that I have attached is enough, so sorry in advance if something is missing.

Thank you,
Sanja

Fred
Posts: 60
Joined: 1 year ago

Re: Error 121 - Issue with indexing gdx data

Post by Fred » 3 weeks ago

Sanja,

I suggest that you share the xlsx files to give other users the chance to reproduce the issues. That will make helping you a lot easier.

Best,
Fred

User avatar
sanjadzeletovic
User
User
Posts: 6
Joined: 3 weeks ago

Re: Error 121 - Issue with indexing gdx data

Post by sanjadzeletovic » 3 weeks ago

Thanks for the advice.
I have attached xlsx files. Files are the same but I couldn't find the way to read both data from the same file.
Attachments
load.xlsx
File with table
(19.41 KiB) Downloaded 6 times
Curve.xlsx
File from which I am reading scalar
(19.43 KiB) Downloaded 5 times

Fred
Posts: 60
Joined: 1 year ago

Re: Error 121 - Issue with indexing gdx data

Post by Fred » 3 weeks ago

Sanja,

You declared parameter p(r,c) where set r contains labels r1*r97. The way you use GDXXRW and load p, I would expect column A of Curve.xlsx to contain r1,r2,...,r97. However, this is not the case. Column A contains times (12:00:00 AM, 12:15:00 AM, 12:30:00 AM, etc). Hence, on $load p, the domain checking filters all entries and parameter p is empty. Not sure what you need the time information for. I changed the Excel file and introduced an additional column for the row labels r1,r2,... (note that there are only 96 data rows). I left the times in the excel file but do not read them anymore. I also did some minor changes to the code to read the data from just one Excel file.

Code: Select all

Sets
   t time intervals       / t1*t12 /  
   i customers          / c1*c5 /
   r row labels         /r1*r97/
   c column labels     /c1,c2,c3,c4,c5/;

parameter p(r,c), h; 
$CALL GDXXRW Curve.xlsx trace=3 par=p rng=Sheet1!b1:g97 rdim=1 cdim=1 par=h rng=Sheet1!i2:i2 dim=0
$gdxin Curve.gdx
$load p h
$gdxin
display p, h;
I hope this helps!

Best,
Fred
Attachments
Curve.xlsx
(20.57 KiB) Downloaded 3 times

User avatar
sanjadzeletovic
User
User
Posts: 6
Joined: 3 weeks ago

Re: Error 121 - Issue with indexing gdx data

Post by sanjadzeletovic » 3 weeks ago

Thank you Fred, this really helped. It is now much more clear how to transfer data from excel. I use time to calculate h, so that I know from which row I should read data for customers.
However, it is only partially solving my problem. I still don't know how to use this data :( . I have problem with indexing. I am not clear why this does not work:

Code: Select all

cost .. z =e= sum(t, maxLoad -  sum(i, u(i,t)*cons(i)*clpu(t)*p(h,i)));
So now I want to read from gdx field of p that I have loaded. This field is changing with i, for fixed row h, and I am getting errors:

--- SR.gms(52) 3 Mb 3 Errors
*** Error 121 in C:\Users\sbajic\OneDrive\McGill\Research\GAMS\SR\SR.gms
Set expected
*** Error 171 in C:\Users\sbajic\OneDrive\McGill\Research\GAMS\SR\SR.gms
Domain violation for set
*** Error 148 in C:\Users\sbajic\OneDrive\McGill\Research\GAMS\SR\SR.gms
Dimension different - The symbol is referenced with more/less
indices as declared

Fred
Posts: 60
Joined: 1 year ago

Re: Error 121 - Issue with indexing gdx data

Post by Fred » 3 weeks ago

Sanja,

P is a parameter with domain (r,c). In the cost equation you use p(h,i) where h is a scalar and i is a different set than c.
I can only guess what you actually try to do:
1) Sets i and c have the same content, so I assume you can actually just get rid of c and declare p(r,i) instead of p(r,c).
2) In the equation you use h which is a scalar in the domain of p. That doesn't make sense. I assume that you want fpr example use 'r14' if h=14. The following should do the trick:

Code: Select all

Sets
   t time intervals       / t1*t12 /  
   i customers          / c1*c5 /
   r row labels         /r1*r97/ ;

parameter p(r,i), h; 
$CALL GDXXRW Curve.xlsx trace=3 par=p rng=Sheet1!b1:g97 rdim=1 cdim=1 par=h rng=Sheet1!i2:i2 dim=0
$gdxin Curve.gdx
$load p h
$gdxin
display p, h;

[...]

cost .. z =e= sum(t, maxLoad -  sum((i,r)$[ord(r)=h], u(i,t)*cons(i)*clpu(t)*p(r,i)));
If you are not familiar with the ord() operator, you can find some documentation here: https://www.gams.com/latest/docs/UG_Ord ... rdOperator

I hope this helps!

Fred

User avatar
sanjadzeletovic
User
User
Posts: 6
Joined: 3 weeks ago

Re: Error 121 - Issue with indexing gdx data

Post by sanjadzeletovic » 3 weeks ago

Fred,

Thank you, I haven't been aware of this operator. This solved my problem.

Post Reply