添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
绅士的啤酒  ·  使用SNAP JAVA ...·  10 月前    · 
坚强的柿子  ·  server2012 wsus ...·  1 年前    · 
爱喝酒的开水瓶  ·  创建Spring ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I tried to run the following code via R with reticulate and pyomo based on the solver "cbc". The Python-code is working if I execute the code independently of R. Executing the code in R with the package "reticulate" doesn't work.

Information about the Python-code: A Simple Concrete Pyomo Model - https://pyomo.readthedocs.io/en/stable/pyomo_overview/simple_examples.html

Code in Python:

from pyomo.environ import *
import pyutilib.subprocess.GlobalData
pyutilib.subprocess.GlobalData.DEFINE_SIGNAL_HANDLERS_DEFAULT = False
model = ConcreteModel()
model.x = Var([1,2], domain=NonNegativeReals)
model.OBJ = Objective(expr = 2*model.x[1] + 3*model.x[2])
model.Constraint1 = Constraint(expr = 3*model.x[1] + 4*model.x[2] >= 1)
opt = SolverFactory('cbc')
opt.solve(model)

Code in R:

library("reticulate")
use_python("C:/Anaconda")
setwd("C:/Users/SimpleConcretePyomoModel")
source_python("C:/Users/SimpleConcretePyomoModel/SimpleConcretePyomoModel.py")

Error in R

Error in py_run_file_impl(file, local, convert) : 
  RuntimeError: Attempting to use an unavailable solver.
The SolverFactory was unable to create the solver "cbc"
and returned an UnknownSolver object.  This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method "solve").
The original solver was created with the following parameters:
    type: cbc
    _args: ()
    options: {}
WARNING: Failed to create solver with name 'cbc': Could not execute the
    command: 'C:\Program Files\cbc-win64\cbc.exe -stop'
        Error message: [WinError 6] Das Handle ist ungültig

Additional information:

python:             C:/Anaconda/python.exe
libpython:          C:/Anaconda/python38.dll
pythonhome:         C:/Anaconda
version:            3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]
Architecture:       64bit
numpy:              C:/Anaconda/Lib/site-packages/numpy
numpy_version:      1.20.3
pyomo_version:      5.7.2
reticulate_version: 1.20
cbc_version:        2.10.3
RStudio_version:    1.4.1717
Spyder_version:     5.0.5

I think you just need to add the following line at the top

from pyomo.opt import SolverFactory

Or else you can try this SolverFactory['cbc'] instead of SolverFactory('cbc'). The braces are different here

Check this site and search for 'cbc'. You might get some more insights.

That will solve the problem as I see that the error says it is unable to create the object.

Hey Murtuza, thank you very much for your answer! Running my Python code in Spyder works. However, I still get the same error when I call the Python code in RStudio using reticulate. I appreciate any other ideas. Br, Alex – Alex Sep 25, 2021 at 10:05 Could you try executing your code using the daily build for RStudio. Here is the reference that mandates or recommends the use for the same when using the 'reticulate' library. – MURTUZA BORIWALA Sep 25, 2021 at 13:17 Sorry, but I do not fully understand what you mean. Do you suggest that I should download RStudio v1.2 (rstudio.github.io/reticulate/articles/rstudio_ide.html) and execute my code again? Thanks for your help! – Alex Sep 25, 2021 at 16:07 I also get the same error when using different solvers, for example glpk or gurobi. In addition to that, I found out that I can execute the code in the console (R.exe) without getting an error. However, executing the code in RStudio still doesn't work. – Alex Sep 26, 2021 at 9:48

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.