R-Link

  • 24 July 2019
  • 5 replies
  • 195 views

Hello Aimms Community,

I have to do a calculation with Parameter K_T(s,i,ii,t) in R and get the calculation results back to Aimms for the Optimization. I want to have the root of the matrix.
i and ii are Indices for the same SetIdentifier Sn.

code:
dl::DataTables+={'MyDataFrame'};
empty DataMap;
DataMap(dl::dt,dl::idn,dl::cn,dl::dn) += data {
!( table_name , identifier , col , dom ) : name_in_dataframe
( MyDataFrame , Sn , 2 , 1 ) : "i",
( MyDataFrame , Sn , 3 , 2 ) : "ii",
( MyDataFrame , P , 4 , 3 ) : "t",
( MyDataFrame , OL , 5 , 4 ) : "s" ,
( MyDataFrame , K_T , 1 , 0 ) : "K"
} ;

MapName:="MyMapName";
dl::RemoveDataSourceMapping(MapName);
dl::AddDataSourceMapping( MapName, DataMap,
dl::DependEmpty,dl::TableAttributesEmpty,dl::ColAttributeEmpty);

dl::ExecScript("source('test_rlink.r')",MapName,XA);


the R-Code in test_rlink.r

code:
# get data from AIMMS
dfin <- aimms::GetData("MyDataFrame")

# do the calculation
dec0 <- eigen(dfin$K)
ev0 <- dec0$vectors
ew0 <- diag(dec0$values)
root <- ev0 %*% ew0^{0.5} %*% t(ev0)

# give data to Aimms
MyDataFrame2 <- aimms::SetData("root")


These warnings appear at the moment:
code:
Domain columns are not declared adjacent in table mapping of table MyDataFrame (columns 1-3 are missing).
Domain columns are not declared adjacent in table mapping of table MyDataFrame (columns 1-4 are missing).
Table mapping of table MyDataFrame contains a 4-dimensional identifier K_T specified over a 1-dimensional domain.
The thread tried to read from or write to a virtual address for which it does not have the appropriate access


I´m not sure how to fix this - could you give me some advice?
Do I have to declare also a DataMap for getting the results in MyDataFrame2?

Many thanks in advance!
Tobi

5 replies

Userlevel 5
Badge +7
Hi Tobi,

The third column in the dataMap looks you are putting the parameter column before the set columns. Can you try using

code:
 ( MyDataFrame , Sn , 1 , 1 ) : "i",
( MyDataFrame , Sn , 2 , 2 ) : "ii",
( MyDataFrame , P , 3 , 3 ) : "t",
( MyDataFrame , OL , 4 , 4 ) : "s" ,
( MyDataFrame , K_T , 5 , 0 ) : "K"


See also https://documentation.aimms.com/datalink/datamap.html#the-classic-data-map

Let me know if this helped. If not we can look further.

With kind regards,

Chris
Dear Chris,

thank you, now the export of data to R has worked! I also changed the set order.

Unfortunately the import of the calculated data from R to Aimms to Parameter W_K_T(s,i,ii,t) is not working at the moment.
I´m not sure if I have everything right.
Do I have to add the SampleID also as set in Aimms?

code:
!Get data to R
dl::DataTables+={'MyDataFrame'};

empty DataMap;
DataMap(dl::dt,dl::idn,dl::cn,dl::dn) += data {
!( table_name , identifier , col , dom ) : name_in_dataframe
( MyDataFrame , OL , 1 , 1 ) : "s",
( MyDataFrame , Sn , 2 , 2 ) : "i",
( MyDataFrame , Sn , 3 , 3 ) : "ii",
( MyDataFrame , P ,4 , 4 ) : "t" ,
( MyDataFrame , K_T , 5 , 0 ) : "K"
} ;

MapName:="MyMapName";
dl::RemoveDataSourceMapping(MapName);
dl::AddDataSourceMapping( MapName, DataMap,
dl::DependEmpty,dl::TableAttributesEmpty,dl::ColAttributeEmpty);

! Get data back to Aimms
dl::DataTables+={'MyDataFrame2'};

empty DataMap;
DataMap(dl::dt,dl::idn,dl::cn,dl::dn) += data {
!( table_name , identifier , col , dom ) : name_in_dataframe
( MyDataFrame2 , SampleID , 1 , 1 ) : "SampleID",
( MyDataFrame2 , OL , 1 , 1 ) : "s",
( MyDataFrame2 , Sn , 2 , 2 ) : "i",
( MyDataFrame2 , Sn , 3 , 3 ) : "ii",
( MyDataFrame2 , P ,4 , 4 ) : "t" ,
( MyDataFrame2 , W_K_T , 5 , 0 ) : "KR"
} ;

MapName:="MyMapName";
dl::RemoveDataSourceMapping(MapName);
dl::AddDataSourceMapping( MapName, DataMap,
dl::DependEmpty,dl::TableAttributesEmpty,dl::ColAttributeEmpty);

! RCode
dl::ExecScript("source('test_rlink.r')",MapName,XA);


the R code for getting the data to aimms
code:
(...)
dfout$SampleID <- seq.int(nrow(result))
aimms::SetData("MyDataFrame2", dfout )
Userlevel 5
Badge +7
You mapped both the columns for SampleId and for OL to 1.

Does MyDataFrame2 has one column more than MyDataFrame?

If yes, is SampleID a set or a parameter depending on the other indices?

If no, can you remove SampleID or OL from the datamap?
Oh, sorry, that was a silly mistake with the column numbering.

- I added Sample ID because of the description in https://documentation.aimms.com/rlink/example.html "Read iris data set". I thought otherwise I could not import data to Aimms. Is that right?
And with that, MyDataFrame2 has one column more than MyDataFrame.
But I have no parameter or set in Aimms defined. (?)

I tried to run the R-Link now with the adjusted column numbering, with and without the Sample ID, but it´s now very, very slow and crashing in the end.
Userlevel 5
Badge +7
Hi Tobi,

This warrants further investigation.
Please send an e-mail to support@aimms.com that leads to a reproduction.

With kind regards,

Chris.

Reply


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator