问题描述
我目前正在努力对优化问题实施简单的二次约束。 Gurobi's website 表示可以实现二次约束。 drake 没有使用 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,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;
}
解决方法
德雷克目前(有意)不支持二次约束。为了获得更好的求解器性能,我建议重新制定没有二次约束但有二阶锥约束的优化问题。
首先是二次约束
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
在 Drake 中,代码是
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 的 paper 以了解更多详细信息。 Mosek 还推荐 SOCP 而不是 QCQP https://docs.mosek.com/9.2/rmosek/prob-def-quadratic.html#a-recommendation
,https://github.com/RobotLocomotion/drake/issues/6341 的(目前尚未实现的)功能请求可能是您要查找的内容?