在Google Colab中无法为IPOPT加载HSL MA97求解器的求助
在Google Colab中配置IPOPT+HSL MA97线性求解器的修复方案
问题背景
已成功运行未搭载HSL的IPOPT及cyipopt,但按以下步骤配置HSL MA97后无法正常工作:
- 执行
!apt install coinor-libipopt1v5 coinor-libipopt-dev安装IPOPT - 执行
!pip install -q cyipopt安装cyipopt - 执行
!git clone https://github.com/coin-or-tools/ThirdParty-HSL.git克隆ThirdParty-HSL - 执行
!cp -R /content/drive/MyDrive/solver/coinhsl/ /content/ThirdParty-HSL/、%cd /content/ThirdParty-HSL/、!./configure、!make、!make install复制并安装HSL
核心问题
通过apt安装的预编译IPOPT并未链接到自行编译的HSL库,必须重新编译IPOPT并指定HSL路径,才能让IPOPT调用MA97。
修复步骤
1. 清理现有冲突安装
卸载系统预装的IPOPT和cyipopt,避免版本冲突:
!apt remove -y coinor-libipopt1v5 coinor-libipopt-dev !pip uninstall -y cyipopt
2. 安装编译依赖
Colab需要补充编译工具和线性代数库:
!apt install -y build-essential gcc gfortran wget git pkg-config liblapack-dev libblas-dev
3. 重新安装HSL库
确保coinhsl目录包含MA97完整源码,重新编译安装HSL到系统路径:
!git clone https://github.com/coin-or-tools/ThirdParty-HSL.git !cp -R /content/drive/MyDrive/solver/coinhsl/ /content/ThirdParty-HSL/ %cd /content/ThirdParty-HSL/ !./configure --prefix=/usr/local !make !make install
4. 编译IPOPT并链接HSL
克隆IPOPT源码,编译时指定HSL安装路径:
%cd /content/ !git clone https://github.com/coin-or/Ipopt.git %cd Ipopt/ !mkdir build && cd build !../configure --with-hsl=/usr/local --prefix=/usr/local !make !make install
注意:检查
configure输出,确认出现checking for HSL library... yes,说明HSL被正确检测。
5. 重新安装cyipopt
编译cyipopt时链接刚编译的IPOPT:
!pip install cyipopt --no-binary cyipopt
如果上述命令失败,改用源码编译:
!git clone https://github.com/mechmotum/cyipopt.git %cd cyipopt/ !python setup.py install --ipopt-dir=/usr/local
6. 验证配置
运行以下代码确认MA97可用:
import cyipopt print("IPOPT版本:", cyipopt.__version__) print("可用求解器:", cyipopt.available_solvers())
若输出包含ma97,则配置成功。
常见排查点
- 确认
coinhsl目录结构完整,包含ma97子文件夹及所有源文件 - 若编译时出现Fortran错误,检查
gfortran是否已正确安装 - 确保
configure命令无报错,HSL路径指向正确
内容的提问来源于stack exchange,提问作者RetroD
相关产品推荐
相关产品推荐

