CppAD 与 Ipopt 安装及测试
最近在研究非线性求解器,摸索了一下如何安装和使用。这位博主werty4321总结得非常好,上手就能用,因此记录一下,为了防止以后找不到,进行了转载。如果各位看官觉得有用,请到该知乎回答下面为博主点赞!
CPPAD安装
sudo apt-get install cppad
Ipopt安装
安装依赖
sudo apt-get install gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev
创建安装文件夹
mkdir ~/Ipopt
cd Ipopt
安装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文件,需要 下载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
将编译好的 .so 软链接到 /usr/local
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
测试CppAD与Ipopt是否可用
问题
min x 1 x 4 ( x 1 + x 2 + x 3 ) + x 3 s . t x 1 x 2 x 3 x 4 ≥ 25 x 1 2 + x 2 2 + x 3 2 + x 4 2 = 40 1 ≤ x 1 , x 2 , x 3 , x 4 ≤ 5 \min x_1x_4(x_1+x_2+x_3)+x_3 \\ s.t x_1x_2x_3x_4 \geq25 \\ 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 minx1x4(x1+x2+x3)+x3s.tx1x2x3x4≥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);
// variables
AD<double> x1 = x[0];
AD<double> x2 = x[1];
AD<double> x3 = x[2];
AD<double> x4 = x[3];
// f(x) objective function
fg[0] = x1 * x4 * (x1 + x2 + x3) + x3;
// constraints
fg[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 varibles
size_t ng = 2; // number of constraints
Dvector x0(nx); // initial condition of varibles
x0[0] = 1.0;
x0[1] = 5.0;
x0[2] = 5.0;
x0[3] = 1.0;
// lower and upper bounds for varibles
Dvector 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 constraints
FG_eval fg_eval;
// options
string options;
// turn off any printing
options += "Integer print_level 0\n";
options += "String sb yes\n";
// maximum iterations
options += "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 tesing
options += "String derivative_test second-order\n";
// maximum amount of random pertubation; e.g.,
// when evaluation finite diff
options += "Numeric point_perturbation_radius 0.\n";
CppAD::ipopt::solve_result<Dvector> solution; // solution
CppAD::ipopt::solve<Dvector, FG_eval>(options, x0, xl, xu, gl, gu, fg_eval, solution); // solve the problem
cout<<"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 tolerance
double abs_tol = 1e-6; // absolute tolerance
for(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.16)
project(cppad_ipopt_demo)
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
结果
CppAD : Hello World Demo!
solution: { 1, 4.743, 3.82115, 1.37941 }