如何在Django-Heroku应用程序中运行Pyomo优化问题?

hgb9j2n6  于 2023-06-06  发布在  Go
关注(0)|答案(1)|浏览(545)

我一直在尝试部署一个使用python优化包'pyomo' https://pyomo.readthedocs.io/en/stable/index.html的Django应用程序
它可以在本地工作,但部署到heroku时就不行了。在生产环境中,无法找到求解器的上载可执行文件。从日志(该应用程序目前被称为'number_generator'):

2023-05-30T19:49:36.256914+00:00 app[web.1]: RuntimeError: Attempting to use an unavailable solver.
2023-05-30T19:49:36.256915+00:00 app[web.1]:
2023-05-30T19:49:36.256915+00:00 app[web.1]: The SolverFactory was unable to create the solver "HIGHS"
2023-05-30T19:49:36.256915+00:00 app[web.1]: and returned an UnknownSolver object.  This error is raised at the point
2023-05-30T19:49:36.256915+00:00 app[web.1]: where the UnknownSolver object was used as if it were valid (by calling
2023-05-30T19:49:36.256915+00:00 app[web.1]: method "solve").
2023-05-30T19:49:36.256915+00:00 app[web.1]:
2023-05-30T19:49:36.256916+00:00 app[web.1]: The original solver was created with the following parameters:
2023-05-30T19:49:36.256916+00:00 app[web.1]: executable: /app/number_generator/executables/highs
2023-05-30T19:49:36.256916+00:00 app[web.1]: type: HIGHS
2023-05-30T19:49:36.256916+00:00 app[web.1]: _args: ()
2023-05-30T19:49:36.256916+00:00 app[web.1]: options: {}
2023-05-30T19:49:36.257340+00:00 app[web.1]: 10.1.20.80 - - [30/May/2023:19:49:36 +0000] "POST /opttool/ HTTP/1.1" 500 145 "https://simple-esa.herokuapp.com/opttool/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
2023-05-30T19:49:36.257469+00:00 heroku[router]: at=info method=POST path="/opttool/" host=simple-esa.herokuapp.com request_id=73e964ec-dfca-487b-8b8c-319d01ce55f0 fwd="2.106.20.24" dyno=web.1 connect=0ms service=11075ms status=500 bytes=445 protocol=https

我使用了'highs'求解器和以下文件夹配置,以及相应的视图函数来加载可执行文件:
一个使用静态文件夹:

> ..
> number_generator
>    static
>      highs
>        highs.exe
> ..
import pyomo.environ as pyo
from django.contrib.staticfiles import finders
...

def opttool(...):
    ...
    p = finders.find('highs/highs.exe')
    p = p.replace('.exe', '')
    opt = pyo.SolverFactory('HIGHS', executable=p)
    ...

...一个位于静态文件夹的同一级别:

> ..
> number_generator
>   static
>   executables
>     highs.exe
> ..
import pyomo.environ as pyo
import os
...

def opttool(...):
    ...
    p = os.path.join(os.path.dirname(__file__), 'executables', 'highs.exe')
    p = p.replace('.exe', '')
    opt = pyo.SolverFactory('HIGHS', executable=p)
    ...

如上所述,两者都可以在本地工作,但部署到heroku时就不行了。有什么想法吗

更新:

AirSquid提出了一个很好的建议(2023年5月30日),尝试在CLI上导航并测试.exe文件是否正常工作。这对我来说是新的(而且似乎非常有用!),可以使用以下命令执行此操作:

heroku run bash --app your-app-name

..而AirSquid是对的它没有:

~/number_generator/executables $ ls 
docs  highs.exe
~/number_generator/executables $ highs --version
bash: highs: command not found

...在桌面上时:

C:\GitRepos\Basic-ESA\number_generator\executables>highs
usage: highs [options] stub [-AMPL] [<assignment> ...]

Options:
        --  end of options
        -=  show solver options and exit
        -?  show usage and exit
        -a  show solver options (ASL style, 1st synonyms if provided) and exit
        -c  show constraint descriptions and exit
        -e  suppress echoing of assignments
        -s  write .sol file (without -AMPL)
        -v  show version and exit

问题可能是.exe文件在Linux上不可执行?..并且Heroku接口在Linux上运行..?我尝试在heroku CLI上下载并安装glpk(当然在pyomo中改为'glpk'):pyo.SolverFactory('glpk')...我假设这会起作用,因为我可以在本地(在Windows上)安装这个求解器后运行gurobi,而无需指定可执行文件。但是我的heroku应用程序在安装后仍然找不到glpk。这可能是因为heroku似乎没有永久保存这个,但只在会话中保存:几分钟后我访问了CLI,glpk丢失了:

~ $ ls
db.sqlite3  manage.py  number_generator  Procfile  __pycache__  random_number_generator  ReadMe  requirements.txt  runtime.txt  staticfiles

所以我猜

  • 一个可以上传到staticfiles的独立的linux求解器二进制文件可能是解决方案。我从https://www.coin-or.org/download/binary/Clp/尝试了CLP,但Clp-1.7.4-linux-x86版本似乎在生产环境中无法工作(在本地它导致了ApplicationError,这对我来说是有意义的,因为我运行的是Windows):
~/number_generator/executables/CLP/bin $ ls
clp
~/number_generator/executables/CLP/bin $ clp --version
bash: clp: command not found
  • 可能需要在heroku部署时/部署期间安装求解器,然后定位二进制文件。我可能要学习使用这个“buildpack”工具?https://github.com/heroku/heroku-buildpack-apt
  • 可能存在更简单的解决方案?
6ie5vjzr

6ie5vjzr1#

解决方案!

我的猜测。2以上工作!我找到了一个GLPK ubuntu包(还有其他求解器):https://packages.ubuntu.com/kinetic/glpk-utils
然后我将apt-get的构建包添加到Heroku https://github.com/heroku/heroku-buildpack-apt(参见https://devcenter.heroku.com/articles/buildpacks
这成功地在部署时安装了GLPK。然后,我可以在heroku CLI中使用which glpsol找到glpsol二进制文件的路径,这意味着通过pyomo的调用可以工作。在我的例子中,它是在'.apt/usr/bin/glpsol'中:

pyo.SolverFactory('glpk', executable='.apt/usr/bin/glpsol')

相关问题