Centos7安装SCIP with AMPL

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

如果是在以Centos7为基础镜像的容器里面执行./get.ASL会报

Utility wget not found in your PATH.

使用命令安装wget

yum -y install wget 

发现依然这个错,我们打开get.ASL文件查看发现了一个判断语句如下图所示:
在这里插入图片描述
我们将红线框内的语句在命令行执行,发现为0,然后直接使用which命令查找wget

which get

发现容器内没有which命令,但是容器内有whereis命令,我们只需将该文件内的which改为whereis即可。

测试

命令测试

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. 运行结果
出现以下结果说明部署成功,并且求解成功:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值