目录
一、casadi介绍
二、源码安装步骤
三、VScode使用
1.CMakeLists文件
2. main.cpp 文件
3. 运行结果:
casadi可用于数值微分、积分、非线性规划求解等,提供C++、MATLAB、Python接口
具体介绍见官方网站:
https://web.casadi.org/docs/
github源码地址:
https://github.com/casadi
详情参考:InstallationLinux · casadi/casadi Wiki · GitHub
step1:安装必要的编译器
sudo apt-get install gcc g++ gfortran git cmake liblapack-dev pkg-config --install-recommends
step2:拷贝源码
git clone https://github.com/casadi
step3:编译与安装
cd casadi
mkdir build
cd build
cmake .. -DWITH_IPOPT=ON -DWITH_EXAMPLES=OFF
make
sudo make install
# 声明要求的 cmake 最低版本
cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 11)
project(TEST)
# 添加一个可执行程序
add_executable(test main.cpp)
# 添加相关库文件链接到工程
target_link_libraries(test /usr/local/lib/libcasadi.so.3.7)
# 设置输出的可执行文件存放目录
set_target_properties(test PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
代码参考:非线性求解器 Casadi (c++使用例子)_casadi c++_梦醒时分1218的博客-CSDN博客
#include
#include
#include
using namespace std;
using namespace casadi;
int main()
{
cout << "casadi_test" << endl;
// This is another way to define a nonlinear solver. Opti is new
/*
* min x1*x4*(x1 + x2 + x3) + x3
* s.t. x1*x2*x3*x4 >=25
x1^2 + x2^2 + x3^2 + x4^2 = 40
1 <= x1, x2, x3, x4 <= 5
*/
// Optimization variables
SX x = SX::sym("x", 4);
std::cout << "x:" << x << std::endl;
// Objective
SX f = x(0)*x(3)*(x(0) + x(1) + x(2)) + x(2);
// SX f = x(0) * x(0)*x(3) + x(0)*x(1)*x(3) + x(0)*x(2)*x(3)+ x(2);
std::cout << "f:" << f << std::endl;
// Constraints
// SX g = vertcat(6 * x(0) + 3 * x(1) + 2 * x(2) - p(0), p(1) * x(0) + x(1) - x(2) - 1);
SX g = vertcat(x(0)*x(1)*x(2)*x(3), pow(x(0),2) + pow(x(1),2) + pow(x(2),2) + pow(x(3),2));
std::cout << "g:" << g << std::endl;
// Initial guess and bounds for the optimization variables
vector x0 = { 0.0, 0.0, 0.0, 0.0 };
vector lbx = { 1, 1, 1, 1 };
vector ubx = {5, 5, 5, 5 };
// Nonlinear bounds
vector lbg = { 25, 40 };
vector ubg = { inf, 40 };
// NLP
SXDict nlp = { { "x", x }, { "f", f }, { "g", g } };
// Create NLP solver and buffers
Function solver = nlpsol("solver", "ipopt", nlp);
std::map arg, res;
// Solve the NLP
arg["lbx"] = lbx;
arg["ubx"] = ubx;
arg["lbg"] = lbg;
arg["ubg"] = ubg;
arg["x0"] = x0;
res = solver(arg);
// Print the solution
cout << "--------------------------------" << endl;
// std::cout << res << std::endl;
cout << "objective: " << res.at("f") << endl;
cout << "solution: " << res.at("x") << endl;
return 0;
}
VScode进入运行与调试界面进行调试即可
casadi_test
x:[x_0, x_1, x_2, x_3]
f:(((x_0*x_3)*((x_0+x_1)+x_2))+x_2)
g:[(((x_0*x_1)*x_2)*x_3), (((sq(x_0)+sq(x_1))+sq(x_2))+sq(x_3))]******************************************************************************
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 http://projects.coin-or.org/Ipopt
******************************************************************************This is Ipopt version 3.11.9, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).Number of nonzeros in equality constraint Jacobian...: 4
Number of nonzeros in inequality constraint Jacobian.: 4
Number of nonzeros in Lagrangian Hessian.............: 10Total number of variables............................: 4
variables with only lower bounds: 0
variables with lower and upper bounds: 4
variables with only upper bounds: 0
Total number of equality constraints.................: 1
Total number of inequality constraints...............: 1
inequality constraints with only lower bounds: 1
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 4.1009029e+00 3.59e+01 1.54e+00 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 6.3052937e+00 3.43e+01 2.33e+01 -1.0 5.89e+00 - 2.21e-03 4.20e-02h 1
2 5.3929003e+00 3.42e+01 7.45e+02 -1.0 3.89e+00 2.0 6.10e-06 3.54e-03F 1
3 6.6372159e+01 6.01e+00 2.27e+05 -1.0 3.78e+00 4.2 9.48e-04 4.80e-01h 2
4 9.6419121e+01 4.30e-01 5.82e+04 -1.0 7.23e+01 - 1.59e-02 1.00e+00h 1
5 9.4884972e+01 1.15e-03 3.12e+02 -1.0 1.47e+00 - 1.00e+00 1.00e+00h 1
6 8.7460064e+01 8.24e-02 1.95e+01 -1.0 6.98e-01 - 1.00e+00 1.00e+00f 1
7 8.9880566e+01 9.69e-03 3.14e+02 -1.0 7.37e-02 3.8 1.00e+00 1.00e+00h 1
8 8.9330308e+01 3.71e-04 1.71e+01 -1.0 2.19e-01 - 1.00e+00 1.00e+00f 1
9 8.9331900e+01 1.73e-06 4.10e+00 -1.0 2.16e-03 3.3 1.00e+00 1.00e+00h 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
10 8.0133699e+01 1.20e-01 1.86e+01 -1.0 2.30e+00 - 1.00e+00 1.00e+00f 1
11 8.2219265e+01 7.59e-03 3.27e+01 -1.0 6.41e-02 2.8 1.00e+00 1.00e+00h 1
12 7.5619844e+01 5.78e-02 1.78e+01 -1.0 2.89e+00 - 1.00e+00 1.00e+00f 1
13 7.6383207e+01 1.10e-03 4.23e+00 -1.0 2.53e-02 2.3 1.00e+00 1.00e+00h 1
14 5.6773756e+01 5.44e-01 1.90e+01 -1.0 1.15e+01 - 1.00e+00 1.00e+00f 1
15 6.0190720e+01 2.74e-02 5.29e+00 -1.0 1.24e-01 1.8 1.00e+00 1.00e+00h 1
16 2.8936531e+01 1.69e+00 2.27e+01 -1.0 3.26e+01 - 1.00e+00 1.00e+00f 1
17 3.1904315e+01 6.64e-02 1.11e+00 -1.0 1.93e-01 1.4 1.00e+00 1.00e+00h 1
18 2.0735088e+01 3.84e-01 1.76e+00 -1.0 9.22e+01 - 1.00e+00 1.99e-01f 1
19 1.7114152e+01 5.20e-01 8.02e+00 -1.0 4.71e+01 - 1.00e+00 1.30e-01f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
20 1.7278457e+01 3.55e-02 1.54e-02 -1.0 4.07e-01 - 1.00e+00 1.00e+00h 1
21 1.7045230e+01 8.77e-03 6.88e-03 -1.7 4.41e-01 - 1.00e+00 1.00e+00h 1
22 1.7017006e+01 2.40e-03 2.52e-03 -2.5 3.47e-02 - 1.00e+00 1.00e+00h 1
23 1.7014119e+01 1.90e-04 1.03e-04 -3.8 6.30e-03 - 1.00e+00 1.00e+00h 1
24 1.7014020e+01 2.57e-07 3.19e-07 -5.7 3.46e-04 - 1.00e+00 1.00e+00h 1
25 1.7014017e+01 1.96e-11 2.80e-11 -8.6 3.31e-06 - 1.00e+00 1.00e+00h 1Number of Iterations....: 25
(scaled) (unscaled)
Objective...............: 1.7014017145174137e+01 1.7014017145174137e+01
Dual infeasibility......: 2.7982007109802195e-11 2.7982007109802195e-11
Constraint violation....: 1.9625190361693967e-11 1.9625190361693967e-11
Complementarity.........: 2.5297906848701718e-09 2.5297906848701718e-09
Overall NLP error.......: 2.5297906848701718e-09 2.5297906848701718e-09
Number of objective function evaluations = 30
Number of objective gradient evaluations = 26
Number of equality constraint evaluations = 30
Number of inequality constraint evaluations = 30
Number of equality constraint Jacobian evaluations = 26
Number of inequality constraint Jacobian evaluations = 26
Number of Lagrangian Hessian evaluations = 25
Total CPU secs in IPOPT (w/o function evaluations) = 0.075
Total CPU secs in NLP function evaluations = 0.004EXIT: Optimal Solution Found.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 184.00us ( 6.13us) 176.33us ( 5.88us) 30
nlp_g | 429.00us ( 14.30us) 401.59us ( 13.39us) 30
nlp_grad_f | 219.00us ( 8.11us) 209.96us ( 7.78us) 27
nlp_hess_l | 203.00us ( 8.12us) 197.42us ( 7.90us) 25
nlp_jac_g | 231.00us ( 8.56us) 174.35us ( 6.46us) 27
total | 85.78ms ( 85.78ms) 228.30ms (228.30ms) 1
--------------------------------
objective: 17.014
solution: [1, 4.743, 3.82115, 1.37941]
[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-xsa1i3qr.udg" 1>"/tmp/Microsoft-MIEngine-Out-cxvrhrtn.1ak"