使用OSQP解决二次凸优化(QP)问题

2023-12-08 17:18

本文主要是介绍使用OSQP解决二次凸优化(QP)问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

什么是二次凸优化问题

可以转化成满足如下方程的优化问题B被称为二次凸优化(QP)问题。

min_x 0.5 * x'Px + q'x
s.t.  l <= Ax <= u

其中P是对称正定矩阵。所以目标函数的全局最小值就是其极小值。在二维的情况下,目标函数的图像类似下面的图。这样大概有一个印象就好。
在这里插入图片描述
约束类型可以是等式约束和不等式约束。
当需要设置等式约束时可以将需要相等的行设置为l[i] == u[i]
单侧的不等式约束,可以将最小或最大侧设置成无穷小或无穷大。

如何构造二次凸优化(QP)问题

这是一个比较大的问题。将很多实际的问题进行数学建模,然后转成凸优化问题。这样就能解了。这里仅说明一下这样的思路。

如何解二次凸优化(QP)问题

这里介绍如何使用OSQP库进行求解。
我已经将依赖的库合在一起了。可以在这里下载https://github.com/shoufei403/OSQP
使用osqp库和osqp-eigen库。osqp-eigen库是对osqp库的封装,其提供了更好用的eigen接口。

主要使用的接口如下:

    // set the initial data of the QP solver//矩阵A为m*n矩阵solver.data()->setNumberOfVariables(NumberOfVariables); //设置A矩阵的列数,即nsolver.data()->setNumberOfConstraints(NumberOfConstraints); //设置A矩阵的行数,即mif(!solver.data()->setHessianMatrix(hessian)) return 1;//设置P矩阵if(!solver.data()->setGradient(gradient)) return 1; //设置q or f矩阵。当没有时设置为全0向量if(!solver.data()->setLinearConstraintsMatrix(linearMatrix)) return 1;//设置线性约束的A矩阵if(!solver.data()->setLowerBound(lowerBound)) return 1;//设置下边界if(!solver.data()->setUpperBound(upperBound)) return 1;//设置上边界

下面的实例来源于https://ww2.mathworks.cn/help/optim/ug/quadprog.html?s_tid=srchtitle

  • 具有线性约束的二次规划
    在这里插入图片描述
    hessian.resize(2,2);hessian.insert(0,0) = 1;hessian.insert(1,0) = -1;hessian.insert(0,1) = -1;hessian.insert(1,1) = 2;std::cout << "hessian:" << std::endl << hessian << std::endl;gradient.resize(2);gradient << -2, -6;std::cout << "gradient:" << std::endl << gradient << std::endl;linearMatrix.resize(3,2);linearMatrix.insert(0,0) = 1;linearMatrix.insert(0,1) = 1;linearMatrix.insert(1,0) = -1;linearMatrix.insert(1,1) = 2;linearMatrix.insert(2,0) = 2;linearMatrix.insert(2,1) = 1;std::cout << "linearMatrix:" << std::endl << linearMatrix << std::endl;lowerBound.resize(3);lowerBound << -OsqpEigen::INFTY, -OsqpEigen::INFTY, -OsqpEigen::INFTY;std::cout << "lowerBound:" << std::endl << lowerBound << std::endl;upperBound.resize(3);upperBound << 2, 2, 3;std::cout << "upperBound:" << std::endl << upperBound << std::endl;int NumberOfVariables = 2; //A矩阵的列数int NumberOfConstraints = 3; //A矩阵的行数
  • 具有线性等式约束的二次规划
    在这里插入图片描述
    hessian.resize(2,2);hessian.insert(0,0) = 1;hessian.insert(1,0) = -1;hessian.insert(0,1) = -1;hessian.insert(1,1) = 2;std::cout << "hessian:" << std::endl << hessian << std::endl;gradient.resize(2);gradient << -2, -6;std::cout << "gradient:" << std::endl << gradient << std::endl;linearMatrix.resize(1,2);linearMatrix.insert(0,0) = 1;linearMatrix.insert(0,1) = 1;std::cout << "linearMatrix:" << std::endl << linearMatrix << std::endl;lowerBound.resize(1);lowerBound << 0;std::cout << "lowerBound:" << std::endl << lowerBound << std::endl;upperBound.resize(1);upperBound << 0;std::cout << "upperBound:" << std::endl << upperBound << std::endl;int NumberOfVariables = 2; //A矩阵的列数int NumberOfConstraints = 1; //A矩阵的行数
  • 具有线性约束和边界的二次最小化
    在这里插入图片描述
    该问题即包含了等式约束也包含了不等式约束。写成矩阵形式如下:
    [ 0 0 0 0.5 ] < = [ 1 0 0 0 1 0 0 0 1 1 1 1 ] [ x 1 x 2 x 3 ] < = [ 1 1 1 0.5 ] \begin{bmatrix} 0\\ 0\\ 0\\ 0.5 \end{bmatrix} <= \begin{bmatrix} 1& 0& 0\\ 0& 1& 0\\ 0& 0& 1\\ 1& 1& 1 \end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} <=\begin{bmatrix} 1\\ 1\\ 1\\ 0.5 \end{bmatrix} 0000.5<=100101010011x1x2x3<=1110.5
    hessian.resize(3,3);hessian.insert(0,0) = 1;hessian.insert(1,0) = -1;hessian.insert(2,0) = 1;hessian.insert(0,1) = -1;hessian.insert(1,1) = 2;hessian.insert(2,1) = -2;hessian.insert(0,2) = 1;hessian.insert(1,2) = -2;hessian.insert(2,2) = 4;std::cout << "hessian:" << std::endl << hessian << std::endl;gradient.resize(3);gradient << 2, -3, 1;std::cout << "gradient:" << std::endl << gradient << std::endl;linearMatrix.resize(4,3);linearMatrix.insert(0,0) = 1;linearMatrix.insert(1,0) = 0;linearMatrix.insert(2,0) = 0;linearMatrix.insert(3,0) = 1;linearMatrix.insert(0,1) = 0;linearMatrix.insert(1,1) = 1;linearMatrix.insert(2,1) = 0;linearMatrix.insert(3,1) = 1;linearMatrix.insert(0,2) = 0;linearMatrix.insert(1,2) = 0;linearMatrix.insert(2,2) = 1;linearMatrix.insert(3,2) = 1;std::cout << "linearMatrix:" << std::endl << linearMatrix << std::endl;lowerBound.resize(4);lowerBound << 0, 0, 0, 0.5;std::cout << "lowerBound:" << std::endl << lowerBound << std::endl;upperBound.resize(4);upperBound << 1, 1, 1, 0.5;std::cout << "upperBound:" << std::endl << upperBound << std::endl;int NumberOfVariables = 3; //A矩阵的列数int NumberOfConstraints = 4; //A矩阵的行数

