MATLAB to GAMS and GAMS to MATLAB

2,620 views
Skip to first unread message

David

unread,
Dec 18, 2010, 6:13:48 AM12/18/10
to gamsworld

Hi!

Can you help me in constructing interfaces between GAMS and MATLAB? I
have read the introduction http://www.cs.wisc.edu/math-prog/matlab.html
However it doesn't fix my problem.

My challenge is:
- MATLAB to GAMS
I have data for an optimisation problem generated by MATLAB and I want
to solve it by using GAMS.


- GAMS to MATLAB
The solution will be used in MATLAB for further calculation.

The attached gams-file is only a small clear extract of the total
optimisation model. The GAMS-Input (MATLAB => GAMS) is:
p_load(t)
and
the output of GAMS (GAMS => MATLAB) is P(i,t) (actual power output
of plant i in time t)


Thanks a lot for your help.

Regards,

David

The given GAMS-model:



SETS
i unit /1 * 4/



t consideration period /1*2/


;

Parameter

price_fuel(i) fuel price for gas (EUR per kWh)/1 0.0001
2 0.002
3
0.0025
4
0.0015/

* p_load(t) is MATLAB output and not determined in GAMS like now.
p_load(t) /1 50
2 100/


p_min(i) /1 15
2 15
3 12
4 12/

p_max(i) /1 40
2 45
3 50
4 50/


VARIABLES
C_total total costs of the schedule
;

POSITIVE VARIABLES
P actual power output of plant i in time t
P_fuel fuel rating of plant i in time t
P1 required power of unit i in t
;

BINARY VARIABLES
gamma_on
;
$ontext
P.up('1',t) = 40;
P.up('2',t) = 45;
P.up('3',t) = 50;
P.up('4',t) = 50;
P.lo('1',t) = 15;
P.lo('2',t) = 15;
P.lo('3',t) = 12;
P.lo('4',t) = 12;
$offtext
EQUATIONS
fuel simple linear fuel function
cost_total cost function
load_cover load cover constraint
syst_unit_pos1 Sytem unit constraint: lower boundfor
operational range
syst_unit_pos2 Sytem unit constraint: upper boundfor
operational range

;


*Load in the system constraint
load_cover(t)..
p_load(t) =E= sum(i, P1(i,t))
;

*Fuel function
fuel(i,t)..
P_fuel(i,t) =E= (2.5891 * P1(i,t) + 43.031)
;


*Object function
cost_total..
C_total =E= sum(t, ( sum(i,
price_fuel(i)* P_fuel(i,t)) ))*0.25
;
*Sytem unit constraint: lower bound for operational range
syst_unit_pos1(i,t)..
P1(i,t) =G= gamma_on(i,t) * p_min(i)
;
*Sytem unit constraint: upper bound for operational range
syst_unit_pos2(i,t)..
P1(i,t) =L= gamma_on(i,t) * p_max(i)
;



model schedule /all/;

solve schedule using mip minimizing C_total;

execute_unload "C:\tmp\resultout.gdx";

Edson Cordeiro do Valle

unread,
Dec 20, 2010, 5:07:35 AM12/20/10
to gams...@googlegroups.com
Hello David
Why don't you code everything in Gams?
If you don't want to code all in Gams, I think the best way to do such
communication is to use a matlab-odbc export ( to send data from Matlab
to Gams).
It could be performed this way:
1)Perform Matlab operations
2)Send Matlab using ODBC ( make a google search matlab odbc, you'll find
some information )
3)Read this ODBC data into Gams
4)Run Gams
5)Export to Matlab using Gams
(http://pages.cs.wisc.edu/~ferris/matlabgams.pdf)
There are some examples in ODBC data reading in Gams in Gams model
libraries ( data utilities models)
Regards
Edson

David escreveu:

.::Morteza Shabanzadeh::.

unread,
Dec 20, 2010, 5:42:38 AM12/20/10
to gams...@googlegroups.com
Hi Davis,
 
With regard to your question I suggest you to follow these steps:
 
*a) Installation:
First of all, you need to install both MATLAB and GAMS on your machine.
We will assume that the relevant system (installation) directories are:
 
C:\MATLAB and C:\GAMS
 
1. Copy matout.gms in C:\GAMS\inclib\matout.gms
2. Copy gams.dll in C:\MATLAB\toolbox\local\gams.dll
3. Set the environment variable path of GAMS
 
