lrn*_*cig 12 python r pyomo reticulate
我正在尝试进行pyomo优化,但收到错误消息[Error 6] The handle is invalid。不确定如何解释它,环顾四周似乎与特权有关,但我不太了解。
在下面找到完整的错误跟踪,以及一个再现它的玩具示例。
完整的错误跟踪:
py_run_file_impl(文件,本地,转换)中的错误:ApplicationError:无法执行命令:'C:\ Users \ xxx \ AppData \ Local \ Continuum \ anaconda3 \ envs \ lucy \ Library \ bin \ ipopt.exe c:\ users \ xxx \ appdata \ local \ temp \ tmpp2hmid.pyomo.nl -AMPL'错误消息:[错误6]句柄无效
详细的追溯:文件“ C:\ Users \ xxx \ AppData \ Local \ CONTIN〜1 \ ANACON〜1 \ envs \ lucy \ lib \ site-packages \ pyomo \ opt \ base \ solvers中的文件“”,第46行。 py”,在第578行中,解决_status = self._apply_solver()文件“ C:\ Users \ xxx \ AppData \ Local \ CONTIN〜1 \ ANACON〜1 \ envs \ lucy \ lib \ site-packages \ pyomo \ opt \ “ solver \ shellcmd.py”,行246,在_apply_solver self._rc中,self._log = self._execute_command(self._command)文件“ C:\ Users \ xxx \ AppData \ Local \ CONTIN〜1 \ ANACON〜1 \ envs \ lucy \ lib \ site-packages \ pyomo \ opt \ solver \ shellcmd.py”,第309行,位于_execute_command tee tee = self._tee文件“ C:\ Users \ xxx \ AppData \ Local \ CONTIN〜1 \ ANACON〜1 \ envs \ lucy \ lib \ site-packages \ pyutilib \ subprocess \ processmngr.py“,行660,在run_command中
基于此的可复制示例。
纯python代码(当我在conda称为“ lucy” 的环境中以python运行它时,它可以工作):
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
Run Code Online (Sandbox Code Playgroud)
在R中运行它的代码reticulate非常简单:
library(reticulate)
use_condaenv(condaenv = "lucy")
py_run_file("../pyomo_scripts/test.py")
Run Code Online (Sandbox Code Playgroud)
最后为了完整起见,这是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 ;
Run Code Online (Sandbox Code Playgroud)
评论后编辑:
这些都是版本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
Run Code Online (Sandbox Code Playgroud)
我已经继承了R中的大量代码,并pyomo通过系统调用完成了优化。我正在尝试通过使用来改进它,reticulate从而避免写入和读取文件,并且拥有更多的控制权...如果仍然可以在python中进行系统调用,则使用不会带来太多收益reticulate。
谢谢。
小智 1
如果可以执行python版本,请尝试使用以下代码以管理权限进行r会话
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
Run Code Online (Sandbox Code Playgroud)