Help with gdxrrw

Hi there,

can someone help with a problem related to the communication of data between GAMS and R.
I have the following code in GAMS:


$Title Emission game in partition form with identical contries (Using anlytic solution)

$set rscript C:\Users\marco\Documents\gamsdir\rprojdir\partitionsgdx.R

Scalar

n /11/

execute_unload “C:\Users\marco\Documents\gamsdir\rprojdir\countryn.gdx”, n;

execute ‘“C:\Program Files\R\R-3.2.2\bin\i386\R.exe” --vanilla < %rscript%’


Set
pn number of partitions
cn size of coalitions per each partition

Parameters

pc(cn,pn)


Till here no problem. The R script calculate the partitions of the scalar n and writes on a gdx files (parti.gdx) the elements of sets pn and cn and the values of pc. Here is the last part of the R code:


spdef<- list(name=‘pn’,uels=list(partn),dim=1,form=‘full’,type=‘set’,ts=‘partitions’)
cddef= list(name=‘cn’,uels=list(coals),dim=1,form=‘full’,type=‘set’,ts=‘coalitions in partitions’)
cpmat= list(name=‘pc’, val=c1,uels=list(coals,partn),dim=2,form=‘full’,type=‘parameter’,ts=‘partitions coalitions matrix’)
wgdx.lst(“C:/Users/marco/Documents/gamsdir/rprojdir/parti.gdx”, list(spdef,cddef,cpmat))


Now, my problem is to load them in Gams. If I use the following code, Gams tells me that it cannot find the parti.gdx file:


$set INPUT C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx
$gdxin %INPUT%
$load pn, cn, pc
$gdxin


I mean, it works if I run the first part of the code (generating the .gdx file) and later I run this part of the code. But if the parti.gdx file has not been previously created, Gams does not wait for R to complete its job, and gives me the error.
I tried then a second option, as to say to this last part of the code with:


execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,pn;
execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,cn;
execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,pc;


Unfortunaltely, it displays an error for the two sets (pn and cn) saying that assigning for these sets is not allowed (error 188).

Any suggestion for solving this problem?

I know that I starting with Gams for just defining a scalar, passing it to R and than taking back the data from R is not a really efficient procedure. I could start with R and simply call Gams from there. But still, I would like to understand how I can implement the procedure in the way shown here (It has become a point of honour, eheheh).

Thanks a lot,

Marco



\

To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Hi Marco

You could run GAMS from within your R-script, or the other way around, so I will only start after if finishes the jobs.

In GAMS : $call “rscript.exe mysript.R”



In R: system(“gams.exe mymodel.gms”)



Otherwise take a look at : http://support.gams.com/interfaces:how_can_a_gams_process_communicate_with_another_process?s[]=waiting



Hope this helps

Renger



From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com] On Behalf Of Marco R.
Sent: Mittwoch, 21. Oktober 2015 10:24
To: gamsworld
Subject: Help with gdxrrw



Hi there,



can someone help with a problem related to the communication of data between GAMS and R.

I have the following code in GAMS:


\


$Title Emission game in partition form with identical contries (Using anlytic solution)



$set rscript C:\Users\marco\Documents\gamsdir\rprojdir\partitionsgdx.R



Scalar



n /11/



execute_unload “C:\Users\marco\Documents\gamsdir\rprojdir\countryn.gdx”, n;



execute ‘“C:\Program Files\R\R-3.2.2\bin\i386\R.exe” --vanilla < %rscript%’





Set

pn number of partitions

cn size of coalitions per each partition



Parameters



pc(cn,pn)


Till here no problem. The R script calculate the partitions of the scalar n and writes on a gdx files (parti.gdx) the elements of sets pn and cn and the values of pc. Here is the last part of the R code:


spdef<- list(name=‘pn’,uels=list(partn),dim=1,form=‘full’,type=‘set’,ts=‘partitions’)

cddef= list(name=‘cn’,uels=list(coals),dim=1,form=‘full’,type=‘set’,ts=‘coalitions in partitions’)

