The result returned by MathematicalProgram::Solve().
It stores the solvers::SolutionResult (whether the program is solved to optimality, detected infeasibility, etc), the optimal value for the decision variables, the optimal cost, and solver specific details.
#include <drake/solvers/mathematical_program_result.h>
Public Member Functions | |
MathematicalProgramResult () | |
Constructs the result. More... | |
bool | is_success () const |
Returns true if the optimization problem is solved successfully; false otherwise. More... | |
void | set_decision_variable_index (std::unordered_map< symbolic::Variable::Id, int > decision_variable_index) |
Sets decision_variable_index mapping, that maps each decision variable to its index in the aggregated vector containing all decision variables in MathematicalProgram. More... | |
const std::optional< std::unordered_map< symbolic::Variable::Id, int > > & | get_decision_variable_index () const |
Gets decision_variable_index. More... | |
void | set_solution_result (SolutionResult solution_result) |
Sets SolutionResult. More... | |
const Eigen::VectorXd & | get_x_val () const |
Gets the decision variable values. More... | |
SolutionResult | get_solution_result () const |
Gets SolutionResult. More... | |
void | set_x_val (const Eigen::VectorXd &x_val) |
Sets the decision variable values. More... | |
template<typename C > | |
void | set_dual_solution (const Binding< C > &constraint, const Eigen::Ref< const Eigen::VectorXd > &dual_solution) |
Sets the dual solution associated with a given constraint. More... | |
double | get_optimal_cost () const |
Gets the optimal cost. More... | |
void | set_optimal_cost (double optimal_cost) |
Sets the optimal cost. More... | |
const SolverId & | get_solver_id () const |
Gets the solver ID. More... | |
void | set_solver_id (const SolverId &solver_id) |
Sets the solver ID. More... | |
template<typename Solver > | |
const Solver::Details & | get_solver_details () const |
Gets the solver details for the Solver that solved the program. More... | |
const AbstractValue & | get_abstract_solver_details () const |
(Advanced.) Gets the type-erased solver details. More... | |
template<typename T > | |
T & | SetSolverDetailsType () |
(Advanced.) Forces the solver_details to be stored using the given type T . More... | |
const Eigen::VectorXd & | GetSolution () const |
Gets the solution of all decision variables. More... | |
template<typename Derived > | |
std::enable_if_t< std::is_same_v< typename Derived::Scalar, symbolic::Variable >, MatrixLikewise< double, Derived > > | GetSolution (const Eigen::MatrixBase< Derived > &var) const |
Gets the solution of an Eigen matrix of decision variables. More... | |
double | GetSolution (const symbolic::Variable &var) const |
Gets the solution of a single decision variable. More... | |
void | SetSolution (const symbolic::Variable &var, double value) |
Resets the solution of a single decision variable that is already registered with this result. More... | |
symbolic::Expression | GetSolution (const symbolic::Expression &e) const |
Substitutes the value of all decision variables into the Expression. More... | |
symbolic::Polynomial | GetSolution (const symbolic::Polynomial &p) const |
Substitutes the value of all decision variables into the coefficients of the symbolic polynomial. More... | |
template<typename Derived > | |
std::enable_if_t< std::is_same_v< typename Derived::Scalar, symbolic::Expression >, MatrixLikewise< symbolic::Expression, Derived > > | GetSolution (const Eigen::MatrixBase< Derived > &m) const |
Substitutes the value of all decision variables into the Matrix<Expression>. More... | |
template<typename C > | |
Eigen::VectorXd | GetDualSolution (const Binding< C > &constraint) const |
Gets the dual solution associated with a constraint. More... | |
template<typename Evaluator > | |
Eigen::VectorXd | EvalBinding (const Binding< Evaluator > &binding) const |
Evaluate a Binding at the solution. More... | |
Implements CopyConstructible, CopyAssignable, MoveConstructible, MoveAssignable | |
MathematicalProgramResult (const MathematicalProgramResult &)=default | |
MathematicalProgramResult & | operator= (const MathematicalProgramResult &)=default |
MathematicalProgramResult (MathematicalProgramResult &&)=default | |
MathematicalProgramResult & | operator= (MathematicalProgramResult &&)=default |
Solution Pools | |
template<typename Derived > | |
std::enable_if_t< std::is_same_v< typename Derived::Scalar, symbolic::Variable >, MatrixLikewise< double, Derived > > | GetSuboptimalSolution (const Eigen::MatrixBase< Derived > &var, int solution_number) const |
Gets the suboptimal solution corresponding to a matrix of decision variables. More... | |
double | GetSuboptimalSolution (const symbolic::Variable &var, int solution_number) const |
Gets the suboptimal solution of a decision variable. More... | |
int | num_suboptimal_solution () const |
Number of suboptimal solutions stored inside MathematicalProgramResult. More... | |
double | get_suboptimal_objective (int solution_number) const |
Gets the suboptimal objective value. More... | |
void | AddSuboptimalSolution (double suboptimal_objective, const Eigen::VectorXd &suboptimal_x) |
Adds the suboptimal solution to the result. More... | |
Get infeasible constraints | |
Some solvers (e.g. SNOPT) provide a "best-effort solution" even when they determine that a problem is infeasible. This method will return the descriptions corresponding to the constraints for which | |
std::vector< std::string > | GetInfeasibleConstraintNames (const MathematicalProgram &prog, std::optional< double > tolerance=std::nullopt) const |
See get_infeasible_constraints for more information. More... | |
std::vector< Binding< Constraint > > | GetInfeasibleConstraints (const MathematicalProgram &prog, std::optional< double > tolerance=std::nullopt) const |
See get_infeasible_constraints for more information. More... | |
|
default |
|
default |
Constructs the result.
void AddSuboptimalSolution | ( | double | suboptimal_objective, |
const Eigen::VectorXd & | suboptimal_x | ||
) |
Adds the suboptimal solution to the result.
See solution pools.
suboptimal_objective | The objective value computed from this suboptimal solution. |
suboptimal_x | The values of the decision variables in this suboptimal solution. |
Eigen::VectorXd EvalBinding | ( | const Binding< Evaluator > & | binding | ) | const |
Evaluate a Binding at the solution.
binding | A binding between a constraint/cost and the variables. |
const AbstractValue& get_abstract_solver_details | ( | ) | const |
(Advanced.) Gets the type-erased solver details.
Most users should use get_solver_details() instead. Throws an error if the solver_details has not been set.
const std::optional<std::unordered_map<symbolic::Variable::Id, int> >& get_decision_variable_index | ( | ) | const |
Gets decision_variable_index.
double get_optimal_cost | ( | ) | const |
Gets the optimal cost.
SolutionResult get_solution_result | ( | ) | const |
Gets SolutionResult.
const Solver::Details& get_solver_details | ( | ) | const |
Gets the solver details for the Solver
that solved the program.
Throws an error if the solver_details has not been set.
const SolverId& get_solver_id | ( | ) | const |
Gets the solver ID.
Gets the suboptimal objective value.
See solutionpools".
solution_number | The index of the sub-optimal solution. |
solution_number
should be in the range [0, num_suboptimal_solution()). const Eigen::VectorXd& get_x_val | ( | ) | const |
Gets the decision variable values.
Eigen::VectorXd GetDualSolution | ( | const Binding< C > & | constraint | ) | const |
Gets the dual solution associated with a constraint.
For constraints in the form lower <= f(x) <= upper (including linear inequality, linear equality, bounding box constraints, and general nonlinear constraints), we interpret the dual variable value as the "shadow price" of the original problem. Namely if we change the constraint bound by one unit (each unit is infinitesimally small), the change of the optimal cost is the value of the dual solution times the unit. Mathematically dual_solution = ∂optimal_cost / ∂bound.
For a linear equality constraint Ax = b where b ∈ ℝⁿ, the vector of dual variables has n rows, and dual_solution(i) is the value of the dual variable for the constraint A(i,:)*x = b(i).
For a linear inequality constraint lower <= A*x <= upper where lower and upper ∈ ℝⁿ, dual_solution also has n rows. dual_solution(i) is the value of the dual variable for constraint lower(i) <= A(i,:)*x <= upper(i). If neither side of the constraint is active, then dual_solution(i) is 0. If the left hand-side lower(i) <= A(i, :)*x is active (meaning lower(i) = A(i, :)*x at the solution), then dual_solution(i) is non-negative (because the objective is to minimize a cost, increasing the lower bound means the constraint set is tighter, hence the optimal solution cannot decrease. Thus the shadow price is non-negative). If the right hand-side A(i, :)*x<=upper(i) is active (meaning A(i,:)*x=upper(i) at the solution), then dual_solution(i) is non-positive.
For a bounding box constraint lower <= x <= upper, the interpretation of the dual solution is the same as the linear inequality constraint.
For a Lorentz cone or rotated Lorentz cone constraint that Ax + b is in the cone, depending on the solver, the dual solution has different meanings:
The interpretation for the dual variable to conic constraint x ∈ K can be different. Here K is a convex cone, including exponential cone, power cone, psd cone, etc. When the problem is solved by a convex solver (like SCS, MOSEK™, CSDP, etc), often it has a dual variable z ∈ K*, where K* is the dual cone. Here the dual variable DOESN'T have the interpretation of "shadow price", but should satisfy the KKT condition, while the dual variable stays inside the dual cone.
When K is a psd cone, the returned dual solution is the lower triangle of the dual symmetric psd matrix. Namely for the primal problem
min trace(C*X) s.t A(X) = b X is psd
the dual is
max b'*y s.t A'(y) - C = Z Z is psd.
We return the lower triangular part of Z. You can call drake::math::ToSymmetricMatrixFromLowerTriangularColumns to get the matrix Z.
std::vector<std::string> GetInfeasibleConstraintNames | ( | const MathematicalProgram & | prog, |
std::optional< double > | tolerance = std::nullopt |
||
) | const |
See get_infeasible_constraints for more information.
prog | The MathematicalProgram that was solved to obtain this MathematicalProgramResult. |
tolerance | A positive tolerance to check the constraint violation. If no tolerance is provided, this method will attempt to obtain the constraint tolerance from the solver, or insert a conservative default tolerance. |
Note: Currently most constraints have the empty string as the description, so the NiceTypeName of the Constraint is used instead. Use e.g. prog.AddConstraint(x == 1).evaluator().set_description(str)
to make this method more specific/useful.
std::vector<Binding<Constraint> > GetInfeasibleConstraints | ( | const MathematicalProgram & | prog, |
std::optional< double > | tolerance = std::nullopt |
||
) | const |
See get_infeasible_constraints for more information.
prog | The MathematicalProgram that was solved to obtain this MathematicalProgramResult. |
tolerance | A positive tolerance to check the constraint violation. If no tolerance is provided, this method will attempt to obtain the constraint tolerance from the solver, or insert a conservative default tolerance. |
const Eigen::VectorXd& GetSolution | ( | ) | const |
Gets the solution of all decision variables.
std::enable_if_t< std::is_same_v<typename Derived::Scalar, symbolic::Variable>, MatrixLikewise<double, Derived> > GetSolution | ( | const Eigen::MatrixBase< Derived > & | var | ) | const |
Gets the solution of an Eigen matrix of decision variables.
Derived | An Eigen matrix containing Variable. |
var | The decision variables. |
double GetSolution | ( | const symbolic::Variable & | var | ) | const |
Gets the solution of a single decision variable.
var | The decision variable. |
std::exception | if var is not captured in the mapping decision_variable_index , as the input argument of set_decision_variable_index(). |
symbolic::Expression GetSolution | ( | const symbolic::Expression & | e | ) | const |
Substitutes the value of all decision variables into the Expression.
e | The decision variable. |
symbolic::Polynomial GetSolution | ( | const symbolic::Polynomial & | p | ) | const |
Substitutes the value of all decision variables into the coefficients of the symbolic polynomial.
p | A symbolic polynomial. Its indeterminates can't intersect with the set of decision variables of the MathematicalProgram from which this result is obtained. |
std::enable_if_t< std::is_same_v<typename Derived::Scalar, symbolic::Expression>, MatrixLikewise<symbolic::Expression, Derived> > GetSolution | ( | const Eigen::MatrixBase< Derived > & | m | ) | const |
Substitutes the value of all decision variables into the Matrix<Expression>.
Derived | An Eigen matrix containing Expression. |
std::enable_if_t< std::is_same_v<typename Derived::Scalar, symbolic::Variable>, MatrixLikewise<double, Derived> > GetSuboptimalSolution | ( | const Eigen::MatrixBase< Derived > & | var, |
int | solution_number | ||
) | const |
Gets the suboptimal solution corresponding to a matrix of decision variables.
See solution pools
var | The decision variables. |
solution_number | The index of the sub-optimal solution. |
solution_number
should be in the range [0, num_suboptimal_solution()). double GetSuboptimalSolution | ( | const symbolic::Variable & | var, |
int | solution_number | ||
) | const |
Gets the suboptimal solution of a decision variable.
See solution pools
var | The decision variable. |
solution_number | The index of the sub-optimal solution. |
solution_number
should be in the range [0, num_suboptimal_solution()). bool is_success | ( | ) | const |
Returns true if the optimization problem is solved successfully; false otherwise.
For more information on the solution status, the user could call get_solver_details() to obtain the solver-specific solution status.
int num_suboptimal_solution | ( | ) | const |
Number of suboptimal solutions stored inside MathematicalProgramResult.
See solution pools.
|
default |
|
default |
void set_decision_variable_index | ( | std::unordered_map< symbolic::Variable::Id, int > | decision_variable_index | ) |
Sets decision_variable_index mapping, that maps each decision variable to its index in the aggregated vector containing all decision variables in MathematicalProgram.
Initialize x_val to NAN.
void set_dual_solution | ( | const Binding< C > & | constraint, |
const Eigen::Ref< const Eigen::VectorXd > & | dual_solution | ||
) |
Sets the dual solution associated with a given constraint.
void set_optimal_cost | ( | double | optimal_cost | ) |
Sets the optimal cost.
void set_solution_result | ( | SolutionResult | solution_result | ) |
Sets SolutionResult.
void set_solver_id | ( | const SolverId & | solver_id | ) |
Sets the solver ID.
void set_x_val | ( | const Eigen::VectorXd & | x_val | ) |
Sets the decision variable values.
void SetSolution | ( | const symbolic::Variable & | var, |
double | value | ||
) |
Resets the solution of a single decision variable that is already registered with this result.
std::exception | if var is not captured in the mapping decision_variable_index , as the input argument of set_decision_variable_index(). |
T& SetSolverDetailsType | ( | ) |
(Advanced.) Forces the solver_details to be stored using the given type T
.
Typically, only an implementation of SolverInterface will call this method. If the storage was already typed as T, this is a no-op. If there were not any solver_details previously, or if it was of a different type, initializes the storage to a default-constructed T. Returns a reference to the mutable solver_details object. The reference remains valid until the next call to this method, or until this MathematicalProgramResult is destroyed.