Search code examples
c++roboticsdrake

How to access the Optimization Solution formulated using Drake Toolbox


A c++ novice here! The verbose in the terminal output says the problem is solved successfully, but I am not able to access the solution. What is the problem with the last line?

drake::solvers::MathematicalProgram prog;
auto x = prog.NewContinuousVariables(n_x);

// Cost and Constraints

drake::solvers::MathematicalProgramResult result;
drake::solvers::OsqpSolver osqp_solver;
if (osqp_solver.available()) {
    // Setting solver options.
    for (int print_to_console : {0, 0}) {           //{0,1} for verbose, {0,0} for no terminal output
        drake::solvers::SolverOptions options;
        options.SetOption(drake::solvers::OsqpSolver::id(), "verbose", print_to_console);
        osqp_solver.Solve(prog, {}, options);
    }
}
const auto u = result.GetSolution(x);

Another question is that what if I don't wanna choose OSQP and let Drake decide which solver to use for the QP, how can I do this?


Solution

  • You will need to change the line

    osqp_solver.Solve(prog, {}, options);
    

    to

    result = osqp_solver.Solve(prog, {}, options);
    

    currently result is unset.

    Another question is that what if I don't wanna choose OSQP and let Drake decide which solver to use for the QP, how can I do this?

    You can do

    const drake::solvers::MathematicalProgramResult result = drake::solvers::Solve(prog, {}, options);
    

    Drake will then choose the solver automatically. For more information, please refer to the tutorial in https://github.com/RobotLocomotion/drake/blob/master/tutorials/mathematical_program.ipynb. It talks about choosing the solver automatically vs manually.