Exporting data from MATLAB to GAMS

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

Exporting data from MATLAB to GAMS

Post by Bahar_Mdi »

Dear all,

I am trying to import data from MATLAB to GAMS. the sets and parameters result from a heuristic algorithm that I can not manipulate the values. Consider I have a set i as follows /10,5,2,9/. I need to define this set and some parameters on this set. bet the parameters which are defined on this set are not imported correctly. the values are zero. but when I use $LOADDC in GAMS, I get this error: (649) Error violation when loading from GDX file.
Therefore I redefine set i which includes ordered numbers as /1,2,3,4/. in this situation, the parameters are imported correctly. but this is not ideal for me and I need to define sets in the order which is resulted from the heuristic algorithm and define parameters base on that order.

I appreciate it if someone can help me.
Thanks and regards,
Bahar
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Exporting data from MATLAB to GAMS

Post by bussieck »

There are different ways to deal with this ordering issue:

1) Have a two dimensional set instead of a one dimensional set to preserve the order:

Code: Select all

set i / 1*100/; alias (i,j);
set order(i,j) / 1.10,2.5,3.2,4.9/;
* iterate through i in user order:
loop(order(i,j), put_utility 'log' / j.tl);
2) Rely on the order specified in the GDX file (you can't specify the set i in other ways inside GAMS):

Code: Select all

set i; set order(i<);
$gdxin fromMatlab
$loadDC order
-Michael
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

Re: Exporting data from MATLAB to GAMS

Post by Bahar_Mdi »

Thanks for your respone.
How should I use the second approach?
When I add the set order(i<), I get errors 10,185,492,649.
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: Exporting data from MATLAB to GAMS

Post by dirkse »

Bahar,

It would be helpful for you to post a short piece of Matlab code that produces the GDX file that is giving you problems. And send also the GDX file, and a short .gms file that reads the data from GDX. With all this, you will perhaps solve your own problem. If not, you'll get some good help.

-Steve
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

Re: Exporting data from MATLAB to GAMS

Post by Bahar_Mdi »

Thanks for your reply.
This is my code in MATLAB:
-----------------------
Ps.name='p';
Ps.type='set';
Ps.val=[1:84]';
Ps.dim=1;

Is.name='i';
Is.type='set';
Is.val=[1;9;6;15];
Is.dim=1;

P8s.name='zb';
P8s.type='parameter';
P8s.form='full';
P8s.val=[0 1 0 0;0 0 1 0;0 0 0 1;0 0 0 0];
P8s.dim=2;

wgdx('data1',Ps,Is,P8s);
----------------------------------------------

- and the following is my code in GAMS:
----------------------------------------------
$GDXIN 'data1'

SETS
k/1/
p
i;

alias (i,j);

$LOADDC i,p

PARAMETERS
zb(i,j);

$LOADDC zb
$GDXIN 'data1'

display i,p,zb;
--------------------------------------------------
data1.gdx
The gdx file
(984 Bytes) Downloaded 261 times

And the gdx file is attached.
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Exporting data from MATLAB to GAMS

Post by bussieck »

Since you unload ps first the label order is fixed to 1,2,3,4... and hence the order in i (1;9;6;15) has no consequence to the order in the GDX file not in GAMS. So my second "solution" won't work (and you seem to work with an old GAMS system that does not have this capability anyway). You would have to implement my first suggestion and make i a 2-dim set.

-Michael
Post Reply