【VS2022 配置 ACADOS环境】
【VS2022 配置 ACADOS求解 NLP问题】
- 1. 前言
- 1.1 来回扁椭圆
- 1.2 椭圆轨迹
- 1.3 月牙形轨迹
- 2. Cmake配置
- 3. 下载acados的源码
- 4. 组合编译Acados
- 5. 测试ACADOS
- 5.1 配置getting_started.c
- 5.2 启动getting_started.c
- 5.3 附加依赖项
- 6 总结
1. 前言
最近在研究 船舶NMPC 控制时,主要是非线性约束的凸二次优化问题属于NLP问题,找到了 casadi 这个求解库,后面读了许多论文,发现Acados求解速度比Casadi还要快许多,所以就来试试呗!但是团队现在软件是基于Visual Studio From架构设计的测试软件,之后再系统的移植过去。
https://github.com/acados/acados
https://docs.acados.org/
仔细阅读安装教程发现支持msvc,只是需要安装Cmake配置环境
- 开发工具:Visual Studio 2022(.NET Framework 4.7.2+)环境搭建参考博客:【VS2019安装+QT配置】
- 配置工具:Cmake 4.0.2
给大家看看PID控制小船跑的几种常见轨迹,后续会更新其他控制效果:
源码地址:
1.1 来回扁椭圆
1.2 椭圆轨迹
1.3 月牙形轨迹
2. Cmake配置
只是需要安装Cmake配置环境
https://cmake.org/download/
一路直接安装注意需要勾选自动配置环境变量
3. 下载acados的源码
在 acados的 github 官方仓库中找到适合的版本,下载对应的 Source Code 压缩包即可
或者git
git clone https://github.com/acados/acados.git
cd acados
git submodule update --recursive --init
4. 组合编译Acados
使用 Microsoft Visual C 编译器 (MSVC) 的工作流
注意:此工作流程是初步的,未经全面测试。 (于 2025 年 6 月使用 MSVC 2022 测试一次)
然后在 Acados 的目录下新建一个 build
下,使用 、导航到<acados_root_folder>/build
并运行Developer Command Prompt for VS
cmake -G "Visual Studio 17 2022 " -DBLASFEO_TARGET=GENERIC -DACADOS_INSTALL_DIR=.. -DBUILD_SHARED_LIBS=OFF ..
# respectively for MVSC 2019
# cmake -G "Visual Studio 16 2019" -DBLASFEO_TARGET=GENERIC -DACADOS_INSTALL_DIR=.. -DBUILD_SHARED_LIBS=OFF ..
cmake --build . -j10 --target INSTALL --config Release
cmake --install build
后面demo只支持Release x64版本,进行二次封装可以解除版本限制就可以支持Debug x64版本
如果编译报错
D:\code\Cplusplus\acados\build>cmake -G "Visual Studio 17 2022" -DBLASFEO_TARGET=GENERIC -DACADOS_INSTALL_DIR=.. -DBUILD_SHARED_LIBS=OFF ..
CMake Deprecation Warning at CMakeLists.txt:32 (cmake_minimum_required):Compatibility with CMake < 3.10 will be removed from a future version ofCMake.Update the VERSION argument <min> value. Or, use the <min>...<max> syntaxto tell CMake that the project requires at least <min> but has been updatedto work with policies introduced by <max> or earlier.-- The C compiler identification is MSVC 19.40.33812.0
-- The CXX compiler identification is MSVC 19.40.33812.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: E:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: E:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Build type is Release
-- ACADOS_WITH_OPENMP: OFF
CMake Error at external/blasfeo/CMakeLists.txt:36 (cmake_minimum_required):Compatibility with CMake < 3.5 has been removed from CMake.Update the VERSION argument <min> value. Or, use the <min>...<max> syntaxto tell CMake that the project requires at least <min> but has been updatedto work with policies introduced by <max> or earlier.Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.-- Configuring incomplete, errors occurred!
解决方法:
根据cmake的提示 CMakeLists.txt 中 cmake_minimum_required (VERSION 2.8.12)的版本要求太低了,可能无法兼容,将最小版本要求更新或者添加一个区间即可。
修改报错文件中的版本声明后解决:
//修改前
cmake_minimum_required (VERSION 2.8.12)//查看系统cmake版本, cmake --version
cmake version 4.0.2//修改后
cmake_minimum_required (VERSION 3.8.0)
或者
cmake_minimum_required (VERSION 2.8.12...4.0.2)
之后双击运行,等待其编译结束,中间会出现许多警告
编译成功后会生成Visual studio的项目文件
5. 测试ACADOS
打开上面编译好的项目文件acados.sln
这里面有许多测试demo,大家有兴趣自行测试
5.1 配置getting_started.c
我们将getting_started项目右键设置为启动项目
/**************************************************************************************************
* *
* This file is part of BLASFEO. *
* *
* BLASFEO -- BLAS For Embedded Optimization. *
* Copyright (C) 2019 by Gianluca Frison. *
* Developed at IMTEK (University of Freiburg) under the supervision of Moritz Diehl. *
* All rights reserved. *
* *
* The 2-Clause BSD License *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice, this *
* list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright notice, *
* this list of conditions and the following disclaimer in the documentation *
* and/or other materials provided with the distribution. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND *
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
* Author: Gianluca Frison, gianluca.frison (at) imtek.uni-freiburg.de *
* *
**************************************************************************************************/#include <stdlib.h>
#include <stdio.h>#include <blasfeo.h>int main(){printf( "Testing processor\n" );char supportString[50];blasfeo_processor_library_string( supportString );printf( "Library requires processor features:%s\n", supportString );int features = 0;int procCheckSucceed = blasfeo_processor_cpu_features( &features );blasfeo_processor_feature_string( features, supportString );printf( "Processor supports features:%s\n", supportString );if( !procCheckSucceed ){printf("Current processor does not support the current compiled BLASFEO library.\n");printf("Please get a BLASFEO library compatible with this processor.\n");exit(3);}int ii; // loop indexint n = 12; // matrix size// Astruct blasfeo_dmat sA; // matrix structureblasfeo_allocate_dmat(n, n, &sA); // allocate and assign memory needed by A// Bstruct blasfeo_dmat sB; // matrix structureint B_size = blasfeo_memsize_dmat(n, n); // size of memory needed by Bvoid *B_mem_align;v_zeros_align(&B_mem_align, B_size); // allocate memory needed by Bblasfeo_create_dmat(n, n, &sB, B_mem_align); // assign aligned memory to struct// Cstruct blasfeo_dmat sC; // matrix structureint C_size = blasfeo_memsize_dmat(n, n); // size of memory needed by CC_size += 64; // 64-bytes alignmentvoid *C_mem = malloc(C_size);//void *C_mem_align = (void *) ((((unsigned long long) C_mem)+63)/64*64); // align memory pointervoid *C_mem_align;blasfeo_align_64_byte(C_mem, &C_mem_align); // align memory pointerblasfeo_create_dmat(n, n, &sC, C_mem_align); // assign aligned memory to struct// Adouble *A = malloc(n*n*sizeof(double));for(ii=0; ii<n*n; ii++)A[ii] = ii;int lda = n;blasfeo_pack_dmat(n, n, A, lda, &sA, 0, 0); // convert from column-major to BLASFEO dmatfree(A);// Bblasfeo_dgese(n, n, 0.0, &sB, 0, 0); // set B to zerofor(ii=0; ii<n; ii++)BLASFEO_DMATEL(&sB, ii, ii) = 1.0; // set B diagonal to 1.0 accessing dmat elements// Cblasfeo_dgese(n, n, -1.0, &sC, 0, 0); // set C to -1.0blasfeo_dgemm_nt(n, n, n, 1.0, &sA, 0, 0, &sB, 0, 0, 0.0, &sC, 0, 0, &sC, 0, 0);printf("\nC = \n");blasfeo_print_dmat(n, n, &sC, 0, 0);blasfeo_free_dmat(&sA);v_free_align(B_mem_align);free(C_mem);return 0;}
5.2 启动getting_started.c
点击调试运行getting_started.c
demo
会输出如下内容
Testing processor
Library requires processor features:
Processor supports features:C =0.00000 12.00000 24.00000 36.00000 48.00000 60.00000 72.00000 84.00000 96.00000 108.00000 120.00000 132.000001.00000 13.00000 25.00000 37.00000 49.00000 61.00000 73.00000 85.00000 97.00000 109.00000 121.00000 133.000002.00000 14.00000 26.00000 38.00000 50.00000 62.00000 74.00000 86.00000 98.00000 110.00000 122.00000 134.000003.00000 15.00000 27.00000 39.00000 51.00000 63.00000 75.00000 87.00000 99.00000 111.00000 123.00000 135.000004.00000 16.00000 28.00000 40.00000 52.00000 64.00000 76.00000 88.00000 100.00000 112.00000 124.00000 136.000005.00000 17.00000 29.00000 41.00000 53.00000 65.00000 77.00000 89.00000 101.00000 113.00000 125.00000 137.000006.00000 18.00000 30.00000 42.00000 54.00000 66.00000 78.00000 90.00000 102.00000 114.00000 126.00000 138.000007.00000 19.00000 31.00000 43.00000 55.00000 67.00000 79.00000 91.00000 103.00000 115.00000 127.00000 139.000008.00000 20.00000 32.00000 44.00000 56.00000 68.00000 80.00000 92.00000 104.00000 116.00000 128.00000 140.000009.00000 21.00000 33.00000 45.00000 57.00000 69.00000 81.00000 93.00000 105.00000 117.00000 129.00000 141.0000010.00000 22.00000 34.00000 46.00000 58.00000 70.00000 82.00000 94.00000 106.00000 118.00000 130.00000 142.0000011.00000 23.00000 35.00000 47.00000 59.00000 71.00000 83.00000 95.00000 107.00000 119.00000 131.00000 143.00000D:\code\Cplusplus\acados\build\external\blasfeo\examples\Debug\getting_started.exe (进程 20572)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
到此为止我们就完成VS2022 配置 ACADOS环境了
5.3 附加依赖项
如果你想生成静态库进行二次调用,需要将INSTALL
设置为启动项目
系统就会生成acados静态库方便你二次调用
6 总结
恭喜你,学习了VS2022 配置 ACADOS环境,从而实现对外部世界进行感知,充分认识这个有机与无机的环境,科学地合理地进行创作和发挥效益,然后为人类社会发展贡献一点微薄之力。🤣🤣🤣
- 我会持续更新对应专栏博客,非常期待你的三连!!!🎉🎉🎉
- 如果鹏鹏有哪里说的不妥,还请大佬多多评论指教!!!👍👍👍
- 下面有我的🐧🐧🐧群推广,欢迎志同道合的朋友们加入,期待与你的思维碰撞😘😘😘
参考文献:VS2022 配置 ACADOS环境