Centos7安裝SCIP求解器
SCIP介紹
SCIP is currently one of the fastest non-commercial solvers for mixed integer programming (MIP) and mixed integer nonlinear programming (MINLP). It is also a framework for constraint integer programming and branch-cut-and-price. It allows for total control of the solution process and the access of detailed information down to the guts of the solver.
背景介紹
公司最近有演算法專案(python3.6)需要部署在centos7服務器上,經過跟演算法工程師溝通發現,部署演算法專案有一個特別重要的東西就是求解器,本專案中用到了兩個求解器cplex和scip,經過部署發現,cplex求解器安裝比較簡單,可以查看我的另一篇博客 https://blog.csdn.net/weixin_39080216/article/details/114389788 , 比較復雜的就是scip,所以重點介紹,
安裝求解器演算法工程師給了一個測驗求解器demo如下:
# encoding=utf-8
import numpy as np
import pandas as pd
from pyomo.environ import *
import pyutilib.subprocess.GlobalData
pyutilib.subprocess.GlobalData.DEFINE_SIGNAL_HANDLERS_DEFAULT = False
def object_func(model):
return model.x + model.y
def constraint_01(model):
return model.x * model.y >= 10
model = ConcreteModel(name="cplex_test")
model.x = Var(bounds=(1,5),within=NonNegativeReals,initialize=1)
model.y = Var(bounds=(1,5),within=NonNegativeReals,initialize=1)
model.cons = ConstraintList()
model.cons.add(constraint_01(model))
model.obj = Objective(rule=object_func, sense=maximize)
//ampl編譯后可執行檔案絕對路徑
solver_path = '/root/scip/scipoptsuite-5.0.0/scip/interfaces/ampl/bin/scipampl'
opt = SolverFactory('scipampl', executable=solver_path)
results = opt.solve(model, tee=True)
results.write()
print(model.x.value)
print(model.y.value)
由于網上關于scip安裝的帖子都是結合PySCIPOpt使用,我也根據網上的帖子成功安裝了scip和PySCIPOpt https://blog.csdn.net/weixin_42690752/article/details/108429040 ,(此帖子在實際安裝程序中也遇到了一些問題,稍后再寫一篇博客專門介紹)
但是演算法工程卻要求不使用PySCIPOpt,必須使用pyomo,一是、使用PySCIPOpt相對編碼比較復雜,二、由于專案中使用了兩個求解器,為了使代碼保持統一,所以放棄了使用PySCIPOpt,
由于之前演算法是運行在windows系統上的,通過測驗demo就可以看出,solver_path的值就是求解器的可運行程式路徑,但是通過實際部署發現,cplex安裝成功后,直接可以在安裝目錄下bin檔案下找到一個cplex程式,以此路徑作為solver_path即可執行成功,但是scip經過上面介紹的博客安裝成功后,能找到可執行檔案,卻一直報錯,在網上找了好多資料也沒有解決,
最終通過請教別人,自己查閱資料還是解決了部署的問題,由于本人是java后臺工程師,兼職干一點部署的活,一下子遇到了這么復雜的部署,最終雖然解決了問題,但是整個程序還是有很多不理解,等使用程序中了解的更多以后再補充,
下載地址
https://www.scipopt.org/index.php#download
此處簡單說明一下 ,根據官網介紹scipopsuite中包含了scip 所以此處選擇下載scipoptsuite原始碼,版本:5.0.0
解壓
將下載的scipoptsuite-5.0.0.tgz 進行解壓:
tar -zxvf scipoptsuite-5.0.0.tgz
Python安裝
由于公司專案需要,Python大版本必須是3.6,因為網上關于Python教程多如牛毛,此處就不在贅述,貼出我安裝使用的教程鏈接:https://www.cnblogs.com/yangzhaon/p/11203395.html
安裝依賴
sudo yum install -y gmp-devel gcc-c++ zlib-devel readline-devel
由于在Python安裝程序中已經安裝了zlib-devel readline-devel所以只需要安裝前面兩個即可,
編譯scip
cd scipopsuite-5.0.0
make
此處編譯程序中問題較多,針對遇到的問題簡單整理下:
1. g++ 命令未找到
sudo yum install -y gcc-c++
2. gmp.h沒有那個檔案或目錄
sudo yum install -y gmp-devel
3. zlib.h: 沒有那個檔案或目錄
sudo yum install -y zlib-devel
4. readline/readline.h:沒有那個檔案或目錄
sudo yum install -y readline-devel
5. /usr/bin/ld: cannot find -lc
sudo yum install -y glibc-static libstdc++-static
編譯ampl
cd scip/interfaces/ampl/
./get.ASL
cd solvers
make -f makefile.u
cd ..
make
測驗
命令測驗
cd bin
./scipampl
如果出現以下結果說明安裝成功:

代碼測驗
1. requirements.txt
numpy
pandas
pyomo
pyutilib
2. 安裝依賴
pip3 install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple -r requirements.
3. 代碼運行
python3 cplex_test.py
4. 運行結果
出現以下結果說明部署成功,并且求解成功:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/266788.html
標籤:python
上一篇:Java中“equals()“與“==“聯系和區別的總結
下一篇:求助!【PAT】01-復雜度2 Maximum Subsequence Sum (25 分)一個測驗點怎么也過不去!
