Problem with round-tripping variables using rgdx / wgdx Topic is solved

questions about GAMS' tools
alodi
User
User
Posts: 5
Joined: 3 months ago

Re: Problem with round-tripping variables using rgdx / wgdx

Post by alodi » 2 months ago

Hi Renger, hi Steve,

thanks for your help. Providing the sets for the dimensions when writing to gdx does indeed solve the problem!
Here is the solution based on the example, where I included the call to wgdx:

Code: Select all

> x=rgdx("test1.gdx", list(name="X", field="all"))
> s=rgdx("test1.gdx", list(name="s"))
> t=rgdx("test1.gdx", list(name="t"))
> str(x)
List of 11
 $ name       : chr "X"
 $ type       : chr "variable"
 $ dim        : int 2
 $ val        : num [1:45, 1:4] 1 1 1 1 1 1 1 1 1 1 ...
 $ form       : chr "sparse"
 $ uels       :List of 3
  ..$ : chr [1:3] "a" "b" "c"
  ..$ : chr [1:3] "1" "2" "3"
  ..$ : chr [1:5] "l" "m" "lo" "up" ...
 $ domains    : chr [1:3] "s" "t" "_field"
 $ domInfo    : chr "full"
 $ field      : chr "all"
 $ varTypeText: chr "free"
 $ typeCode   : int 5
> wgdx("test2.gdx", x, s, t)
> str(rgdx("test2.gdx", list(name="X", field="all")))
List of 11
 $ name       : chr "X"
 $ type       : chr "variable"
 $ dim        : int 2
 $ val        : num [1:45, 1:4] 1 1 1 1 1 1 1 1 1 1 ...
 $ form       : chr "sparse"
 $ uels       :List of 3
  ..$ : chr [1:3] "a" "b" "c"
  ..$ : chr [1:3] "1" "2" "3"
  ..$ : chr [1:5] "l" "m" "lo" "up" ...
 $ domains    : chr [1:3] "s" "t" "_field"
 $ domInfo    : chr "full"
 $ field      : chr "all"
 $ varTypeText: chr "free"
 $ typeCode   : int 5
Thanks again for your help and the swift response,

Alois

Edit: It might be worthwhile to point out in the documentation of wgdx that one has to save the uel sets explicitly. To me it was not obvious since this information is clearly contained in the object delivered to wgdx.
Edit2: Here is a wrapper (not tested)

Code: Select all

var2gdx <- function(gdx, var){
    uels <- list()
    for(n in 1:(length(var$uels) - 1)){
        uels[[n]] <- list(name=var$domains[[n]], type="set", uels=list(var$uels[[n]]))
    }
    wgdx(gdx, var, uels)
}

Post Reply