drake gurobi 无法处理二次约束
drake gurobi cannot handle quadratic constraint
我目前正在努力对优化问题实施简单的二次约束。 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,
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;
}
可能 https://github.com/RobotLocomotion/drake/issues/6341 上的(尚未实现的)功能请求是您要查找的内容?
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());
对于更一般的二次约束0.5xᵀQx + bᵀx + c<=0
,我们还提供了AddQuadraticAsRotatedLorentzConeConstraint,它施加了一个旋转的洛伦兹锥约束。
prog.AddQuadraticAsRotatedLorentzConeConstraint(Q, b, c, x);
对于二次成本
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 的论文 paper for more details. And Mosek also recommends SOCP over QCQP https://docs.mosek.com/9.2/rmosek/prob-def-quadratic.html#a-recommendation
我目前正在努力对优化问题实施简单的二次约束。 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,
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;
}
可能 https://github.com/RobotLocomotion/drake/issues/6341 上的(尚未实现的)功能请求是您要查找的内容?
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());
对于更一般的二次约束0.5xᵀQx + bᵀx + c<=0
,我们还提供了AddQuadraticAsRotatedLorentzConeConstraint,它施加了一个旋转的洛伦兹锥约束。
prog.AddQuadraticAsRotatedLorentzConeConstraint(Q, b, c, x);
对于二次成本
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 的论文 paper for more details. And Mosek also recommends SOCP over QCQP https://docs.mosek.com/9.2/rmosek/prob-def-quadratic.html#a-recommendation