forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.cc
65 lines (57 loc) · 2.24 KB
/
solve.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "drake/solvers/solve.h"
#include <memory>
#include "drake/common/nice_type_name.h"
#include "drake/solvers/choose_best_solver.h"
#include "drake/solvers/solver_interface.h"
namespace drake {
namespace solvers {
MathematicalProgramResult Solve(const MathematicalProgram& prog,
const optional<Eigen::VectorXd>& initial_guess,
const optional<SolverOptions>& solver_options) {
const SolverId solver_id = ChooseBestSolver(prog);
std::unique_ptr<SolverInterface> solver = MakeSolver(solver_id);
MathematicalProgramResult result{};
solver->Solve(prog, initial_guess, solver_options, &result);
return result;
}
MathematicalProgramResult Solve(
const MathematicalProgram& prog,
const Eigen::Ref<const Eigen::VectorXd>& initial_guess) {
const Eigen::VectorXd initial_guess_xd = initial_guess;
return Solve(prog, initial_guess_xd, {});
}
MathematicalProgramResult Solve(const MathematicalProgram& prog) {
return Solve(prog, {}, {});
}
std::vector<std::string> GetInfeasibleConstraints(
const MathematicalProgram& prog, const MathematicalProgramResult&
result, optional<double> tolerance) {
std::vector<std::string> descriptions;
if (!tolerance) {
// TODO(russt): Extract the constraint tolerance from the solver. This
// value was used successfully for some time in MATLAB Drake, so I've
// ported it as the default here.
tolerance = 1e-4;
}
for (const auto& binding : prog.GetAllConstraints()) {
const Eigen::VectorXd val = result.EvalBinding(binding);
const std::shared_ptr<Constraint>& constraint = binding.evaluator();
std::string d = constraint->get_description();
if (d.empty()) {
d = NiceTypeName::Get(*constraint);
}
for (int i = 0; i < val.rows(); i++) {
if (val[i] < constraint->lower_bound()[i] - *tolerance ||
val[i] > constraint->upper_bound()[i] + *tolerance) {
descriptions.push_back(
d + "[" + std::to_string(i) +
"]: " + std::to_string(constraint->lower_bound()[i]) +
" <= " + std::to_string(val[i]) +
" <= " + std::to_string(constraint->upper_bound()[i]));
}
}
}
return descriptions;
}
} // namespace solvers
} // namespace drake