我尝试通过R和基于解算器“cbc”的网状和pyomo运行以下代码。如果我独立于R执行代码,那么Python代码就可以工作。使用包“netricate”在R中执行代码是不起作用的
关于Python代码的信息:一个简单的具体Pyomo模型-https://pyomo.readthedocs.io/en/stable/pyomo_overview/simple_examples.html
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)
R中的代码:
library("reticulate")
use_python("C:/Anaconda")
setwd("C:/Users/SimpleConcretePyomoModel")
source_python("C:/Users/SimpleConcretePyomoModel/SimpleConcretePyomoModel.py")
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
其他信息:
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
我认为您只需要在顶部添加以下行
或者你可以试试这个
SolverFactory['cbc']
而不是SolverFactory('cbc')
。这里的大括号不同检查此site并搜索“cbc”。你可能会得到更多的见解
这将解决问题,因为我看到错误表明它无法创建对象
相关问题 更多 >
编程相关推荐