Matlab-gams wgdx

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

Matlab-gams wgdx

Post by bigakrim »

Hi everyone
I want to solve a gams code in matlab and get the cost function, the objective function for each update of some parameters and set. I wrote the gams code which works perfectly. I created in gams the gdx file with the parameters and set that I want to update in matlab.
here is the coding
$set matout "'gams_matlab.gdx' cost.l, R_m, R_p, Q_m, Q_p, t " where t is the set, R_m, R_p, Q_m, Q_p the parameters and cost the variables.

In matlab, I create the structures for the parameters and set and use wgdx command to write these parameters and set such they can be updated in gams. the structure are well created in matlab.
here is the coding

Q_m = struct('name','Q_m','val', [4]);
Q_p = struct('name','Q_p','val',[0]);
R_m = struct('name','R_m','val',[4]);
R_p = struct('name','R_p','val',[0]);
t = struct('name','t','val','s1');

But unfortunately, running the wgdx('gams_matlab.gdx', Q_m, Q_p, R_m, R_p, t); I am getting error: Error using wgdx
Non-positive coordinates are not allowed in index columns of sparse data

here is the rest of the coding
cost = gams('myopic_gams_matlab.gms LP=cplex', t, Q_m, Q_p, R_m, R_p);
reward = cost.val;
please help me. the txt file is the matlab code
data_opt_lookup_inc.xlsx
(2 MiB) Downloaded 263 times
gams_matlab.txt
(672 Bytes) Downloaded 281 times
myopic_gams_matlab.gms
(11.53 KiB) Downloaded 281 times
renke.kuhlmann
User
User
Posts: 5
Joined: 2 years ago

Re: Matlab-gams wgdx

Post by renke.kuhlmann »

Hi,

your wgdx input structures are incorrect. If you don't provide the field "type", it is assumed "set". And if you don't set "form" to "full" you can't pass a single value as it is assumed "sparse". In sparse format you have to provide a n+1 column matrix where the first n columns are the domain entries and the last one are the actual values. Domain entries means pointers into the "uels" field. In your case, since your parameters are scalars it makes sense to use the "full" form.

This should work for you:

Code: Select all

Q_m = struct('name', 'Q_m', 'type', 'parameter', 'form', 'full', 'val', 4);
Q_p = struct('name', 'Q_p', 'type', 'parameter', 'form', 'full', 'val', 0);
R_m = struct('name', 'R_m', 'type', 'parameter', 'form', 'full', 'val', 4);
R_p = struct('name', 'R_p', 'type', 'parameter', 'form', 'full', 'val', 0);
t = struct('name', 't', 'type', 'set', 'form', 'full', 'val', 1, 'uels', {{'s1'}});
bigakrim
User
User
Posts: 12
Joined: 4 years ago

Re: Matlab-gams wgdx

Post by bigakrim »

Thanks Mr Renke.Kuhlmann
I correct my Matlab code and I have no more errors while compiling the coding, but updating the structures' value of the parameters, and set in Matlab gives the same cost. I expect the cost to be changing as the new value of these parameters and set is pass to gams for the new optimization. Modification in the gams or/and Matlab codes are surely required.
Please help me with the modification to make allowing to run gams from Matlab with the update parameters and set value read by gams from Matlab.

here is the new matlab coding

Q_m = struct('name', 'Q_m', 'type', 'parameter', 'form', 'full', 'val', 4);
Q_p = struct('name', 'Q_p', 'type', 'parameter', 'form', 'full', 'val', 0);
R_m = struct('name', 'R_m', 'type', 'parameter', 'form', 'full', 'val', 4);
R_p = struct('name', 'R_p', 'type', 'parameter', 'form', 'full', 'val', 0);
t = struct('name', 't', 'type', 'set', 'form', 'full', 'val', 1, 'uels', {{'s1'}});

wgdx('gams_matlab.gdx', Q_m, Q_p, R_m, R_p, t);

cost = gams('myopic_gams_matlab.gms LP=cplex', t, Q_m, Q_p, R_m, R_p);
reward = cost.val;

thanks
renke.kuhlmann
User
User
Posts: 5
Joined: 2 years ago

Re: Matlab-gams wgdx

Post by renke.kuhlmann »

But this is because of the model and not the Matlab interface. In the model you reset the values before you solve:

Code: Select all

R_m$(ord(t)=1) = 0;
Q_m$(ord(t)=1) = 0;
R_m =4;
R_p=1*R(t);
Q_m =4;
Q_p=1*Q(t);
Remove the above and change

Code: Select all

execute_load 'gams_matlab.gdx';
to

Code: Select all

execute_load 'gams_matlab.gdx' Q_m, Q_p, R_m, R_p;
in order to actually load the symbols. You'll probably have to provide a default value for R_m and Q_m then.

By the way, if you load the symbols yourself, there is no need to provide them to the Matlab gams function. You could simply write:

Code: Select all

cost = gams('myopic_gams_matlab.gms LP=cplex');
Post Reply