aligator  0.6.1
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
value-function.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include "aligator/fwd.hpp"
9
10#include <ostream>
11
12namespace aligator {
13
15template <typename _Scalar> struct ValueFunctionTpl {
16 using Scalar = _Scalar;
18 int ndx_;
19 VectorXs Vx_;
20 MatrixXs Vxx_;
22
23 ValueFunctionTpl(const int ndx) : ndx_(ndx), Vx_(ndx), Vxx_(ndx, ndx) {
24 Vx_.setZero();
25 Vxx_.setZero();
26 }
27
28 friend std::ostream &operator<<(std::ostream &oss,
29 const ValueFunctionTpl &store) {
30 oss << "ValueFunction {\n";
31 oss << fmt::format("\tndx: {:d}", store.ndx_);
32 oss << "\n}";
33 return oss;
34 }
35};
36
40template <typename _Scalar> struct QFunctionTpl {
41 using Scalar = _Scalar;
43
44 long ndx_;
45 long nu_;
46 long ndy_;
47 long ntot() const { return ndx_ + nu_ + ndy_; }
48
49 Scalar q_ = 0.;
50
51 VectorXs grad_;
52 MatrixXs hess_;
53
54 VectorRef Qx;
55 VectorRef Qu;
56
57 MatrixRef Qxx;
58 MatrixRef Qxu;
59 MatrixRef Qxy;
60 MatrixRef Quu;
61 MatrixRef Quy;
62 MatrixRef Qyy;
63
64 QFunctionTpl(const long ndx, const long nu, const long ndy)
65 : ndx_(ndx), nu_(nu), ndy_(ndy), grad_(ndx_ + nu_), hess_(ntot(), ntot()),
66 Qx(grad_.head(ndx)), Qu(grad_.segment(ndx, nu)),
67 Qxx(hess_.topLeftCorner(ndx, ndx)), Qxu(hess_.block(0, ndx, ndx, nu)),
68 Qxy(hess_.topRightCorner(ndx, ndy)), Quu(hess_.block(ndx, ndx, nu, nu)),
69 Quy(hess_.block(ndx, ndx + nu, nu, ndy)),
70 Qyy(hess_.bottomRightCorner(ndy, ndy)) {
71 grad_.setZero();
72 hess_.setZero();
73 assert(hess_.rows() == ntot());
74 assert(hess_.cols() == ntot());
75 }
76
78 : QFunctionTpl(qf.ndx_, qf.nu_, qf.ndy_) {
79 ndx_ = qf.ndx_;
80 nu_ = qf.nu_;
81 ndy_ = qf.ndy_;
82 q_ = qf.q_;
83 grad_ = qf.grad_;
84 hess_ = qf.hess_;
85
86 redef_refs(*this);
87 }
88
89 QFunctionTpl(QFunctionTpl &&qf) : QFunctionTpl(0, 0, 0) { swap(*this, qf); }
90
92 swap(*this, qf);
93 return *this;
94 }
95
97 ndx_ = qf.ndx_;
98 nu_ = qf.nu_;
99 ndy_ = qf.ndy_;
100 q_ = qf.q_;
101 grad_ = qf.grad_;
102 hess_ = qf.hess_;
103
104 redef_refs(*this);
105 return *this;
106 }
107
108 friend std::ostream &operator<<(std::ostream &oss,
109 const QFunctionTpl &store) {
110 oss << "QFunction {\n";
111 oss << fmt::format("ndx: {:d}, nu: {:d}, ndy: {:d}", store.ndx_, store.nu_,
112 store.ndy_);
113 oss << "\n}";
114 return oss;
115 }
116
117 friend void swap(QFunctionTpl &qa, QFunctionTpl &qb) {
118 using std::swap;
119
120 swap(qa.ndx_, qb.ndx_);
121 swap(qa.nu_, qb.nu_);
122 swap(qa.ndy_, qb.ndy_);
123 swap(qa.q_, qb.q_);
124 swap(qa.grad_, qb.grad_);
125 swap(qa.hess_, qb.hess_);
126
127 redef_refs(qa);
128 redef_refs(qb);
129 }
130
131protected:
132 // reinitialize Ref members
133 static void redef_refs(QFunctionTpl &q) {
134 auto ndx = q.ndx_;
135 auto nu = q.nu_;
136 auto ndy = q.ndy_;
137 new (&q.Qx) VectorRef(q.grad_.head(ndx));
138 new (&q.Qu) VectorRef(q.grad_.segment(ndx, nu));
139
140 new (&q.Qxx) MatrixRef(q.hess_.topLeftCorner(ndx, ndx));
141 new (&q.Qxu) MatrixRef(q.hess_.block(0, ndx, ndx, nu));
142 new (&q.Qxy) MatrixRef(q.hess_.topRightCorner(ndx, ndy));
143 new (&q.Quu) MatrixRef(q.hess_.block(ndx, ndx, nu, nu));
144 new (&q.Quy) MatrixRef(q.hess_.block(ndx, ndx + nu, nu, ndy));
145 new (&q.Qyy) MatrixRef(q.hess_.bottomRightCorner(ndy, ndy));
146 }
147};
148
149} // namespace aligator
150
151#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
152#include "aligator/core/value-function.txx"
153#endif
Forward declarations.
Main package namespace.
Q-function model parameters.
friend void swap(QFunctionTpl &qa, QFunctionTpl &qb)
friend std::ostream & operator<<(std::ostream &oss, const QFunctionTpl &store)
ALIGATOR_DYNAMIC_TYPEDEFS(Scalar)
QFunctionTpl & operator=(const QFunctionTpl &qf)
QFunctionTpl(const long ndx, const long nu, const long ndy)
QFunctionTpl(const QFunctionTpl &qf)
QFunctionTpl & operator=(QFunctionTpl &&qf)
QFunctionTpl(QFunctionTpl &&qf)
static void redef_refs(QFunctionTpl &q)
Storage for the value function model parameters.
friend std::ostream & operator<<(std::ostream &oss, const ValueFunctionTpl &store)