casadi源码安装过程及初步使用(ubuntu,c++)

文章介绍了Casadi这个用于数值计算的开源库,包括它的功能和接口。接着详细阐述了在Linux环境下源码安装Casadi的步骤,以及如何在VScode中配置CMakeLists文件和main.cpp文件来实现代码调试。最后展示了运行结果,包括IPopt求解器的输出信息。
摘要由CSDN通过智能技术生成

目录

一、casadi介绍

二、源码安装步骤

三、VScode使用

1.CMakeLists文件

2. main.cpp 文件 

3. 运行结果:


一、casadi介绍

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

三、VScode使用

1.CMakeLists文件

# 声明要求的 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")

2. main.cpp 文件 

代码参考:非线性求解器 Casadi (c++使用例子)_casadi c++_梦醒时分1218的博客-CSDN博客

#include<iostream>
#include<vector>
#include <casadi/casadi.hpp>

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<double> x0 = { 0.0, 0.0, 0.0, 0.0 };
  vector<double> lbx = { 1, 1, 1, 1 };
  vector<double> ubx = {5, 5, 5, 5 };

  // Nonlinear bounds
  vector<double> lbg = { 25, 40 };
  vector<double> 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<std::string, DM> 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进入运行与调试界面进行调试即可

3. 运行结果:

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.............:       10

Total 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:        0

iter    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  1

Number 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.004

EXIT: 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"

  • 4
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在C程序中调用源码安装的库,在Ubuntu中通常需要进行以下步骤: 第一步,安装依赖库:源码安装的库通常依赖于其他的库文件,因此在调用之前,需要确保已经安装了这些依赖库。可以使用apt-get命令来安装缺失的依赖库。 第二步,添加头文件路径:源码安装通常会将库的头文件放置在指定的目录下,而程序需要通过包含头文件来使用库的函数和数据结构。在C程序中,可以通过使用#include指令来包含头文件。因此,在编写C程序时,需要在开头添加如下指令:#include <library_header.h>,其中library_header.h是库的头文件名。 第三步,链接库文件:源码安装会生成库文件,通常是以.so(动态链接库)或.a(静态链接库)的形式存在。而C程序在编译时需要将库文件链接到生成的可执行文件中,以便程序可以调用其中的函数和数据结构。在gcc编译器中,可以通过使用-l参数来指定需要链接的库。例如,如果库文件名为library.so,则编译命令可以写为:gcc -o program program.c -llibrary。 第四步,编写调用代码:在C程序中,可以通过调用库中的函数和使用库中的数据结构来使用源码安装的库。具体的调用方式和使用方法需要根据库的文档来确定,通常会提供相应的示例代码。 因此,使用源码安装的库需要先安装依赖库,然后包含头文件,链接库文件,并在C程序中编写相应的调用代码。以上就是在Ubuntu中在C程序中调用源码安装库的一般步骤。 ### 回答2: 在C程序中调用通过源码安装Ubuntu库需要进行以下步骤: 1. 在编写C程序时,需要包含相应的头文件。这些头文件通常位于库文件安装目录的include文件夹中。你可以使用命令"sudo find / -name <header_file.h>"来查找头文件的位置。 2. 链接库文件。通过源码安装库时,编译器不会自动找到并链接库文件。你需要将库文件的路径添加到链接器的参数中。在编译C程序时,你可以使用命令"gcc -o output_file source_file.c -I <library_include_path> -L <library_path> -l <library_name>",其中"<library_include_path>"是库的头文件路径,"<library_path>"是库文件路径,"<library_name>"是库名。 3. 调用库函数。通过上述步骤,你应该可以在C程序中使用库函数了。你可以通过引用库的头文件来调用库的函数,头文件中定义了函数的接口。 总结起来,要在C程序中调用通过源码安装Ubuntu库,你需要包含库头文件、链接库文件,并调用库函数。 ### 回答3: 在C程序中调用通过Ubuntu源码安装的库,需要进行以下步骤: 1. 确认库的安装路径:使用"dpkg -L"命令查看库安装的路径。例如,如果安装了libxxx库,可以使用命令"dpkg -L libxxx"查看其安装路径。 2. 在C程序中包含头文件:在C程序的开头,使用#include指令包含库所对应的头文件。例如,如果安装了libxxx库,需要在C程序中添加#include <xxx.h>。 3. 链接库:在编译C程序时,需要将库链接到程序中。使用"-l"选项指定需要链接的库的名称。例如,如果安装了libxxx库,需要在编译命令中添加"-lxxx"。如果库的安装路径不在系统默认路径中,还需要使用"-L"选项指定库的路径。 综上所述,使用Ubuntu源码安装的库在C程序中调用的步骤主要包括确认库的安装路径、包含头文件和链接库。通过这些步骤,可以在C程序中成功调用使用Ubuntu源码安装的库。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值