aligator 0.19.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
value-function.hpp
Go to the documentation of this file.
1
6#pragma once
7
9
10#include <ostream>
11#include <fmt/format.h>
12
13namespace aligator {
14
16template <typename _Scalar> struct ValueFunctionTpl {
17 using Scalar = _Scalar;
19 int ndx_;
20 VectorXs Vx_;
21 MatrixXs Vxx_;
23
24 ValueFunctionTpl(const int ndx)
25 : ndx_(ndx)
26 , Vx_(ndx)
27 , Vxx_(ndx, ndx) {
28 Vx_.setZero();
29 Vxx_.setZero();
30 }
31
32 friend std::ostream &operator<<(std::ostream &oss,
33 const ValueFunctionTpl &store) {
34 oss << "ValueFunction {\n";
35 oss << fmt::format("\tndx: {:d}", store.ndx_);
36 oss << "\n}";
37 return oss;
38 }
39};
40
44template <typename _Scalar> struct QFunctionTpl {
45 using Scalar = _Scalar;
47
48 long ndx_;
49 long nu_;
50 long ndy_;
51 long ntot() const { return ndx_ + nu_ + ndy_; }
52
53 Scalar q_ = 0.;
54
55 VectorXs grad_;
56 MatrixXs hess_;
57
58 VectorRef Qx;
59 VectorRef Qu;
60
61 MatrixRef Qxx;
62 MatrixRef Qxu;
63 MatrixRef Qxy;
64 MatrixRef Quu;
65 MatrixRef Quy;
66 MatrixRef Qyy;
67
68 QFunctionTpl(const long ndx, const long nu, const long ndy)
69 : ndx_(ndx)
70 , nu_(nu)
71 , ndy_(ndy)
72 , grad_(ndx_ + nu_)
73 , hess_(ntot(), ntot())
74 , Qx(grad_.head(ndx))
75 , Qu(grad_.segment(ndx, nu))
76 , Qxx(hess_.topLeftCorner(ndx, ndx))
77 , Qxu(hess_.block(0, ndx, ndx, nu))
78 , Qxy(hess_.topRightCorner(ndx, ndy))
79 , Quu(hess_.block(ndx, ndx, nu, nu))
80 , Quy(hess_.block(ndx, ndx + nu, nu, ndy))
81 , Qyy(hess_.bottomRightCorner(ndy, ndy)) {
82 grad_.setZero();
83 hess_.setZero();
84 assert(hess_.rows() == ntot());
85 assert(hess_.cols() == ntot());
86 }
87
89 : QFunctionTpl(qf.ndx_, qf.nu_, qf.ndy_) {
90 ndx_ = qf.ndx_;
91 nu_ = qf.nu_;
92 ndy_ = qf.ndy_;
93 q_ = qf.q_;
94 grad_ = qf.grad_;
95 hess_ = qf.hess_;
96
97 redef_refs(*this);
98 }
99
101 : QFunctionTpl(0, 0, 0) {
102 swap(*this, qf);
103 }
104
106 swap(*this, qf);
107 return *this;
108 }
109
111 ndx_ = qf.ndx_;
112 nu_ = qf.nu_;
113 ndy_ = qf.ndy_;
114 q_ = qf.q_;
115 grad_ = qf.grad_;
116 hess_ = qf.hess_;
117
118 redef_refs(*this);
119 return *this;
120 }
121
122 friend std::ostream &operator<<(std::ostream &oss,
123 const QFunctionTpl &store) {
124 oss << "QFunction {\n";
125 oss << fmt::format("ndx: {:d}, nu: {:d}, ndy: {:d}", store.ndx_, store.nu_,
126 store.ndy_);
127 oss << "\n}";
128 return oss;
129 }
130
131 friend void swap(QFunctionTpl &qa, QFunctionTpl &qb) {
132 using std::swap;
133
134 swap(qa.ndx_, qb.ndx_);
135 swap(qa.nu_, qb.nu_);
136 swap(qa.ndy_, qb.ndy_);
137 swap(qa.q_, qb.q_);
138 swap(qa.grad_, qb.grad_);
139 swap(qa.hess_, qb.hess_);
140
141 redef_refs(qa);
142 redef_refs(qb);
143 }
144
145protected:
146 // reinitialize Ref members
147 static void redef_refs(QFunctionTpl &q) {
148 auto ndx = q.ndx_;
149 auto nu = q.nu_;
150 auto ndy = q.ndy_;
151 new (&q.Qx) VectorRef(q.grad_.head(ndx));
152 new (&q.Qu) VectorRef(q.grad_.segment(ndx, nu));
153
154 new (&q.Qxx) MatrixRef(q.hess_.topLeftCorner(ndx, ndx));
155 new (&q.Qxu) MatrixRef(q.hess_.block(0, ndx, ndx, nu));
156 new (&q.Qxy) MatrixRef(q.hess_.topRightCorner(ndx, ndy));
157 new (&q.Quu) MatrixRef(q.hess_.block(ndx, ndx, nu, nu));
158 new (&q.Quy) MatrixRef(q.hess_.block(ndx, ndx + nu, nu, ndy));
159 new (&q.Qyy) MatrixRef(q.hess_.bottomRightCorner(ndy, ndy));
160 }
161};
162
163#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
164extern template struct ValueFunctionTpl<context::Scalar>;
165extern template struct QFunctionTpl<context::Scalar>;
166#endif
167} // namespace aligator
Main package namespace.
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)
friend std::ostream & operator<<(std::ostream &oss, const ValueFunctionTpl &store)