forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
choose_best_solver.cc
85 lines (81 loc) · 3.51 KB
/
choose_best_solver.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "drake/solvers/choose_best_solver.h"
#include "drake/solvers/equality_constrained_qp_solver.h"
#include "drake/solvers/gurobi_solver.h"
#include "drake/solvers/ipopt_solver.h"
#include "drake/solvers/linear_system_solver.h"
#include "drake/solvers/moby_lcp_solver.h"
#include "drake/solvers/mosek_solver.h"
#include "drake/solvers/nlopt_solver.h"
#include "drake/solvers/osqp_solver.h"
#include "drake/solvers/scs_solver.h"
#include "drake/solvers/snopt_solver.h"
namespace drake {
namespace solvers {
SolverId ChooseBestSolver(const MathematicalProgram& prog) {
if (LinearSystemSolver::is_available() &&
LinearSystemSolver::ProgramAttributesSatisfied(prog)) {
return LinearSystemSolver::id();
} else if (EqualityConstrainedQPSolver::is_available() &&
EqualityConstrainedQPSolver::ProgramAttributesSatisfied(prog)) {
return EqualityConstrainedQPSolver::id();
} else if (MosekSolver::is_available() &&
MosekSolver::ProgramAttributesSatisfied(prog)) {
// TODO([email protected]): based on my limited experience, Mosek is
// faster than Gurobi for convex optimization problem. But we should run
// a more thorough comparison.
return MosekSolver::id();
} else if (GurobiSolver::is_available() &&
GurobiSolver::ProgramAttributesSatisfied(prog)) {
return GurobiSolver::id();
} else if (OsqpSolver::is_available() &&
OsqpSolver::ProgramAttributesSatisfied(prog)) {
return OsqpSolver::id();
} else if (MobyLCPSolver<double>::is_available() &&
MobyLCPSolver<double>::ProgramAttributesSatisfied(prog)) {
return MobyLcpSolverId::id();
} else if (SnoptSolver::is_available() &&
SnoptSolver::ProgramAttributesSatisfied(prog)) {
return SnoptSolver::id();
} else if (IpoptSolver::is_available() &&
IpoptSolver::ProgramAttributesSatisfied(prog)) {
return IpoptSolver::id();
} else if (NloptSolver::is_available() &&
NloptSolver::ProgramAttributesSatisfied(prog)) {
return NloptSolver::id();
} else if (ScsSolver::is_available() &&
ScsSolver::ProgramAttributesSatisfied(prog)) {
// Use SCS as the last resort. SCS uses ADMM method, which converges fast to
// modest accuracy quite fast, but then slows down significantly if the user
// wants high accuracy.
return ScsSolver::id();
}
throw std::invalid_argument(
"There is no available solver for the optimization program");
}
std::unique_ptr<SolverInterface> MakeSolver(const SolverId& id) {
if (id == LinearSystemSolver::id()) {
return std::make_unique<LinearSystemSolver>();
} else if (id == EqualityConstrainedQPSolver::id()) {
return std::make_unique<EqualityConstrainedQPSolver>();
} else if (id == MosekSolver::id()) {
return std::make_unique<MosekSolver>();
} else if (id == GurobiSolver::id()) {
return std::make_unique<GurobiSolver>();
} else if (id == OsqpSolver::id()) {
return std::make_unique<OsqpSolver>();
} else if (id == MobyLcpSolverId::id()) {
return std::make_unique<MobyLCPSolver<double>>();
} else if (id == SnoptSolver::id()) {
return std::make_unique<SnoptSolver>();
} else if (id == IpoptSolver::id()) {
return std::make_unique<IpoptSolver>();
} else if (id == NloptSolver::id()) {
return std::make_unique<NloptSolver>();
} else if (id == ScsSolver::id()) {
return std::make_unique<ScsSolver>();
} else {
throw std::invalid_argument("MakeSolver: no matching solver " + id.name());
}
}
} // namespace solvers
} // namespace drake