cpmat= list(name=‘pc’, val=c1,uels=list(coals,partn),dim=2,form=‘full’,type=‘parameter’,ts=‘partitions coalitions matrix’)

wgdx.lst(“C:/Users/marco/Documents/gamsdir/rprojdir/parti.gdx”, list(spdef,cddef,cpmat))


Now, my problem is to load them in Gams. If I use the following code, Gams tells me that it cannot find the parti.gdx file:


\


$set INPUT C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx

$gdxin %INPUT%

$load pn, cn, pc

$gdxin


I mean, it works if I run the first part of the code (generating the .gdx file) and later I run this part of the code. But if the parti.gdx file has not been previously created, Gams does not wait for R to complete its job, and gives me the error.

I tried then a second option, as to say to this last part of the code with:


\


execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,pn;

execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,cn;

execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,pc;


Unfortunaltely, it displays an error for the two sets (pn and cn) saying that assigning for these sets is not allowed (error 188).



Any suggestion for solving this problem?



I know that I starting with Gams for just defining a scalar, passing it to R and than taking back the data from R is not a really efficient procedure. I could start with R and simply call Gams from there. But still, I would like to understand how I can implement the procedure in the way shown here (It has become a point of honour, eheheh).



Thanks a lot,



Marco








\

To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.


To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Hi Ranger,

yeap. Now it works!!!

Many, many thanks,

Marco

Il giorno mercoledì 21 ottobre 2015 10:29:15 UTC+2, Marco R. ha scritto:

Hi there,

can someone help with a problem related to the communication of data between GAMS and R.
I have the following code in GAMS:


$Title Emission game in partition form with identical contries (Using anlytic solution)

$set rscript C:\Users\marco\Documents\gamsdir\rprojdir\partitionsgdx.R

Scalar

n /11/

execute_unload “C:\Users\marco\Documents\gamsdir\rprojdir\countryn.gdx”, n;

execute ‘“C:\Program Files\R\R-3.2.2\bin\i386\R.exe” --vanilla < %rscript%’


Set
pn number of partitions
cn size of coalitions per each partition

Parameters

pc(cn,pn)


Till here no problem. The R script calculate the partitions of the scalar n and writes on a gdx files (parti.gdx) the elements of sets pn and cn and the values of pc. Here is the last part of the R code:


spdef<- list(name=‘pn’,uels=list(partn),dim=1,form=‘full’,type=‘set’,ts=‘partitions’)
cddef= list(name=‘cn’,uels=list(coals),dim=1,form=‘full’,type=‘set’,ts=‘coalitions in partitions’)
cpmat= list(name=‘pc’, val=c1,uels=list(coals,partn),dim=2,form=‘full’,type=‘parameter’,ts=‘partitions coalitions matrix’)
wgdx.lst(“C:/Users/marco/Documents/gamsdir/rprojdir/parti.gdx”, list(spdef,cddef,cpmat))


Now, my problem is to load them in Gams. If I use the following code, Gams tells me that it cannot find the parti.gdx file:


$set INPUT C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx
$gdxin %INPUT%
$load pn, cn, pc
$gdxin


I mean, it works if I run the first part of the code (generating the .gdx file) and later I run this part of the code. But if the parti.gdx file has not been previously created, Gams does not wait for R to complete its job, and gives me the error.
I tried then a second option, as to say to this last part of the code with:


execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,pn;
execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,cn;
execute_load “C:\Users\marco\Documents\gamsdir\rprojdir\parti.gdx”,pc;


Unfortunaltely, it displays an error for the two sets (pn and cn) saying that assigning for these sets is not allowed (error 188).

Any suggestion for solving this problem?

I know that I starting with Gams for just defining a scalar, passing it to R and than taking back the data from R is not a really efficient procedure. I could start with R and simply call Gams from there. But still, I would like to understand how I can implement the procedure in the way shown here (It has become a point of honour, eheheh).

Thanks a lot,

Marco



\

To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.