遇到的问题

  1. 编译osqp-eigen库时报下面的错误:
CMake Error at cmake/OsqpEigenDependencies.cmake:12 (find_package):Could not find a configuration file for package "Eigen3" that is compatiblewith requested version "3.2.92".The following configuration files were considered but not accepted:/usr/lib/cmake/eigen3/Eigen3Config.cmake, version: unknownCall Stack (most recent call first):CMakeLists.txt:63 (include)

解决措施:需要将原来旧的eigen库删掉,重新按照最新的eigen库。

sudo rm -rf /usr/include/eigen3
sudo rm -rf /usr/lib/cmake/eigen3

重新安装eigen,注意要安装到原来的位置/usr/include,不然catkin_make会报错。

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr
sudo make install
  1. 编译osqp-eigen库时报target_compile_features specified unknown feature cxx_std_14 for target错误。
    解决措施:将cmake升级到3.14版本后可以正常编译。但是sudo apt remove cmake时,把很多ros的库也删掉了,导致roscore都运行不了。
    其实将CMakeList.txt文件更改一下就好了:
add_definitions(-std=c++14)  #添加这一行
#target_compile_features(${LIBRARY_TARGET_NAME} PUBLIC cxx_std_14)  #注释这一行
  1. 使用osqp-eigen库时出现这样的问题:
In file included from /usr/local/include/OsqpEigen/OsqpEigen.h:10:0,from /catkin_ws/src/MinimumSnap-Trajectory-Generation/waypoint_trajectory_generator/src/trajectory_generator_osqp.cpp:10:
/usr/local/include/OsqpEigen/Constants.hpp:12:18: fatal error: osqp.h: No such file or directory

这是因为头文件的包含路径有问题。按下图方式更改osqp-eigen库头文件,再重新编译安装。
在这里插入图片描述
Solver.tpp文件中对osqp库头文件的引用要改成下面的方式

#include <osqp/auxil.h>
#include <osqp/scaling.h>
  1. 运行程序链接osqp库时报错。error while loading shared libraries: libosqp.so: cannot open shared object file: No such file or directory
    解决措施:需要添加相应的链接地址
    ···
    sudo vim /etc/ld.so.conf
    ···
    在里面添加一行
/usr/local/lib

最后再执行一下下面的语句

sudo  /sbin/ldconfig
  1. 运行链接了osqp库和OsqpEigen的程序时,总是会报Segmentation fault (core dumped)
    发现是CMakeLists的写法不同导致的。
    原来自己的写法是:
set(ADDITIONAL_CXX_FLAG "-Wall -O3 -march=native") //事实上是这一条不能加
target_link_libraries(trajectory_generator_node_testqdldlosqpOsqpEigen
)

官方的例子是这样写的。

cmake_minimum_required(VERSION 3.1)set (CMAKE_CXX_STANDARD 11)project(OsqpEigen-Example)find_package(OsqpEigen)
find_package(Eigen3)include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})#MPCExample
add_executable(MPCExample src/MPCExample.cpp)
target_link_libraries(MPCExample OsqpEigen::OsqpEigen)#Simple Example
add_executable(SimpleExample src/simpleqp_example.cpp)
target_link_libraries(SimpleExample OsqpEigen::OsqpEigen)

参照官方的例子写就正常了。

关注公众号《首飞》回复“机器人”获取精心推荐的C/C++,Python,Docker,Qt,ROS1/2等机器人行业常用技术资料。

这篇关于使用OSQP解决二次凸优化(QP)问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/470686

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(