To test the installation, carry out the following steps:
1. Start up MATLAB
2. In the MATLAB command window, change directories to the examples directory provided as part of the distribution (This directory contains at least two files, testinst.m and testinst.gms that are required for this test.)
3. Run the example “testinst” that is found in the examples directory of the distribution.
 
 At the MATLAB prompt you just type:
>>testinst

The resulting output will depend on the platform on which you run this from.
 
*b) Returning Values to MATLAB:
In order to run the same model within MATLAB and return the solution vector
variable of x(i) and parameter of d(i, j) back into the MATLAB workspace, one
change is required to the GAMS file, namely to add the below line after the
SOLVE statement:
 
$libinclude matout x.l i
$libinclude matout d i j
 
This just writes out the level values of the solution to a file that can be read back
into MATLAB. In MATLAB, you just execute the following statement:
 
[x d] = gams('NameGamsFile');
 
For more information click here: 
 
or
 
my weblog: http://mortezash.blogfa.com/ (in Persian)
 
Morteza Sha'ban-Zadeh
 


 

             

 

--- On Sat, 12/18/10, David <david....@googlemail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "gamsworld" group.
To post to this group, send email to gams...@googlegroups.com.
To unsubscribe from this group, send email to gamsworld+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.


Steven Dirkse

unread,
Dec 21, 2010, 9:40:52 AM12/21/10
to gams...@googlegroups.com
Morteza,

The notes you provided below are for an older version of the GAMS/Matlab interface tools.  In the newest version all of the tools are contained in the GAMS distribution, so there is no need to copy files like matout.gms and gams.dll.  Also, the gams.dll file is a very old Mex-file.  Matlab has been using platform-specific names for their Mex-files for several years and the new interface follows this standard.  I encourage all user to update to the latest version of these tools found in the recent GAMS Distribution 23.6.2.

-Steve

To unsubscribe from this group, send email to gamsworld+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.



--
Steven Dirkse, Ph.D.
GAMS Development Corp.,  Washington DC
Voice: (202)342-0180     Fax: (202)342-0181
sdi...@gams.com
http://www.gams.com

David

unread,
Dec 21, 2010, 9:34:55 AM12/21/10
to gamsworld
Hi Morteza Sha'ban-Zadeh, Hi Edson,

Thank you for your help. I will try both ways.

Regrads,

David

Steven Dirkse

unread,
Dec 21, 2010, 9:36:16 AM12/21/10
to gams...@googlegroups.com
David,

This question and ones like it seem to be common ones. I collected
some thoughts on this, along with a fully-worked-out example
illustrating my suggested approach, and made this available as a FAQ
at http://www.gams.com/~steve/gdxmrw.html. HTH,

-Steve

.::Morteza Shabanzadeh::.

unread,
Dec 22, 2010, 2:17:59 AM12/22/10
to gams...@googlegroups.com
Dear Steve,
 
You right. I used an older version of the GAMS/MATLAB interface tools but absolutely I will update it.
 
Thanks,


 

             

 

--- On Tue, 12/21/10, Steven Dirkse <sdi...@gams.com> wrote:

David

unread,
Jan 4, 2011, 4:42:31 AM1/4/11
to gamsworld
Hi Steve,

Your approach works perfectly translated to my problem.
Thank you for help.

Regards,

David

On 21 Dez. 2010, 15:36, Steven Dirkse <sdir...@gams.com> wrote:
> David,
>
> This question and ones like it seem to be common ones.  I collected
> some thoughts on this, along with a fully-worked-out example
> illustrating my suggested approach, and made this available as a FAQ
> athttp://www.gams.com/~steve/gdxmrw.html.  HTH,
>
> -Steve
>
>
>
>
>
> On Sat, Dec 18, 2010 at 6:13 AM, David <david.gun...@googlemail.com> wrote:
>
> > Hi!
>
> > Can you help me in constructing interfaces between GAMS and MATLAB? I
> > have read the introductionhttp://www.cs.wisc.edu/math-prog/matlab.html
> > For more options, visit this group athttp://groups.google.com/group/gamsworld?hl=en.
>
> --
> Steven Dirkse, Ph.D.
> GAMS Development Corp.,  Washington DC
> Voice: (202)342-0180     Fax: (202)342-0181
> sdir...@gams.comhttp://www.gams.com- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

saeedsay...@gmail.com

