bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
qp_solver.h
Go to the documentation of this file.
1#ifndef BITSIM_MATHS_OPTIMIZATION_QP_SOLVER_H
2#define BITSIM_MATHS_OPTIMIZATION_QP_SOLVER_H
3
6
7#include <Eigen/Dense>
8
9#include <memory>
10#include <vector>
11
12namespace bitsim
13{
14namespace maths
15{
16namespace optimization
17{
18
34
44
54
82{
83 public:
84 using Matrix = RealMat;
86
87 explicit QPSolver(const QPSettings &settings = QPSettings());
89 QPSolver(const QPSolver &other);
90 QPSolver &operator=(const QPSolver &other);
91 QPSolver(QPSolver &&other) noexcept;
92 QPSolver &operator=(QPSolver &&other) noexcept;
93
100 void setup(const Matrix &P, const Vector &q, const Matrix &C, const Vector &l, const Vector &u);
101
107 void setup(index_t n, const std::vector<QPEntry> &P, const Vector &q, index_t m,
108 const std::vector<QPEntry> &C, const Vector &l, const Vector &u);
109
113 void update(const Vector &q, const Vector &l, const Vector &u);
114
118 QPStatus solve();
119
123 void warm_start(const Vector &x);
124
128 void reset();
129
130 const Vector &solution() const noexcept { return x_out_; }
131
136 const Vector &duals() const noexcept { return y_out_; }
137
138 uint_t iterations() const noexcept { return iterations_; }
139 real_t primal_residual() const noexcept { return primal_residual_; }
140 real_t dual_residual() const noexcept { return dual_residual_; }
141 QPStatus status() const noexcept { return status_; }
142 const QPSettings &settings() const noexcept { return settings_; }
143
144 private:
145 // the sparse matrices and the factorisation, kept out of this header: Eigen's sparse
146 // matrices must not be included before Chrono's headers, which extend them
147 struct Sparse;
148
149 void setup_(Sparse &problem, const Vector &q, const Vector &l, const Vector &u);
150 void check_bounds_(const Vector &l, const Vector &u) const;
151 bool factorise_();
152 void equilibrate_(const Vector &q);
153 void unscale_();
154 void compute_residuals_();
155
156 QPSettings settings_;
157 std::unique_ptr<Sparse> sp_;
158 Vector q_;
159 Vector l_;
160 Vector u_;
161 Vector rho_;
162 real_t rho_scalar_{0.1};
163 bool is_set_up_{false};
164
165 // the scaling: the variables by D, the constraints by E and the cost by cost_scale_
166 Vector D_;
167 Vector E_;
168 real_t cost_scale_{1.0};
169
170 // the iterates of the scaled problem, and the solution and the duals of the given one
171 Vector x_;
172 Vector z_;
173 Vector y_;
174 Vector x_out_;
175 Vector y_out_;
176 real_t primal_scale_{0.0};
177 real_t dual_scale_{0.0};
178 uint_t iterations_{0};
179 real_t primal_residual_{0.0};
180 real_t dual_residual_{0.0};
181 QPStatus status_{QPStatus::NOT_SET_UP};
182};
183
184} // namespace optimization
185} // namespace maths
186} // namespace bitsim
187
188#endif // BITSIM_MATHS_OPTIMIZATION_QP_SOLVER_H
#define BITSIM_EXPORT
Marks a class or a function that bitsim's library exports from a source file compiled with hidden vis...
Definition bitsim_export.h:16
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
QPSolver(const QPSettings &settings=QPSettings())
Definition qp_solver.cpp:54
RealMat Matrix
Definition qp_solver.h:84
const Vector & duals() const noexcept
Definition qp_solver.h:136
real_t dual_residual() const noexcept
Definition qp_solver.h:140
real_t primal_residual() const noexcept
Definition qp_solver.h:139
QPStatus status() const noexcept
Definition qp_solver.h:141
void warm_start(const Vector &x)
Start the next solve from x, and from no constraint being active.
Definition qp_solver.cpp:323
uint_t iterations() const noexcept
Definition qp_solver.h:138
void setup(const Matrix &P, const Vector &q, const Matrix &C, const Vector &l, const Vector &u)
Set the matrices and the bounds, and factorise the matrix of the iterations.
Definition qp_solver.cpp:117
void reset()
Forget the last solution: the next solve starts from zero.
Definition qp_solver.cpp:335
void update(const Vector &q, const Vector &l, const Vector &u)
Change q and the bounds, keeping P and C and the factorisation.
Definition qp_solver.cpp:293
const QPSettings & settings() const noexcept
Definition qp_solver.h:142
RealColVec Vector
Definition qp_solver.h:85
QPSolver(QPSolver &&other) noexcept
QPStatus solve()
Solve, starting from the last solution.
Definition qp_solver.cpp:365
QPSolver & operator=(const QPSolver &other)
Definition qp_solver.cpp:63
const Vector & solution() const noexcept
Definition qp_solver.h:130
QPSolver & operator=(QPSolver &&other) noexcept
Definition qp_solver.cpp:14
QPStatus
How a solve ended.
Definition qp_solver.h:49
@ MAX_ITERATIONS
stopped at max_iterations; the solution is the last iterate
Definition qp_solver.h:51
@ SOLVED
the residuals are within the tolerances
Definition qp_solver.h:50
@ NOT_SET_UP
solve() before setup()
Definition qp_solver.h:52
Definition math_utils.h:14
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Eigen::Index index_t
The type Eigen indexes and sizes matrices and vectors with.
Definition bitsim_types.h:45
ColVec< real_t > RealColVec
Dynamically sized column vector.
Definition bitsim_types.h:122
double real_t
real_t
Definition bitsim_types.h:20
DynMat< real_t > RealMat
Dynamically sized matrix of type double.
Definition bitsim_types.h:56
A non-zero of a sparse matrix: its row, its column and its value.
Definition qp_solver.h:39
index_t row
Definition qp_solver.h:40
index_t col
Definition qp_solver.h:41
real_t value
Definition qp_solver.h:42
The settings of QPSolver.
Definition qp_solver.h:23
uint_t max_iterations
iterations at most
Definition qp_solver.h:29
real_t eps_rel
relative tolerance of the residuals
Definition qp_solver.h:28
uint_t scaling_iterations
iterations of the Ruiz equilibration; 0 for none
Definition qp_solver.h:32
real_t alpha
over-relaxation, in (0, 2)
Definition qp_solver.h:26
uint_t check_every
iterations between checks of the residuals
Definition qp_solver.h:30
real_t eps_abs
absolute tolerance of the residuals
Definition qp_solver.h:27
bool adaptive_rho
change rho to balance the residuals, as OSQP does
Definition qp_solver.h:31
real_t rho
the step of the dual update; equality rows use 1000 rho
Definition qp_solver.h:24
real_t sigma
a small regularisation of the x update
Definition qp_solver.h:25