linux服务器下安装cbc和ipopt求解器【踩坑总结】

安装CBC求解器

CBC求解器是一个C++库,我们可以通过以下命令在Linux系统中进行安装:

sudo apt-get install coinor-cbc

对于Windows操作系统,可以从CBC官方网站下载。
安装完成后,我们可以使用CBC求解器来解决线性规划问题。
例如用pyomo求解

from pyomo.environ import *
model = ConcreteModel() # 定义模型
model.x = Var(within=NonNegativeReals) # 声明决策变量 x
model.y = Var(within=NonNegativeReals) # 声明决策变量 y
model.obj = Objective(expr=model.x + model.y, sense = minimize) # 声明目标函数为 x+y, minimize 表示极小化
model.constrs = Constraint(expr=model.x + model.y <= 1) # 添加约束 x+y <= 1
model.write('model.lp') # 输出模型文件
model.pprint() # 打印模型信息
opt = SolverFactory('cbc') # 指定Gurobi为求解器
solution = opt.solve(model) # 调用求解器求解

安装ipopt求解器

该方案是手动编译源码来实现。【可能还有其他的办法,目前我只实现了这个】
运行如下脚本来实现,需要预先下载好一些源码文件

#!/bin/bash
echo "Compile ipopt and dependencies:" # echo is used to printf in terminal
echo "dependencies: gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev"
apt-get update
apt-get -y upgrade
apt install build-essential
apt-get install -y gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev vim
echo "===========================dependencies: over====================================="
if [ ! -d "/Ipopt_pkg" ]; then
  mkdir /Ipopt_pkg
fi
cd Ipopt_pkg || exit
unzip Ipopt_pkg.zip
echo "===========================make ASL==========================="
if [ ! -d "/ThirdParty-ASL" ]; then
  git clone https://github.com/coin-or-tools/ThirdParty-ASL.git
fi
cd ThirdParty-ASL || exit
./get.ASL
./configure
make
make install
cd ..
echo "===========================make HSL==========================="
if [ ! -d "/ThirdParty-HSL" ]; then
  git clone https://github.com/coin-or-tools/ThirdParty-HSL.git
fi

cp coinhsl.zip ThirdParty-HSL
cd ThirdParty-HSL || exit
unzip coinhsl.zip
./configure
make
make install
cd ..
echo "===========================make Mumps==========================="
if [ ! -d "/ThirdParty-Mumps" ]; then
  git clone https://github.com/coin-or-tools/ThirdParty-Mumps.git
fi
cd ThirdParty-Mumps || exit
./get.Mumps
./configure
make
make install
cd ..
echo "===========================make Ipopt==========================="
if [ ! -d "/Ipopt" ]; then
  git clone https://github.com/coin-or/Ipopt.git
fi
cd Ipopt || exit
mkdir build
cd build || exit
../configure
make
make test
make install
echo "===========================Improve the environment==========================="
cd /usr/local/include || exit
cp coin-or coin -r
ln -s /usr/local/lib/libcoinmumps.so.3 /usr/lib/libcoinmumps.so.3
ln -s /usr/local/lib/libcoinhsl.so.2 /usr/lib/libcoinhsl.so.2
ln -s /usr/local/lib/libipopt.so.3 /usr/lib/libipopt.so.3
echo "Add the/usr/local/lib directory to the configuration file of the shared library"
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
echo "===========================Compile ipopt and dependencies: over!==========================="
posted @ 2024-01-10 10:39  薄书  阅读(377)  评论(0编辑  收藏  举报