-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcostFunctions.cpp
More file actions
198 lines (170 loc) · 7.49 KB
/
Copy pathcostFunctions.cpp
File metadata and controls
198 lines (170 loc) · 7.49 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// This file is part of mpc.
// mpc is free software: you can redistribute it and/or
// modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mpc is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with mpc. If not, see
// <http://www.gnu.org/licenses/>.
// header
#include "costFunctions.h"
// mpc
#include "AutoSpan.h"
namespace mpc {
/*************************************************************************************************
* Cost Function *
*************************************************************************************************/
CostFunction::CostFunction(std::string&& name)
: name_(std::move(name))
, fullSizeEntry_(false)
{
}
void CostFunction::autoSpan()
{
}
void CostFunction::initializeCost(const PreviewSystem& ps)
{
Q_.resize(ps.fullUDim, ps.fullUDim);
c_.resize(ps.fullUDim);
}
/*************************************************************************************************
* Trajectory Cost Function *
*************************************************************************************************/
void TrajectoryCost::autoSpan()
{
auto max_dim = std::max(M_.rows(), std::max(weights_.rows(), p_.rows()));
AutoSpan::spanMatrix(M_, max_dim);
AutoSpan::spanVector(p_, max_dim);
AutoSpan::spanVector(weights_, max_dim);
}
void TrajectoryCost::initializeCost(const PreviewSystem& ps)
{
CostFunction::initializeCost(ps);
if (M_.rows() != p_.rows())
DOMAIN_ERROR_EXCEPTION(throwMsgOnRowsAskAutoSpan("M", "p", M_, p_));
if (M_.cols() == ps.xDim) {
Q_.setZero();
c_.setZero();
} else if (M_.cols() == ps.fullXDim) {
fullSizeEntry_ = true;
} else {
DOMAIN_ERROR_EXCEPTION(throwMsgOnColsOnPSXDim("M", M_, &ps));
}
}
void TrajectoryCost::update(const PreviewSystem& ps)
{
if (fullSizeEntry_) {
Eigen::MatrixXd tmp {M_ * ps.Psi};
Q_ = tmp.transpose() * weights_.asDiagonal() * tmp;
c_ = (M_ * (ps.Phi * ps.x0 + ps.xi) + p_).transpose() * weights_.asDiagonal() * tmp;
} else {
for (int i = 0; i < ps.nrXStep; ++i) { // Can be optimized. Lot of sums of zero here
Eigen::MatrixXd tmp {M_ * ps.Psi.block(i * ps.xDim, 0, ps.xDim, ps.fullUDim)};
Q_.noalias() += tmp.transpose() * weights_.asDiagonal() * tmp;
c_.noalias() += (M_ * (ps.Phi.block(i * ps.xDim, 0, ps.xDim, ps.xDim) * ps.x0 + ps.xi.segment(i * ps.xDim, ps.xDim)) + p_).transpose() * weights_.asDiagonal() * tmp;
}
}
}
/*************************************************************************************************
* Target Cost Function *
*************************************************************************************************/
void TargetCost::initializeCost(const PreviewSystem& ps)
{
CostFunction::initializeCost(ps);
if (M_.rows() != p_.rows())
DOMAIN_ERROR_EXCEPTION(throwMsgOnRowsAskAutoSpan("M", "p", M_, p_));
if (M_.cols() != ps.xDim)
DOMAIN_ERROR_EXCEPTION(throwMsgOnRowsOnPSxDim("M", M_, &ps));
}
void TargetCost::update(const PreviewSystem& ps)
{
Eigen::MatrixXd tmp {M_ * ps.Psi.bottomRows(ps.xDim)};
Q_.noalias() = tmp.transpose() * weights_.asDiagonal() * tmp;
c_.noalias() = (M_ * (ps.Phi.bottomRows(ps.xDim) * ps.x0 + ps.xi.bottomRows(ps.xDim)) + p_).transpose() * weights_.asDiagonal() * tmp;
}
/*************************************************************************************************
* Control Cost Function *
*************************************************************************************************/
void ControlCost::autoSpan()
{
auto max_dim = std::max(N_.rows(), std::max(weights_.rows(), p_.rows()));
AutoSpan::spanMatrix(N_, max_dim);
AutoSpan::spanVector(p_, max_dim);
AutoSpan::spanVector(weights_, max_dim);
}
void ControlCost::initializeCost(const PreviewSystem& ps)
{
CostFunction::initializeCost(ps);
if (N_.rows() != p_.rows())
DOMAIN_ERROR_EXCEPTION(throwMsgOnRowsAskAutoSpan("N", "p", N_, p_));
if (N_.cols() == ps.uDim)
Q_.setZero();
else if (N_.cols() == ps.fullUDim)
fullSizeEntry_ = true;
else
DOMAIN_ERROR_EXCEPTION(throwMsgOnColsOnPSUDim("N", N_, &ps));
}
void ControlCost::update(const PreviewSystem& ps)
{
if (fullSizeEntry_) {
Q_.noalias() = N_.transpose() * weights_.asDiagonal() * N_;
c_.noalias() = p_.transpose() * weights_.asDiagonal() * N_;
} else {
Eigen::MatrixXd mat {N_.transpose() * weights_.asDiagonal() * N_};
Eigen::VectorXd vec {p_.transpose() * weights_.asDiagonal() * N_};
for (int i = 0; i < ps.nrUStep; ++i) {
Q_.block(i * ps.uDim, i * ps.uDim, ps.uDim, ps.uDim) = mat;
c_.segment(i * ps.uDim, ps.uDim) = vec;
}
}
}
/*************************************************************************************************
* Mixed Cost Function *
*************************************************************************************************/
void MixedCost::autoSpan()
{
auto max_dim = std::max(M_.rows(), std::max(N_.rows(), std::max(weights_.rows(), p_.rows())));
AutoSpan::spanMatrix(M_, max_dim, 1); // This is tricky. Has X and U are not the same dimensions, we need to handle it.
AutoSpan::spanMatrix(N_, max_dim);
AutoSpan::spanVector(p_, max_dim);
AutoSpan::spanVector(weights_, max_dim);
}
void MixedCost::initializeCost(const PreviewSystem& ps)
{
CostFunction::initializeCost(ps);
if (M_.rows() != p_.rows())
DOMAIN_ERROR_EXCEPTION(throwMsgOnRowsAskAutoSpan("M", "p", M_, p_));
if (N_.rows() != p_.rows())
DOMAIN_ERROR_EXCEPTION(throwMsgOnRowsAskAutoSpan("N", "p", N_, p_));
if (M_.cols() == ps.xDim && N_.cols() == ps.uDim) {
Q_.setZero();
c_.setZero();
} else if (M_.cols() == ps.fullXDim && N_.cols() == ps.fullUDim) {
fullSizeEntry_ = true;
} else {
DOMAIN_ERROR_EXCEPTION(throwMsgOnColsOnPSXUDim("M", "N", M_, N_, &ps));
}
}
void MixedCost::update(const PreviewSystem& ps)
{
if (fullSizeEntry_) {
Eigen::MatrixXd tmp = M_ * ps.Psi + N_;
Q_.noalias() = tmp.transpose() * weights_.asDiagonal() * tmp;
c_.noalias() = (M_ * (ps.Phi * ps.x0 + ps.xi) + p_).transpose() * weights_.asDiagonal() * tmp;
} else {
for (int i = 0; i < ps.nrUStep; ++i) { // Can be optimized. Lot of sums of zero here
Eigen::MatrixXd tmp {M_ * ps.Psi.block(i * ps.xDim, 0, ps.xDim, ps.fullUDim)};
tmp.block(0, i * ps.uDim, M_.rows(), ps.uDim).noalias() += N_;
Q_.noalias() += tmp.transpose() * weights_.asDiagonal() * tmp;
c_.noalias() += (M_ * (ps.Phi.block(i * ps.xDim, 0, ps.xDim, ps.xDim) * ps.x0 + ps.xi.segment(i * ps.xDim, ps.xDim)) + p_).transpose() * weights_.asDiagonal() * tmp;
}
}
}
} // namespace mpc