我有一个python函数,可以根据我的需求使用pyomo和glpk求解器来优化数据集。当iam直接在python中运行此代码时,它给出正确的答案,但是当iam尝试使用网状包从R运行python函数时,则会引发以下错误:
py_call_impl(可调用,dots $ args,dots $ keywords)错误: RuntimeError:尝试使用不可用的求解器。
SolverFactory无法创建求解器“ glpk” 并返回了UnknownSolver对象。此时会引发此错误 将该UnknownSolver对象当作有效对象使用的位置(通过调用 方法“解决”)。
我尝试在R中安装ROI.plugin.glpk,并尝试在我的python代码中为glpk求解器使用此新路径,但这也没有解决
下面是python代码
import numpy as np
import pandas as pd
import shutil
import sys
import os.path
from pyomo.environ import *
from pyomo.gdp import *
import pyomo.environ
def c1(model, d):
days = ['d1', 'd2', 'd3','d4','d5','d6']
idx = days.index(d)
if value(model.avl_inv[d]>=10):
if value(model.num_vessels[d]>1):
if d!= days[-1]:
idx = days.index(d)
nxt_day = days[idx+1]
model.num_vessels[nxt_day]+= model.num_vessels[d]-1
model.num_vessels[d] = 1
return model.x[d] == 1
else:
return model.x[d] == 1
elif value(model.num_vessels[d]==1):
return model.x[d]==1
else:
return model.x[d]==0
else:
if value(model.num_vessels[d]>=1):
if d!= days[-1]:
idx = days.index(d)
nxt_day = days[idx+1]
model.num_vessels[nxt_day]+= model.num_vessels[d]
model.num_vessels[d] = 0
return model.x[d]==0
else:
return model.x[d]==0
else:
return model.x[d]==0
def objective_rule(model):
return sum(model.x[d] for d in model.d)
def JobShopSolve(model, solvername, solverpath_exe):
SolverFactory('glpk',executable=solverpath_exe).solve(model)
results = [{'Day': j,
'Vessels': model.num_vessels[j](),
'inventory': model.avl_inv[j]}
for j in model.d]
return results
def model():
model = ConcreteModel()
model.d = Set(initialize=['d1', 'd2', 'd3','d4','d5','d6'], doc='days')
model.num_vessels = Param(model.d, initialize = {'d1':1, 'd2':2, 'd3':0, 'd4':1, 'd5':0, 'd6':0}, doc='available number of vessels',mutable = True)
model.avl_inv = Param(model.d, initialize = {'d1':10, 'd2':12, 'd3':8, 'd4':11, 'd5':10, 'd6':4}, doc='available number of inventory')
model.x = Var(model.d, bounds=(0.0,1.0))
model.demand = Constraint(model.d, rule=c1, doc='Satisfy constraint for one cargo loading on one vessel')
days = list(model.d.keys())
model.objective = Objective(rule=objective_rule, sense=minimize, doc='Define objective function')
TransformationFactory('gdp.chull').apply_to(model)
solvername='glpk'
solverpath_exe='C:\\Users\\shubhamkumar.agrawa\\AppData\\Local\\Continuum\\anaconda3\\pkgs\\glpk-4.65-h2fa13f4_1001\\Library\\bin\\glpsol'
results = JobShopSolve(model, solvername, solverpath_exe)
return results
使用以下命令从R调用上面的python代码:
library(reticulate)
source_python('test.py')
model()
在spyder中运行以上python代码集时的实际结果给出:
[{'Day': 'd1', 'Vessels': 1, 'inventory': 10},
{'Day': 'd2', 'Vessels': 1, 'inventory': 12},
{'Day': 'd3', 'Vessels': 0, 'inventory': 8},
{'Day': 'd4', 'Vessels': 1, 'inventory': 11},
{'Day': 'd5', 'Vessels': 1, 'inventory': 10},
{'Day': 'd6', 'Vessels': 0, 'inventory': 4}]
但是从R中调用相同的python脚本会引发以下错误:
py_call_impl(可调用,dots $ args,dots $ keywords)错误:
RuntimeError:尝试使用不可用的求解器。
SolverFactory无法创建求解器“ ROI.plugin.glpk” 并返回了UnknownSolver对象。此时会引发此错误 将该UnknownSolver对象当作有效对象使用的位置(通过调用 方法“解决”)。
任何线索都将得到真正的赞赏