unread,
Mar 29, 2014, 6:27:27 AM3/29/14
to gams...@googlegroups.com
Hello everyone!
I am trying to link Matlab and Gams.
The output of Gams model, which is named 'producer1',is the variable "y" which is a 17x104 matrix.
I type this line in command window:
y = gams('producer1')
It solves thd model but it cant assign the outputs to 'y' and returns this error:
"Number of output argument in Matlab exceed output argument specified in gams"
When I just type this line in command window:
gams('producer1')
the error doesn't  appear any more.
Do you happen to help me?
Kind Regards 

Mohsen Avaji

unread,
May 1, 2014, 7:16:24 PM5/1/14
to gams...@googlegroups.com, david....@googlemail.com
Hello everyone!
I am trying to link Matlab and Gams
please send me a Simple example for better understanding.

Thank you.

Daniel Orlando Martinez Quezada

unread,
Jul 13, 2014, 11:53:15 AM7/13/14
to gams...@googlegroups.com
you can try only run "gams('producer1')", in the gms file you have to put some variable or parameter in a gdx file by matout sentence. just read that gdx in the matlab file using "readgdx".


DANIEL ORLANDO MARTINEZ QUEZADA  

arash moradinejad

unread,
Jan 21, 2015, 1:47:28 PM1/21/15
to gams...@googlegroups.com
Hi,

I am trying to use matgam; however, I have got two error:

   - "Unsupported data format entered." in response of running testinst example;
   - "Number of output argument in Matlab exceed output argument specified in gams" whenever I try to take the output such as x = gams('qp').

The first problem is really serious since I want to give some parameter values before solve the problem. I think it may because of version mismatch.

By the way, I am using Matlab 2012a (64-bit) and the demo version of GAMS release 24.3.3 r48116 WEX-WEI over Windows 7 (64bit).

I would be grateful if you could let me know your comments.

Kind regards,
Arash

Ali Reza Bahari

unread,
Jan 22, 2015, 7:15:49 AM1/22/15
to gams...@googlegroups.com
Hi
If it possible to you, Send me both Matlab and Gams file.
I try to solve it.

Best Regards
Ali Reza

--
You received this message because you are subscribed to the Google Groups "gamsworld" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.

To post to this group, send email to gams...@googlegroups.com.

arash moradinejad

unread,
Jan 22, 2015, 1:10:34 PM1/22/15
to gams...@googlegroups.com
Hi Alireza,

Thank you for your reply.

As a matter of fact, there is no new GAMS nor Matlab codes. I just tried to run two GAMS and Matlab test codes available by the matgams package, which are testinst and qp,

Best regards,
Arash

Ali Reza Bahari

unread,
Jan 23, 2015, 6:08:21 AM1/23/15
to gams...@googlegroups.com
Hi
Follow this steps:

1- Have a look to the PDF file i attached you(pages 2 and 7). I had attached all of you need. 

2- Add the Gams path to the Matlab
Inline image 1



2- Set the environment variable.

Inline image 2
gams.dll
matout.gms

Ali Reza Bahari

unread,
Jan 23, 2015, 6:08:24 AM1/23/15
to gams...@googlegroups.com
matlabgams2.pdf

arash moradinejad

unread,
Jan 23, 2015, 6:47:28 AM1/23/15
to gams...@googlegroups.com
Hi Alireza,

Thank you very much for your help.

Actually, I have followed all instructions given by you and the attached pdf and I think I have installed everything properly; however, it still does not work and it generates all previously mentioned errors.

I have solved the problem by changing the gms file directly and parse the generated matsol.gms file including all results, but it is very slow.

Many thanks and kind regards,
Arash

Fabricio Porras Ortiz

unread,
Feb 23, 2016, 3:05:57 AM2/23/16
to gamsworld
Hi Arash,

I would be grateful if you tell me where I can download the matgam?
If you have this file, please send me to fpor...@gmail.com

Thanks for your attention!

Kind regards,

Fabricio

Claudio Delpino

unread,
Feb 23, 2016, 7:06:24 AM2/23/16
to gams...@googlegroups.com
Semi Off Topic advice: Don't lose sight that the functions wgdx and rgdx for MatLab allow for an easier, more flexible and easier to debug interaction. Of course, running gams from MatLab is also trivial (in my experience).

Regards
Claudio

--
You received this message because you are subscribed to the Google Groups "gamsworld" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.
To post to this group, send email to gams...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages