pyomo + reticulate error 6 句柄无效
pyomo + reticulate error 6 the handle is invalid
我正在尝试 运行 pyomo
优化,但收到错误消息 [Error 6] The handle is invalid
。不知道怎么解释,环顾四周好像和特权有关,但我不是很懂。
在下面找到完整的错误跟踪以及重现它的玩具示例。
完整错误跟踪:
Error in py_run_file_impl(file, local, convert) : ApplicationError:
Could not execute the command:
'C:\Users\xxx\AppData\Local\Continuum\anaconda3\envs\lucy\Library\bin\ipopt.exe
c:\users\xxx\appdata\local\temp\tmpp2hmid.pyomo.nl -AMPL' Error
message: [Error 6] The handle is invalid
Detailed traceback: File "", line 46, in File
"C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyomo\opt\base\solvers.py",
line 578, in solve
_status = self._apply_solver() File "C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyomo\opt\solver\shellcmd.py",
line 246, in _apply_solver
self._rc, self._log = self._execute_command(self._command) File "C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyomo\opt\solver\shellcmd.py",
line 309, in _execute_command
tee = self._tee File "C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyutilib\subprocess\processmngr.py",
line 660, in run_command
基于 this 的可重现示例。
纯python代码(当我运行它在python,在conda
环境中调用"lucy"时它有效):
from pyomo.environ import *
infinity = float('inf')
model = AbstractModel()
# Foods
model.F = Set()
# Nutrients
model.N = Set()
# Cost of each food
model.c = Param(model.F, within=PositiveReals)
# Amount of nutrient in each food
model.a = Param(model.F, model.N, within=NonNegativeReals)
# Lower and upper bound on each nutrient
model.Nmin = Param(model.N, within=NonNegativeReals, default=0.0)
model.Nmax = Param(model.N, within=NonNegativeReals, default=infinity)
# Volume per serving of food
model.V = Param(model.F, within=PositiveReals)
# Maximum volume of food consumed
model.Vmax = Param(within=PositiveReals)
# Number of servings consumed of each food
model.x = Var(model.F, within=NonNegativeIntegers)
# Minimize the cost of food that is consumed
def cost_rule(model):
return sum(model.c[i]*model.x[i] for i in model.F)
model.cost = Objective(rule=cost_rule)
# Limit nutrient consumption for each nutrient
def nutrient_rule(model, j):
value = sum(model.a[i,j]*model.x[i] for i in model.F)
return model.Nmin[j] <= value <= model.Nmax[j]
model.nutrient_limit = Constraint(model.N, rule=nutrient_rule)
# Limit the volume of food consumed
def volume_rule(model):
return sum(model.V[i]*model.x[i] for i in model.F) <= model.Vmax
model.volume = Constraint(rule=volume_rule)
opt = SolverFactory('ipopt')
instance = model.create_instance('diet.dat')
results = opt.solve(instance, tee=False)
results
运行 在 R 中 reticulate
的代码非常简单:
library(reticulate)
use_condaenv(condaenv = "lucy")
py_run_file("../pyomo_scripts/test.py")
最后,为了完整起见,这是 diet.dat
文件(必须与 python/R 文件位于同一路径):
param: F: c V :=
"Cheeseburger" 1.84 4.0
"Ham Sandwich" 2.19 7.5
"Hamburger" 1.84 3.5
"Fish Sandwich" 1.44 5.0
"Chicken Sandwich" 2.29 7.3
"Fries" .77 2.6
"Sausage Biscuit" 1.29 4.1
"Lowfat Milk" .60 8.0
"Orange Juice" .72 12.0 ;
param Vmax := 75.0;
param: N: Nmin Nmax :=
Cal 2000 .
Carbo 350 375
Protein 55 .
VitA 100 .
VitC 100 .
Calc 100 .
Iron 100 . ;
param a:
Cal Carbo Protein VitA VitC Calc Iron :=
"Cheeseburger" 510 34 28 15 6 30 20
"Ham Sandwich" 370 35 24 15 10 20 20
"Hamburger" 500 42 25 6 2 25 20
"Fish Sandwich" 370 38 14 2 0 15 10
"Chicken Sandwich" 400 42 31 8 15 15 8
"Fries" 220 26 3 0 15 0 2
"Sausage Biscuit" 345 27 15 4 0 20 15
"Lowfat Milk" 110 12 9 10 4 30 0
"Orange Juice" 80 20 1 2 120 2 2 ;
评论后编辑:
这些是 pyomo
和 ipopt
的版本
pyomo 5.6.4 py36_0 conda-forge
pyomo.extras 3.3 py36_182212 conda-forge
ipopt 3.11.1 2 conda-forge
我继承了 R 中的大量代码,并通过系统调用在 pyomo
中完成了优化。我正在尝试通过使用 reticulate
来改进它,这样我就可以避免写入和读取文件,并且我有更多的控制权......如果我仍然在 python 内进行系统调用,我将获得很少的收益使用 reticulate
.
谢谢。
我不能说我完全理解这个问题,但是研究它是一个非常有趣的问题,主要是因为我收到了不同的错误消息
TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object
虽然我每次在新的 r 会话中 运行 py_run_file("test.py")
都会收到错误,但到第二个 运行 时就没有错误了。
话虽这么说,但我认为这与此问题有关:
https://github.com/PyUtilib/pyutilib/issues/31
添加两行后我没有遇到任何问题:
import pyutilib.subprocess.GlobalData
pyutilib.subprocess.GlobalData.DEFINE_SIGNAL_HANDLERS_DEFAULT = False
在调用求解器之前的 python 脚本中。
希望对您有所帮助
如果可以执行python版本,尝试用下面的代码
以管理权限r session
library("reticulate")
##-- your directory containing 'diet.py' and 'diet.dat'
setwd("D:/project/Dropbox/lectures/2104xxx scg_opt/src/02"")
##-- execute code
a <- py_run_file("diet.py",local=T)
a$results
我正在尝试 运行 pyomo
优化,但收到错误消息 [Error 6] The handle is invalid
。不知道怎么解释,环顾四周好像和特权有关,但我不是很懂。
在下面找到完整的错误跟踪以及重现它的玩具示例。
完整错误跟踪:
Error in py_run_file_impl(file, local, convert) : ApplicationError: Could not execute the command: 'C:\Users\xxx\AppData\Local\Continuum\anaconda3\envs\lucy\Library\bin\ipopt.exe c:\users\xxx\appdata\local\temp\tmpp2hmid.pyomo.nl -AMPL' Error message: [Error 6] The handle is invalid
Detailed traceback: File "", line 46, in File "C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyomo\opt\base\solvers.py", line 578, in solve _status = self._apply_solver() File "C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 246, in _apply_solver self._rc, self._log = self._execute_command(self._command) File "C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 309, in _execute_command tee = self._tee File "C:\Users\xxx\AppData\Local\CONTIN~1\ANACON~1\envs\lucy\lib\site-packages\pyutilib\subprocess\processmngr.py", line 660, in run_command
基于 this 的可重现示例。
纯python代码(当我运行它在python,在conda
环境中调用"lucy"时它有效):
from pyomo.environ import *
infinity = float('inf')
model = AbstractModel()
# Foods
model.F = Set()
# Nutrients
model.N = Set()
# Cost of each food
model.c = Param(model.F, within=PositiveReals)
# Amount of nutrient in each food
model.a = Param(model.F, model.N, within=NonNegativeReals)
# Lower and upper bound on each nutrient
model.Nmin = Param(model.N, within=NonNegativeReals, default=0.0)
model.Nmax = Param(model.N, within=NonNegativeReals, default=infinity)
# Volume per serving of food
model.V = Param(model.F, within=PositiveReals)
# Maximum volume of food consumed
model.Vmax = Param(within=PositiveReals)
# Number of servings consumed of each food
model.x = Var(model.F, within=NonNegativeIntegers)
# Minimize the cost of food that is consumed
def cost_rule(model):
return sum(model.c[i]*model.x[i] for i in model.F)
model.cost = Objective(rule=cost_rule)
# Limit nutrient consumption for each nutrient
def nutrient_rule(model, j):
value = sum(model.a[i,j]*model.x[i] for i in model.F)
return model.Nmin[j] <= value <= model.Nmax[j]
model.nutrient_limit = Constraint(model.N, rule=nutrient_rule)
# Limit the volume of food consumed
def volume_rule(model):
return sum(model.V[i]*model.x[i] for i in model.F) <= model.Vmax
model.volume = Constraint(rule=volume_rule)
opt = SolverFactory('ipopt')
instance = model.create_instance('diet.dat')
results = opt.solve(instance, tee=False)
results
运行 在 R 中 reticulate
的代码非常简单:
library(reticulate)
use_condaenv(condaenv = "lucy")
py_run_file("../pyomo_scripts/test.py")
最后,为了完整起见,这是 diet.dat
文件(必须与 python/R 文件位于同一路径):
param: F: c V :=
"Cheeseburger" 1.84 4.0
"Ham Sandwich" 2.19 7.5
"Hamburger" 1.84 3.5
"Fish Sandwich" 1.44 5.0
"Chicken Sandwich" 2.29 7.3
"Fries" .77 2.6
"Sausage Biscuit" 1.29 4.1
"Lowfat Milk" .60 8.0
"Orange Juice" .72 12.0 ;
param Vmax := 75.0;
param: N: Nmin Nmax :=
Cal 2000 .
Carbo 350 375
Protein 55 .
VitA 100 .
VitC 100 .
Calc 100 .
Iron 100 . ;
param a:
Cal Carbo Protein VitA VitC Calc Iron :=
"Cheeseburger" 510 34 28 15 6 30 20
"Ham Sandwich" 370 35 24 15 10 20 20
"Hamburger" 500 42 25 6 2 25 20
"Fish Sandwich" 370 38 14 2 0 15 10
"Chicken Sandwich" 400 42 31 8 15 15 8
"Fries" 220 26 3 0 15 0 2
"Sausage Biscuit" 345 27 15 4 0 20 15
"Lowfat Milk" 110 12 9 10 4 30 0
"Orange Juice" 80 20 1 2 120 2 2 ;
评论后编辑:
这些是 pyomo
和 ipopt
pyomo 5.6.4 py36_0 conda-forge
pyomo.extras 3.3 py36_182212 conda-forge
ipopt 3.11.1 2 conda-forge
我继承了 R 中的大量代码,并通过系统调用在 pyomo
中完成了优化。我正在尝试通过使用 reticulate
来改进它,这样我就可以避免写入和读取文件,并且我有更多的控制权......如果我仍然在 python 内进行系统调用,我将获得很少的收益使用 reticulate
.
谢谢。
我不能说我完全理解这个问题,但是研究它是一个非常有趣的问题,主要是因为我收到了不同的错误消息
TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object
虽然我每次在新的 r 会话中 运行 py_run_file("test.py")
都会收到错误,但到第二个 运行 时就没有错误了。
话虽这么说,但我认为这与此问题有关: https://github.com/PyUtilib/pyutilib/issues/31
添加两行后我没有遇到任何问题:
import pyutilib.subprocess.GlobalData
pyutilib.subprocess.GlobalData.DEFINE_SIGNAL_HANDLERS_DEFAULT = False
在调用求解器之前的 python 脚本中。
希望对您有所帮助
如果可以执行python版本,尝试用下面的代码
以管理权限r sessionlibrary("reticulate")
##-- your directory containing 'diet.py' and 'diet.dat'
setwd("D:/project/Dropbox/lectures/2104xxx scg_opt/src/02"")
##-- execute code
a <- py_run_file("diet.py",local=T)
a$results