Ubuntu 20.04 源码安装 ipopt 和 CasADi

本文详细介绍了在LinuxUbuntu20.04环境下安装Ipopt和Casadi库的过程,包括遇到的问题如HSL编译错误的解决方法,以及Casadi的安装测试和常见问题处理。
摘要由CSDN通过智能技术生成

1 Ipopt 安装

casadi 的非线性优化功能是调用的 ipopt 库,所以需要先安装 ipopt。

参考网址如下:Linux | Ubuntu 20.04安装ipopt和cppAD | 安装全流程+报错解决_csdn ipopt anzhaung-CSDN博客。具体采用方式二进行安装。

简单步骤如下:

  1. 安装所需依赖
sudo apt-get install gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev 
  1. 创建一个存放所有跟 ipopt 相关的文件夹,便于管理
mkdir ~/Ipopt_pkg
cd Ipopt_pkg
  1. 安装 ASL
git clone https://github.com/coin-or-tools/ThirdParty-ASL.git
cd ThirdParty-ASL
sudo ./get.ASL
sudo ./configure
sudo make
sudo make install
cd ..
  1. 安装 HSL
git clone https://github.com/coin-or-tools/ThirdParty-HSL.git
cd ThirdParty-HSL

接下来需要下载 coinhsl 文件,并解压到 ThirdParty-HSL 目录下。其中 coinhsl 文件下载方式如下: 想要下载 coinhsl 文件需要申请许可证,许可证申请网址为:STFC IP Store,其中可以申请的类型为 Coin-HSL 或者 Coin-HSL Archive 类型。 Coin-HSL 类型只有学术机构可以免费申请,其它机构则需要付费,而且需要经过审批,时间较长。Coin-HSL Archive 类型的申请马上就可以通过,着急使用可以申请这个。申请通过后,将 coinhsl 文件在 ThirdParty-HSL 目录解压并将解压后的文件夹重命名为 coinhsl。之后再在 ThirdParty-HSL 目录下,执行以下命令。

sudo ./configure
sudo make
sudo make install
cd ..
  1. 安装 MUMPS
git clone https://github.com/coin-or-tools/ThirdParty-Mumps.git
cd ThirdParty-Mumps
sudo ./get.Mumps
sudo ./configure
sudo make
sudo make install
cd ..
  1. 安装 Ipopt
git clone https://github.com/coin-or/Ipopt.git
cd Ipopt
mkdir build
cd build
sudo ../configure
sudo make
sudo make test
sudo make install
  1. 完善环境
cd /usr/local/include
sudo cp coin-or coin -r
sudo ln -s /usr/local/lib/libcoinmumps.so.3 /usr/lib/libcoinmumps.so.3
sudo ln -s /usr/local/lib/libcoinhsl.so.2 /usr/lib/libcoinhsl.so.2
sudo ln -s /usr/local/lib/libipopt.so.3 /usr/lib/libipopt.so.3

到这里都没有报错则 ipopt 安装完成。

2 casadi 安装

Casadi 的安装和测试可以参考该帖子:非线性求解器 Casadi (c++使用例子)_casadi c+±CSDN博客,下面复制了该帖子写的一个脚本,一键完成安装。需要安装更高的版本也是类似,或者参照 casadi 官方 github 的链接中的安装方式。

#!/usr/bin/env bash

# Fail on first error.
set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo -e "\033[40;32m${DIR} \033[0m"

# download
wget https://github.com/casadi/casadi/releases/download/3.5.5/casadi-3.5.5-1.tar.gz
tar -zxvf casadi-3.5.5-1.tar.gz
echo -e "\033[40;32mdownload finish \033[0m"

cd casadi-3.5.5.1
mkdir build && cd build
cmake .. -DWITH_IPOPT=ON -DWITH_EXAMPLES=OFF
make -j4
sudo make install
sudo ldconfig

