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
Matlab-gams wgdx
-
- User
- Posts: 5
- Joined: 2 years ago
Re: Matlab-gams wgdx
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:
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'}});
Re: Matlab-gams wgdx
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
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
-
- User
- Posts: 5
- Joined: 2 years ago
Re: Matlab-gams wgdx
But this is because of the model and not the Matlab interface. In the model you reset the values before you solve:
Remove the above and change
to
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
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);
Code: Select all
execute_load 'gams_matlab.gdx';
Code: Select all
execute_load 'gams_matlab.gdx' Q_m, Q_p, R_m, R_p;
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');