gpt4 book ai didi

python - pyomo + reticulate error 6 句柄无效

转载 作者:太空狗 更新时间:2023-10-29 17:50:26 25 4
gpt4 key购买 nike

我正在尝试运行 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 中运行它时,它在名为“lucy”的 conda 环境中运行):

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

使用 reticulate 在 R 中运行它的代码非常简单:

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 ;

评论后编辑:

这些是 pyomoipopt 的版本

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 中进行系统调用,我将通过使用获得很少的 yield 网状结构

谢谢。

最佳答案

我不能说我完全理解这个问题,但是研究它是一个非常有趣的问题,主要是因为我收到了不同的错误消息

TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object

虽然我每次在新的 r session 中运行 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 - pyomo + reticulate error 6 句柄无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56379818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com