aligator  0.14.0
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)
24 : ndx_(ndx)
25 , Vx_(ndx)
26 , Vxx_(ndx, ndx) {
27 Vx_.setZero();
28 Vxx_.setZero();
29 }
30
31 friend std::ostream &operator<<(std::ostream &oss,
32 const ValueFunctionTpl &store) {
33 oss << "ValueFunction {\n";
34 oss << fmt::format("\tndx: {:d}", store.ndx_);
35 oss << "\n}";
36 return oss;
37 }
38};
39
43template <typename _Scalar> struct QFunctionTpl {
44 using Scalar = _Scalar;
46
47 long ndx_;
48 long nu_;
49 long ndy_;
50 long ntot() const { return ndx_ + nu_ + ndy_; }
51
52 Scalar q_ = 0.;
53
54 VectorXs grad_;
55 MatrixXs hess_;
56
57 VectorRef Qx;
58 VectorRef Qu;
59
60 MatrixRef Qxx;
61 MatrixRef Qxu;
62 MatrixRef Qxy;
63 MatrixRef Quu;
64 MatrixRef Quy;
65 MatrixRef Qyy;
66
67 QFunctionTpl(const long ndx, const long nu, const long ndy)
68 : ndx_(ndx)
69 , nu_(nu)
70 , ndy_(ndy)
71 , grad_(ndx_ + nu_)
72 , hess_(ntot(), ntot())
73 , Qx(grad_.head(ndx))
74 , Qu(grad_.segment(ndx, nu))
75 , Qxx(hess_.topLeftCorner(ndx, ndx))
76 , Qxu(hess_.block(0, ndx, ndx, nu))
77 , Qxy(hess_.topRightCorner(ndx, ndy))
78 , Quu(hess_.block(ndx, ndx, nu, nu))
79 , Quy(hess_.block(ndx, ndx + nu, nu, ndy))
80 , Qyy(hess_.bottomRightCorner(ndy, ndy)) {
81 grad_.setZero();
82 hess_.setZero();
83 assert(hess_.rows() == ntot());
84 assert(hess_.cols() == ntot());
85 }
86
88 : QFunctionTpl(qf.ndx_, qf.nu_, qf.ndy_) {
89 ndx_ = qf.ndx_;
90 nu_ = qf.nu_;
91 ndy_ = qf.ndy_;
92 q_ = qf.q_;
93 grad_ = qf.grad_;
94 hess_ = qf.hess_;
95
96 redef_refs(*this);
97 }
98
100 : QFunctionTpl(0, 0, 0) {
101 swap(*this, qf);
102 }
103
105 swap(*this, qf);
106 return *this;
107 }
108
110 ndx_ = qf.ndx_;
111 nu_ = qf.nu_;
112 ndy_ = qf.ndy_;
113 q_ = qf.q_;
114 grad_ = qf.grad_;
115 hess_ = qf.hess_;
116
117 redef_refs(*this);
118 return *this;
119 }
120
121 friend std::ostream &operator<<(std::ostream &oss,
122 const QFunctionTpl &store) {
123 oss << "QFunction {\n";
124 oss << fmt::format("ndx: {:d}, nu: {:d}, ndy: {:d}", store.ndx_, store.nu_,
125 store.ndy_);
126 oss << "\n}";
127 return oss;
128 }
129
130 friend void swap(QFunctionTpl &qa, QFunctionTpl &qb) {
131 using std::swap;
132
133 swap(qa.ndx_, qb.ndx_);
134 swap(qa.nu_, qb.nu_);
135 swap(qa.ndy_, qb.ndy_);
136 swap(qa.q_, qb.q_);
137 swap(qa.grad_, qb.grad_);
138 swap(qa.hess_, qb.hess_);
139
140 redef_refs(qa);
141 redef_refs(qb);
142 }
143
144protected:
145 // reinitialize Ref members
146 static void redef_refs(QFunctionTpl &q) {
147 auto ndx = q.ndx_;
148 auto nu = q.nu_;
149 auto ndy = q.ndy_;
150 new (&q.Qx) VectorRef(q.grad_.head(ndx));
151 new (&q.Qu) VectorRef(q.grad_.segment(ndx, nu));
152
153 new (&q.Qxx) MatrixRef(q.hess_.topLeftCorner(ndx, ndx));
154 new (&q.Qxu) MatrixRef(q.hess_.block(0, ndx, ndx, nu));
155 new (&q.Qxy) MatrixRef(q.hess_.topRightCorner(ndx, ndy));
156 new (&q.Quu) MatrixRef(q.hess_.block(ndx, ndx, nu, nu));
157 new (&q.Quy) MatrixRef(q.hess_.block(ndx, ndx + nu, nu, ndy));
158 new (&q.Qyy) MatrixRef(q.hess_.bottomRightCorner(ndy, ndy));
159 }
160};
161
162} // namespace aligator
163
164#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
165#include "aligator/solvers/value-function.txx"
166#endif
Forward declarations.
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)