简体   繁体   English

PYOMO:如何使用 glpk 创建求解器

[英]PYOMO: how to create a solver with glpk

I try to create a solver for a MILP-problem using PYOMO and glpk.我尝试使用 PYOMO 和 glpk 为 MILP 问题创建求解器。 I try我试试

import pyomo.environ as pyo

opt = pyo.SolverFactory('glpk')

just as described in the PYOMO documentation .正如PYOMO 文档中所述。

However, this just gets me a warning:但是,这只会给我一个警告:

WARNING: Failed to create solver with name 'glpk': Command
    '['C:\\Users\\andre\\anaconda3\\Library\\bin\\glpsol.exe', '--version']'
    timed out after 1 seconds

I have both PYOMO and glpk installed using conda install我使用 conda conda install安装了 PYOMO 和 glpk

And I can call glpsol from the command line - which should indicate everything is alright (see here ).我可以从命令行调用glpsol - 这应该表明一切正常(请参阅此处)。

Versions :版本

glpk: 5.0 glpk:5.0

pyomo: 6.0.1表模:6.0.1

python: 3.8.5 python:3.8.5

windows: windows 10 pro 21H1 windows:windows 10 亲 21H1

Any ideas how to solve this?任何想法如何解决这个问题?

SOLUTION:解决方案:

I have found something similar in the pyomo github issue 2102 .我在 pyomo github issue 2102中发现了类似的东西。 I have adopted the mentioned monkey-patch approach and it actually worked:我采用了提到的猴子补丁方法并且它确实有效:

import subprocess

def patched_subprocess_run(*args, **kwargs):
    if kwargs.get("timeout") is not None:
        kwargs["timeout"] = 5
    return orig_subprocess_run(*args, **kwargs)

orig_subprocess_run = subprocess.run
subprocess.run = patched_subprocess_run

# Your Pyomo call here.

So setting the timeout to 5 seconds works for me.所以将超时设置为 5 秒对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM