How to read a GAMS gms file into ROI (R Optimisation Interface)?

121 views Asked by At

I have a NLP model written in GAMS and I would like to use ROI (R Optimisation Interface) to send it to the NEOS Server.

It looks like ROI_read() is the command for reading in external models, but I don't know how to find out or install the correct reader type/plugin.

Sys.setenv(ROI_LOAD_PLUGINS = FALSE)
library(ROI)             # ROI_solve
library(ROI.plugin.neos) # NEOS

x <- ROI_read("rawdata/mymodel.gms", type = "GAMS", solver = "neos")

Error in ROI_read("rawdata/mymodel.gms", type = "GAMS", solver = "neos") : 
  no reader found for type 'GAMS'
2

There are 2 answers

0
roschu On

The new GAMS Studio has an integrated NEOS interface that allows you to solve your model on NEOS from within GAMS Studio. Also see this blog post.

Submitting data to NEOS

0
Ben373 On

ROI doesn't have a method for reading GAMS models. What it does when sending a model to the NEOS server it is writing the model as GAMS format and sending this file.

Suppose you have the following model saved as model.gms

Option IntVarUp = 0;

Set i / R1*R3 / ;
Set ileq(i) / R1, R2, R3 / ;
Set j / C1*C3 / ;

Parameter objL(j)
/C1 2
C2 4
C3 3/ ;

Parameter rhs(i)
/R1 60
R2 40
R3 80/ ;

Parameter A
/R1.C1 3
R2.C1 2
R3.C1 1
R1.C2 4
R2.C2 1
R3.C2 3
R1.C3 2
R2.C3 2
R3.C3 2/;

Variables obj;
Positive Variables x(j);


Equations
    ObjSum
    LinLeq(ileq);

ObjSum .. obj =e= sum(j, x(j) * objL(j)) ;
LinLeq(ileq) .. sum(j, A(ileq, j) * x(j)) =l= rhs(ileq) ;

Model LinearProblem /all/ ;

Solve LinearProblem using LP maximizing obj ;

option decimals = 8;

display '---BEGIN.SOLUTION---', x.l, '---END.SOLUTION---';


file results /results.txt/;
results.nw = 0;
results.nd = 15;
results.nr = 2;
results.nz = 0;
put results;
put 'solution:'/;
loop(j, put, x.l(j)/);
put 'objval:'/;
put LinearProblem.objval/;
put 'solver_status:'/;
put LinearProblem.solvestat/;
put 'model_status:'/;
put LinearProblem.modelstat/;

Then you could modify the solve_op function from ROI.plugin.neos in the following way to achieve what you want.

Sys.setenv(ROI_LOAD_PLUGINS = FALSE)
library(ROI)
#> ROI: R Optimization Infrastructure
#> Registered solver plugins: nlminb.
#> Default solver: auto.
library(ROI.plugin.neos)
model <- paste0(readLines('model.gms'), collapse = '\n')

control <- ROI.plugin.neos:::set_default_control_values(ROI.plugin.neos:::neos_control())
control$email <- YOUR_EMAIL

solver_name <- "scip"

xml <- ROI.plugin.neos:::neos_xml_call(model, solver_name, control)
solver_call <- list(ROI.plugin.neos:::neos_submit_job, x = NULL, xmlstring = xml, user = control$user, 
                        password = control$password)
mode(solver_call) <- "call"
job <- eval(solver_call)
sol <- job$solution(wait=TRUE)
str(sol)
#> List of 4
#>  $ solution: num [1:3] 0 6.67 16.67
#>  $ objval  : num NA
#>  $ status  :List of 2
#>   ..$ code: int 0
#>   ..$ msg :List of 5
#>   .. ..$ solver  : chr "neos"
#>   .. ..$ code    : int 1
#>   .. ..$ symbol  : chr "NORMAL_COMPLETION"
#>   .. ..$ message : chr "An optimal solution was obtained."
#>   .. ..$ roi_code: int 0
#>   .. ..- attr(*, "class")= chr "registry_entry"
#>  $ message :List of 5
#>   ..$ solution     : num [1:3] 0 6.67 16.67
#>   ..$ objval       : num 76.7
#>   ..$ solver_status: num 1
#>   ..$ model_status : num 1
#>   ..$ message      : chr "Executed on prod-exec-1.neos-server.org\nGAMS 39.1.0  5f04cd76 May 3, 2022           LEX-LEG x86 64bit/Linux - "| __truncated__
#>  - attr(*, "meta")=List of 1
#>   ..$ solver: chr "neos"
#>  - attr(*, "class")= chr [1:2] "neos_solution" "OP_solution"

The drawback of this approach is that the default printing method of ROI for the solution won't work any longer and that you have to select the solver manually.

A better approach would be to save the model in GAMS to fixed MPS format or any other format which ROI can actually read.