drake - drake gurobi 无法处理二次约束
问题描述
我目前正在努力对优化问题实施一个简单的二次约束。Gurobi 的网站说可以实现二次约束。德雷克没有使用 Gurobi 的这个约束的接口吗?
代码如下。
#include <Eigen/Dense>
#include <math.h>
#include <iostream>
#include "drake/solvers/mathematical_program.h"
#include "drake/solvers/solve.h"
#include "drake/solvers/gurobi_solver.h"
#include "drake/solvers/scs_solver.h"
using namespace std;
using namespace Eigen;
using namespace drake;
int main(){
solvers::MathematicalProgram prog;
// Instantiate the decision variables
solvers::VectorXDecisionVariable x = prog.NewContinuousVariables(2, "x");
// Define constraints
for(int i = 0; i < 2; i++){
prog.AddConstraint(x[i]*x[i] <= 2); //Replacing this with a linear constraint
//such as prog.AddLinearConstraint(-5 <= x[i] && x[i] <= 5);
//will work
}
// Define the cost
MatrixXd Q(2,2);
Q << 2, 1,
1, 4;
VectorXd b(2);
b << 0, 3;
double c = 1.0;
prog.AddQuadraticCost(Q, b, c, x);
solvers::GurobiSolver solver;
cout << "Gurobi available? " << solver.is_enabled() << endl;
auto result = solver.Solve(prog);
cout << "Is optimization successful?" << result.is_success() << endl;
cout << "Optimal x: " << result.GetSolution().transpose() << endl;
cout << "solver is: " << result.get_solver_id().name() << endl;
cout << "computation time is: " << result.get_solver_details<solvers::GurobiSolver>().optimizer_time;
return 0;
}
解决方案
Drake 当前(有意)不支持二次约束。为了获得更好的求解器性能,我建议重新制定没有二次约束但具有二阶锥约束的优化问题。
首先对于二次约束
xᵀx <= a²
这可以看作是一个洛伦兹锥约束
(y, x) is in the Lorentz cone
y = a
在 Drake 中,您可以将此约束添加为
prog.AddLorentzConeConstraint((drake::VectorX<drake::symbolic::Expression>(x.rows() + 1) << a, x).finished());
对于二次成本
min 0.5xᵀQx + bᵀx + c
您还可以将其重新表述为具有旋转洛伦兹锥约束的线性成本
min z
z >= 0.5xᵀQx + bᵀx + c
在德雷克,代码是
z = prog.NewContinuousVariables<1>()(0);
prog.AddLinearCost(z);
prog.AddRotatedLorentzConeConstraint(symbolic::Expression(z), symbolic::Expression(1), 0.5 * x.dot(Q * x) + b.dot(x) + c);
我们更喜欢洛伦兹锥约束而不是二次约束二次规划 (QCQP) 的原因是,使用洛伦兹锥约束,您最终会遇到二阶锥优化问题 (SOCP),这个优化问题有就其双重形式而言,有很多不错的功能。您可以阅读 Alizadeh 的论文以了解更多详细信息。Mosek 还推荐 SOCP 而不是 QCQP https://docs.mosek.com/9.2/rmosek/prob-def-quadratic.html#a-recommendation
推荐阅读
- excel - 距离矩阵 API 中的 UTF-8 处理
- sql-server - 在 Excel 中打开连接时出现 SQL Server 错误
- python - 优化性能 - 写出质数
- xamarin - Xamarin froms Forms 水平列表视图,带有将日期向右、向左移动的按钮
- arrays - 如何根据参数对对象列表进行分类
- github - 如何更改 GitHub 拉取请求中使用的作者/用户名?
- wix - MSI 失败时,WiX 引导程序显示错误
- java - 插入场景中按版本的 JPA/Hibernate 乐观锁
- laravel - Laravel,显示 dd() 结果而不用 die -> d(),dump()?
- angular - 错误 TS2339:“订阅”类型上不存在属性“订阅”