首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将GAMS gms文件读入ROI (R优化接口)?

如何将GAMS gms文件读入ROI (R优化接口)?
EN

Stack Overflow用户
提问于 2022-03-22 22:07:50
回答 2查看 61关注 0票数 0

我有一个用GAMS编写的NLP模型,我想使用ROI (R优化接口)将其发送到NEOS服务器。

看起来,ROI_read()是在外部模型中读取的命令,但我不知道如何找到或安装正确的读取器类型/插件。

代码语言:javascript
运行
复制
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'
EN

回答 2

Stack Overflow用户

发布于 2022-07-17 14:26:42

ROI没有读取GAMS模型的方法。当将模型发送到NEOS服务器时,它所做的是以GAMS格式编写模型并发送此文件。

假设您将以下模型保存为model.gms

代码语言:javascript
运行
复制
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/;

然后,您可以通过以下方式从solve_op中修改ROI.plugin.neos函数,以实现您想要的结果。

代码语言:javascript
运行
复制
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"

这种方法的缺点是解决方案的ROI默认打印方法将不再工作,您必须选择求解器manually

一种更好的方法是将GAMS中的模型保存为固定的MPS格式或ROI实际可以读取的任何其他格式。

票数 1
EN

Stack Overflow用户

发布于 2022-07-20 06:36:41

新的GAMS工作室有一个集成NEOS接口,允许您在GAMS中解决NEOS上的模型。还可以看到这个博客帖子

向近地天体提交数据

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71579509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档