# Clean up.
sudo apt-get clean && sudo rm -rf /var/lib/apt/lists/*
sudo rm -fr casadi-3.5.5-1.tar.gz casadi-3.5.5.1

3 Casadi 安装测试

安装完成后,测试下 casadi 是否成功安装。为了方便测试,将测试程序打包成了一个压缩包,点此下载该压缩包进行测试。

下载后解压进入该文件夹,文件目录如下:

.
├── CMakeLists.txt
└── src
    └── casadi_test.cpp

执行以下命令编译测试文件

mkdir build
cd build/
cmake ..
make

编译成功后,在 build文件夹下执行 ./casadi_test_node 命令执行测试程序。如果 casadi 正确安装,那么终端中就会有如下输出:

casadi_test
x:[x_0, x_1, x_2]
p:[p_0, p_1]
f:((sq(x_0)+sq(x_1))+sq(x_2))
g:[((((6*x_0)+(3*x_1))+(2*x_2))-p_0), ((((p_1*x_0)+x_1)-x_2)-1)]

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit https://github.com/coin-or/Ipopt
******************************************************************************

This is Ipopt version 3.14.16, running with linear solver ma27.

Number of nonzeros in equality constraint Jacobian...:        6
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:        3

Total number of variables............................:        3
                     variables with only lower bounds:        3
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        2
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  4.5100000e-02 3.63e+00 4.11e-01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  5.8681488e-01 0.00e+00 1.95e+00  -1.0 3.91e-01    -  3.37e-01 1.00e+00h  1
   2  5.9327019e-01 1.11e-16 1.00e-06  -1.0 1.32e-02    -  1.00e+00 1.00e+00f  1
   3  5.6004673e-01 0.00e+00 4.92e-02  -2.5 8.93e-02    -  9.26e-01 1.00e+00f  1
   4  5.5264341e-01 0.00e+00 2.83e-08  -2.5 4.42e-02    -  1.00e+00 1.00e+00f  1
   5  5.5114453e-01 8.88e-16 1.50e-09  -3.8 2.36e-02    -  1.00e+00 1.00e+00f  1
   6  5.5102559e-01 1.78e-15 1.50e-09  -3.8 7.16e-03    -  1.00e+00 1.00e+00f  1
   7  5.5102042e-01 8.88e-16 1.84e-11  -5.7 1.77e-03    -  1.00e+00 1.00e+00f  1
   8  5.5102041e-01 8.88e-16 2.51e-14  -8.6 6.77e-05    -  1.00e+00 1.00e+00h  1
   9  5.5102041e-01 0.00e+00 9.17e-15  -9.0 9.29e-08    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 9

                                   (scaled)                 (unscaled)
Objective...............:   5.5102040816326525e-01    5.5102040816326525e-01
Dual infeasibility......:   9.1685374877122625e-15    9.1685374877122625e-15
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   9.0911698984215914e-10    9.0911698984215914e-10
Overall NLP error.......:   9.0911698984215914e-10    9.0911698984215914e-10


Number of objective function evaluations             = 10
Number of objective gradient evaluations             = 10
Number of equality constraint evaluations            = 10
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 10
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 9
Total seconds in IPOPT                               = 0.001

EXIT: Optimal Solution Found.
      solver  :   t_proc      (avg)   t_wall      (avg)    n_eval
       nlp_f  |   4.00us (400.00ns)   4.15us (415.40ns)        10
       nlp_g  |  14.00us (  1.40us)   9.50us (950.10ns)        10
    nlp_grad  |   2.00us (  2.00us)   1.40us (  1.40us)         1
  nlp_grad_f  |  14.00us (  1.27us)  11.14us (  1.01us)        11
  nlp_hess_l  |   5.00us (555.56ns)   4.56us (506.78ns)         9
   nlp_jac_g  |   7.00us (636.36ns)   5.98us (543.18ns)        11
       total  |   1.41ms (  1.41ms)   8.76ms (  8.76ms)         1
--------------------------------
Optimal solution for p = [5, 1]:
                   Objective: 0.55102
             Primal solution: [0.632653, 0.387755, 0.0204082]
           Dual solution (x): [-1.43695e-09, -2.3445e-09, -4.45467e-08]
           Dual solution (g): [-0.163265, -0.285714]

好了,至此 casadi 就安装成功了。

4 Ipopt 安装过程中出现的一个问题

配置过程中提示配置出现以下问题:

checking for function ma27ad_ in -L/usr/local/lib -lcoinhsl ... no checking for function ma27ad in -L/usr/local/lib -lcoinhsl ... no checking for function MA27AD_ in -L/usr/local/lib -lcoinhsl ... no checking for function MA27AD in -L/usr/local/lib -lcoinhsl ... no checking for function ma27ad__ in -L/usr/local/lib -lcoinhsl ... no checking for function ma27ad_ in -L/usr/local/lib -lcoinhsl ... no checking for function MA27AD__ in -L/usr/local/lib -lcoinhsl ... no checking for function MA27AD_ in -L/usr/local/lib -lcoinhsl ... no configure: error: Provided package HSL is not working or does not contain MA27. See config.log for details on failed checks.”

发现是 HSL 没有正常安装,在 HSL 的编译目录下执行以下操作:

4.1 清理之前的配置:

make distclean

这将删除之前生成的配置文件和对象文件,为重新配置做准备。

4.2 重新运行 configure:

./configure LIBS="-ldl"

在这个命令中,我们显式指定了 -ldl 库,以便解决一些在 configure 时可能出现的链接问题。这是为了确保在链接时包含动态链接库的正确版本。

4.3 检查配置结果:

查看 configure 的输出,确保没有出现类似于 unrecognized command line option 的错误。

4.4 继续编译

make
make install

对自己学习过程中的成功安装过程做过记录并做个分享。

  • 13
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ubuntu 20.04上进行源码安装OpenCV,你可以按照以下步骤进行操作: 1. 首先,确保你的系统已经更新到最新版本。你可以运行以下命令来更新系统: ``` sudo apt update sudo apt upgrade ``` 2. 创建一个目录来存放OpenCV的源码和构建文件。你可以运行以下命令来创建目录: ``` mkdir ~/opencv_build && cd ~/opencv_build ``` 3. 克隆OpenCV和OpenCV contrib的源码到你的本地目录。你可以运行以下命令来克隆源码: ``` git clone https://github.com/opencv/opencv.git git clone https://github.com/opencv/opencv_contrib.git ``` 4. 进入到OpenCV源码目录,并创建一个用于构建的目录。你可以运行以下命令来进入目录并创建构建目录: ``` cd opencv mkdir build && cd build ``` 5. 使用CMake配置OpenCV的构建选项。你可以运行以下命令来配置构建: ``` cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules .. ``` 6. 编译和安装OpenCV。你可以运行以下命令来进行编译和安装: ``` make -j$(nproc) sudo make install ``` 7. 安装完成后,你可以通过运行以下命令来验证OpenCV是否成功安装: ``` pkg-config --modversion opencv4 ``` 这样,你就可以在Ubuntu 20.04上成功进行源码安装OpenCV了。请注意,这个过程可能需要一些时间和资源,具体取决于你的系统配置和网络速度。希望对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [如何在 Ubuntu 20.04安装 OpenCV](https://blog.csdn.net/qq_33532713/article/details/122994933)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [在 Ubuntu 20.04安装 OpenCV](https://blog.csdn.net/qq_58060770/article/details/127553911)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值