Linux | Ubuntu 20.04安装ipopt和cppAD | 安装全流程+报错解决
文章目录
- 参考资料
- 1. Ipopt安装
- 1. 方式1: 命令行安装
- 2. 方式2:源码安装
- 3. 方式3:源码安装
- 4. Ipopt测试
- 5. 报错修复
- 2. CppAD安装
- 1. 方式1:命令行安装
- 2. 方式2:源码方式安装
- 3. CppAD测试
- 3. 测试Ipopt与CppAD是否可用
- 1. 例子
- 2. 编译报错解决
- 1. Undefined reference to `Ipopt::IpoptApplication::IpoptApplication(bool, bool)'
- 2. fatal error: coin/IpIpoptApplication.hpp: No such file or directorycompilation terminated.
参考资料
https://github.com/udacity/CarND-MPC-Quizzes/blob/master/install_Ipopt_CppAD.md
https://blog.csdn.net/qq_34525916/article/details/119186692#:~:text=%E6%A6%82%E8%A7%88
https://coin-or.github.io/CppAD/doc/install.htm
Ubuntu20.04 安装 Ipopt + cppAD流程
coinor库的安装与问题解决
Undefined reference to `Ipopt::IpoptApplication::IpoptApplication(bool, bool)’
在进行cppad和ipopt的安装时,发现在按照其他博主写的博客进行安装时总是会出现各种各样他们没遇到的问题,所以这里也记录下自己成功安装的步骤。
1. Ipopt安装
这边提供3种方式进行安装,可以先尝试使用方式1,方式1不行的话尝试方式2,方式2不行最后再尝试方式3.
1. 方式1: 命令行安装
sudo apt-get install coinor-libipopt
我这边使用命令行安装时显示无法定位软件包,在更新软件源后依旧无法生效,所以这个方式我放弃了。
2. 方式2:源码安装
安装依赖
sudo apt-get install gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev
创建一个存放所有跟
Ipopt
相关的文件夹,便于管理mkdir ~/Ipopt_pkg cd Ipopt_pkg
安装
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 ..
安装
HSL
git clone https://github.com/coin-or-tools/ThirdParty-HSL.git cd ThirdParty-HSL # 接下来需要下载coinhsl文件,并解压到ThirdParty-HSL目录下
下载coinhsl文件,并解压到
ThirdParty-HSL
目录下在
ThirdParty-HSL
目录下,执行以下命令sudo ./configure sudo make sudo make install cd ..
安装
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 ..
安装
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
完善环境
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
到这里都没有报错则安装完成。
3. 方式3:源码安装
首先,安装依赖
sudo apt-get install gfortran apt-get install unzip
下载Ipopt压缩包,并解压。
wget https://www.coin-or.org/download/source/Ipopt/Ipopt-3.12.7.zip unzip Ipopt-3.12.7.zip
在使用
wget
方式下载Ipopt压缩包时,可能因为网络原因下载不了,那么可以直接进入网址,找到Ipopt-3.12.7.zip进行手动下载即可。
下载并解压完成后,进入
Ipopt-3.12.7
文件夹,执行新建文件install_ipopt.sh
touch install_ipopt.sh
打开该文件,并写入以下内容:
# Pass the Ipopt source directory as the first argument if [ -z $1 ] thenecho "Specifiy the location of the Ipopt source directory in the first argument."exit fi cd $1prefix=/usr/local srcdir=$PWDecho "Building Ipopt from ${srcdir}" echo "Saving headers and libraries to ${prefix}"# BLAS cd $srcdir/ThirdParty/Blas ./get.Blas mkdir -p build && cd build ../configure --prefix=$prefix --disable-shared --with-pic make install# Lapack cd $srcdir/ThirdParty/Lapack ./get.Lapack mkdir -p build && cd build ../configure --prefix=$prefix --disable-shared --with-pic \--with-blas="$prefix/lib/libcoinblas.a -lgfortran" make install# ASL cd $srcdir/ThirdParty/ASL ./get.ASL# MUMPS cd $srcdir/ThirdParty/Mumps ./get.Mumps# build everything cd $srcdir ./configure --prefix=$prefix coin_skip_warn_cxxflags=yes \--with-blas="$prefix/lib/libcoinblas.a -lgfortran" \--with-lapack=$prefix/lib/libcoinlapack.a make make test make -j1 install
给该文件添加可执行权限:
sudo chmod +x install_ipopt.sh
然后运行脚本:
./install_ipopt.sh Ipopt-3.12.7
运行脚本后,有可能因为网络问题运行失败,可以多尝试几次。
最后静静等待安装即可。
最终,我是使用方式2步骤进行安装的。方式3因为网络问题,比较难成功。
4. Ipopt测试
进入 IPOPT 源码文件夹如下位置,用官方例子测试
cd Ipopt/build/examples/Cpp_example
sudo make
./Cpp_example
出现以下界面说明安装成功
5. 报错修复
安装完成后,有可能后期在使用阶段会编译报错:
/usr/include/coin/IpSmartPtr.hpp:19:4: error: #error "don't have header file for stddef"
需要通过以下方式解决。
打开文件
sudo gedit /usr/include/coin/IpSmartPtr.hpp
在文件的预处理部分,添加如下内容
#define HAVE_CSTDDEF // 添加部分
#ifdef HAVE_CSTDDEF
# include <cstddef>
#else
# ifdef HAVE_STDDEF_H
# include <stddef.h>
# else
# error "don't have header file for stddef"
# endif
#endif
#undef HAVE_CSTDDEF // 添加部分
2. CppAD安装
先尝试方式1,方式1不行再尝试方式2。
1. 方式1:命令行安装
一般网上已有的教程都是说直接在终端中输入以下命令安装:
sudo apt-get install cppad
但是,使用这种方式可能找不到该库。
遇到这个问题时,可以尝试切换源。切换源方式见博客。切换源后再进行更新。
sudo apt update
然后再重新尝试安装。
安装完成后如果需要卸载,则执行以下命令:
sudo apt-get remove cppad
如果上述方式无法成功安装,可采取下面源码的方式安装。
2. 方式2:源码方式安装
使用源码的方式安装。
下载源码:
git clone https://github.com/coin-or/CppAD.git
下载完成后,进入
CppAD
文件夹,然后执行以下命令:mkdir build cd build cmake .. make sudo make install
即可安装成功。
如果需要卸载的话,在
CppAD/build
文件夹内执行以下命令即可:sudo make uninstall
3. CppAD测试
使用以下代码测试CppAD是否安装成功。
CppAD_demo.cpp
文件# include <iostream> // standard input/output # include <vector> // standard vector # include <cppad/cppad.hpp> // the CppAD packagenamespace { // begin the empty namespace// define the function Poly(a, x) = a[0] + a[1]*x[1] + ... + a[k-1]*x[k-1]template <class Type>Type Poly(const CPPAD_TESTVECTOR(double) &a, const Type &x){ size_t k = a.size();Type y = 0.; // initialize summationType x_i = 1.; // initialize x^ifor(size_t i = 0; i < k; i++){ y += a[i] * x_i; // y = y + a_i * x^ix_i *= x; // x_i = x_i * x}return y;} } // main program int main(void) { using CppAD::AD; // use AD as abbreviation for CppAD::ADusing std::vector; // use vector as abbreviation for std::vector// vector of polynomial coefficientssize_t k = 5; // number of polynomial coefficientsCPPAD_TESTVECTOR(double) a(k); // vector of polynomial coefficientsfor(size_t i = 0; i < k; i++)a[i] = 1.; // value of polynomial coefficients// domain space vectorsize_t n = 1; // number of domain space variablesvector< AD<double> > ax(n); // vector of domain space variablesax[0] = 3.; // value at which function is recorded// declare independent variables and start recording operation sequenceCppAD::Independent(ax);// range space vectorsize_t m = 1; // number of ranges space variablesvector< AD<double> > ay(m); // vector of ranges space variablesay[0] = Poly(a, ax[0]); // record operations that compute ay[0]// store operation sequence in f: X -> Y and stop recordingCppAD::ADFun<double> f(ax, ay);// compute derivative using operation sequence stored in fvector<double> jac(m * n); // Jacobian of f (m by n matrix)vector<double> x(n); // domain space vectorx[0] = 3.; // argument value for computing derivativejac = f.Jacobian(x); // Jacobian for operation sequence// print the resultsstd::cout << "f'(3) computed by CppAD = " << jac[0] << std::endl;// check if the derivative is correctint error_code;if( jac[0] == 142. )error_code = 0; // return code for correct caseelse error_code = 1; // return code for incorrect casereturn error_code; }
CMakeLists.txt
文件cmake_minimum_required(VERSION 3.21)project(testCPP)set(CMAKE_CXX_STANDARD 14)add_executable(CppAD_demo CppAD_demo.cpp)
在该项目目录内执行
mkdir build cd build cmake ../ make ./CppAD_demo
输出以下信息,则CppAD安装成功
3. 测试Ipopt与CppAD是否可用
以下例子来自于博客。
1. 例子
利用CppAD与Ipopt求解以下非线性规划问题
minimizex1x4(x1+x2+x3)+x3s.t. x1x2x3x4≥25x12+x22+x32+x42=401≤x1,x2,x3,x4≤5\begin{array}{cc}\operatorname{minimize} & x_1 x_4\left(x_1+x_2+x_3\right)+x_3 \\ \text { s.t. } & x_1 x_2 x_3 x_4 \geq 25 \\ & x_1^2+x_2^2+x_3^2+x_4^2=40 \\ & 1 \leq x_1, x_2, x_3, x_4 \leq 5\end{array}minimize s.t. x1x4(x1+x2+x3)+x3x1x2x3x4≥25x12+x22+x32+x42=401≤x1,x2,x3,x4≤5
cppad_ipopt_demo.cpp
文件#include <iostream> #include <cppad/ipopt/solve.hpp>using namespace std;namespace {using CppAD::AD; class FG_eval {public:typedef CPPAD_TESTVECTOR(AD<double>) ADvector;void operator()(ADvector& fg, const ADvector& x){assert(fg.size() == 3);assert(x.size() == 4);// variablesAD<double> x1 = x[0];AD<double> x2 = x[1];AD<double> x3 = x[2];AD<double> x4 = x[3];// f(x) objective functionfg[0] = x1 * x4 * (x1 + x2 + x3) + x3;// constraintsfg[1] = x1 * x2 * x3 * x4;fg[2] = x1 * x1 + x2 * x2 + x3 * x3 + x4 * x4;return;}};}bool get_started(void) {bool ok = true;size_t i;typedef CPPAD_TESTVECTOR(double) Dvector;size_t nx = 4; // number of variblessize_t ng = 2; // number of constraintsDvector x0(nx); // initial condition of variblesx0[0] = 1.0;x0[1] = 5.0;x0[2] = 5.0;x0[3] = 1.0;// lower and upper bounds for variblesDvector xl(nx), xu(nx);for(i = 0; i < nx; i++){xl[i] = 1.0;xu[i] = 5.0;}Dvector gl(ng), gu(ng);gl[0] = 25.0; gu[0] = 1.0e19;gl[1] = 40.0; gu[1] = 40.0;// object that computes objective and constraintsFG_eval fg_eval;// optionsstring options;// turn off any printingoptions += "Integer print_level 0\n";options += "String sb yes\n";// maximum iterationsoptions += "Integer max_iter 10\n";//approximate accuracy in first order necessary conditions;// see Mathematical Programming, Volume 106, Number 1,// Pages 25-57, Equation (6)options += "Numeric tol 1e-6\n";//derivative tesingoptions += "String derivative_test second-order\n";// maximum amount of random pertubation; e.g.,// when evaluation finite diffoptions += "Numeric point_perturbation_radius 0.\n";CppAD::ipopt::solve_result<Dvector> solution; // solutionCppAD::ipopt::solve<Dvector, FG_eval>(options, x0, xl, xu, gl, gu, fg_eval, solution); // solve the problemcout<<"solution: "<<solution.x<<endl;////check some of the solution values//ok &= solution.status == CppAD::ipopt::solve_result<Dvector>::success;//double check_x[] = {1.000000, 4.743000, 3.82115, 1.379408};double check_zl[] = {1.087871, 0., 0., 0. };double check_zu[] = {0., 0., 0., 0. };double rel_tol = 1e-6; // relative tolerancedouble abs_tol = 1e-6; // absolute tolerancefor(i = 0; i < nx; i++){ok &= CppAD::NearEqual(check_x[i], solution.x[i], rel_tol, abs_tol);ok &= CppAD::NearEqual(check_zl[i], solution.zl[i], rel_tol, abs_tol);ok &= CppAD::NearEqual(check_zu[i], solution.zu[i], rel_tol, abs_tol);}return ok; }int main() {cout << "CppAD : Hello World Demo!" << endl;get_started();return 0; }
CMakeLists.txt
文件cmake_minimum_required(VERSION 3.21) project(testCPP)set(CMAKE_CXX_STANDARD 14)add_executable(cppad_ipopt_demo cppad_ipopt_demo.cpp) target_link_libraries(cppad_ipopt_demo ipopt)
在该项目目录内执行
mkdir build cd build cmake ../ make ./cppad_ipopt_demo
若输出如下信息,则说明Ipopt与CppAD安装成功。
2. 编译报错解决
1. Undefined reference to `Ipopt::IpoptApplication::IpoptApplication(bool, bool)’
这里需要注意的是,CMakeLists.txt
文件需要加入target_link_libraries(cppad_ipopt_demo ipopt)
链接,否则会报如下错误:
Undefined reference to `Ipopt::IpoptApplication::IpoptApplication(bool, bool)'
2. fatal error: coin/IpIpoptApplication.hpp: No such file or directorycompilation terminated.
若编译后报错:
fatal error: coin/IpIpoptApplication.hpp: No such file or directorycompilation terminated.
这是由缺少coin_or
库引起的,可以执行以下命令安装
sudo apt install coinor-libipopt-dev
以上所有代码存于github仓库。
Linux | Ubuntu 20.04安装ipopt和cppAD | 安装全流程+报错解决相关推荐
- DELL台式机Linux ubuntu 20.04.5 安装
DELL台式机Linux ubuntu 20.04.5 安装 陈拓 2022/09/07-2022/09/08 镜像下载 Index of /ubuntu-releases/20.04/ | 清华大学 ...
- Ubuntu 20.04.4 LTS下载、安装、部分配置
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.Ubuntu 20.04.4 LTS下载链接 二.安装过程 三.部分配置 1.更新源服务器为国内地址 2.安装中文 ...
- Ubuntu 20.04向日葵远程下载及安装记录
Ubuntu 20.04向日葵远程下载及安装记录 下载向日葵 下载向日葵用来远程,去向日葵官网,选择图形版本(命令行版本不支持发起远控) 安装向日葵 进入到下载目录,进行安装 sudo dpkg -i ...
- please reinstall the mysql distribution_php安装扩展mysqli的实现步骤及报错解决办法
php安装扩展mysqli的实现步骤及报错解决办法 terminal#cd php-5.3.6/ext/mysqli #/usr/local/webserver/php/bin/phpize #./c ...
- php mysqli报错,php安装扩展mysqli的实现步骤及报错解决办法
php安装扩展mysqli的实现步骤及报错解决办法 terminal #cd php-5.3.6/ext/mysqli #/usr/local/webserver/php/bin/phpize #./ ...
- cmd输入pip报错_安装虚拟环境+pip安装XXX包的常见操作与报错解决
安装虚拟环境+pip安装XXX包的常见操作与报错解决blog.csdn.net 文章目录: 一.开始前提 二.安装虚拟环境的常见操作 三.pip 的命令的常见操作:安装.更新.卸载.指定源 四.安装 ...
- php的mysqli步骤,php安装扩展mysqli的实现步骤及报错解决办法
php安装扩展mysqli的实现步骤及报错解决办法 php安装扩展mysqli的实现步骤及报错解决办法 terminal #cd php-5.3.6/ext/mysqli #/usr/local/we ...
- Linux Ubuntu 20.04安装UHD和GNU Radio
一.说明 本文使用Ubuntu 20.04系统.采用二进制包的方式安装UHD与GNU Radio. 为防止出现问题,要先安装UHD,再安装GNU Radio. 二.更换国内下载源 如果网速不好,会出现 ...
- win10应用商店linux,Ubuntu 20.04 LTS已可通过Windows 10应用商店获取
获取 Ubuntu 20.04 LTS:Microsoft Sotre 传送门 值得一提的是,这也是随附 Fast ID Online(简称 FIDO)的首个 Ubuntu 版本,可方便在操作系统中提 ...
最新文章
- linux环境中,查询网卡的速度(带宽)
- nb-iot_如何使用语音通过Android控制IoT设备
- 7-1 银行业务队列简单模拟 (25 分)
- 一篇文章搞定Linux基础操作
- 关于mysql的update、delete、和insert into能否使用别名问题
- chromium net库设计
- npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@
- gdb 调试 入手 实例讲解-转
- linux 系统常用的C语言函数库
- 我的世界服务器开服文件夹,详细开服教程。想要自己开服的小伙伴儿 戳这里·...
- “21天好习惯”第一期-7
- poj 1719 Shooting Contest 二分匹配
- csdn如何写出文章,拥有较高的推荐量以及点击率
- VM虚拟机下载及安装教程
- 分享大二时用python写的银行管理系统(超简单版)内附源码
- 1585 Amount of Degrees
- Ping其他电脑ping不通的解决方法
- oracle 表数据删除后恢复还原
- 随机点名器——java
- 语言 {软件开发概述}