19 #include <qpOASES.hpp>
31 template<
typename T,
int d>
34 Ad = dlib::zeros_matrix(Ad);
36 T pow_dt[d+1]; pow_dt[0]=(T)1;
40 pow_dt[i] = pow_dt[i-1] * dt;
46 Ad(i,j) = q[j-i]*pow_dt[j-i];
48 Bd(i) = q[d-i]*pow_dt[d-i];
59 template<
int N,
int R,
int K,
int P>
64 static const int nV = R*K + N;
65 static const int nC = 2*N*K;
66 typedef dlib::matrix<real_t,N,N>
t_Ad;
67 typedef dlib::matrix<real_t,N,R>
t_Bd;
70 typedef dlib::matrix<real_t,N,1>
t_wx;
72 typedef dlib::matrix<real_t,R,1>
t_wu;
74 typedef dlib::matrix<real_t,N,1>
t_weps;
75 typedef dlib::matrix<real_t,N,1>
t_geps;
77 typedef dlib::matrix<real_t,N,K>
t_lbx;
78 typedef dlib::matrix<real_t,N,K>
t_ubx;
79 typedef dlib::matrix<real_t,R,K>
t_lbu;
80 typedef dlib::matrix<real_t,R,K>
t_ubu;
85 typedef dlib::matrix<real_t,N,1>
t_x0;
86 typedef dlib::matrix<real_t,N,K>
t_y;
88 typedef dlib::matrix<real_t,R,K>
t_uset;
91 typedef dlib::matrix<real_t,R,K>
t_U;
93 typedef dlib::matrix<real_t,N,K+1>
t_X;
95 typedef dlib::matrix<real_t,N,1>
t_eps;
134 dlib::matrix<real_t,N*K,1>
m_E;
135 dlib::matrix<real_t,N*K,R*K>
m_F;
136 dlib::matrix<real_t,N*K,N*K>
m_Hx;
137 dlib::matrix<real_t,R*K,R*K>
m_Hu;
139 dlib::matrix<real_t,R*K,1>
m_g1;
142 dlib::matrix<real_t,N*K,nV>
m_A1;
143 dlib::matrix<real_t,N*K,nV>
m_A2;
161 m_Ad = dlib::identity_matrix<real_t>(N);
175 m_y = dlib::zeros_matrix(
m_y);
176 m_X = dlib::zeros_matrix(
m_X);
178 m_U = dlib::zeros_matrix(
m_U);
186 for(
int i=0;i<=K;i++)
188 auto M =
new dlib::matrix<real_t,N,N>();
189 *M = dlib::identity_matrix<real_t>(N);
215 m_F = dlib::zeros_matrix(
m_F);
246 for(
int i=1;i<=K;i++)
278 for(
int j=0;j+i<K;j++)
281 auto rows = dlib::range((i+j)*N,(i+j)*N+N-1);
282 auto cols = dlib::range(j*R,j*R+R-1);
283 dlib::set_subm(
m_F,rows,cols) = Ad_pow_i_Bd;
317 m_Hu((K-1)*R+j,(K-1)*R+j) =
m_wu(j);
367 set_colm(
m_A1,dlib::range(0,R*K-1)) =
m_F;
375 set_colm(
m_A2,dlib::range(0,R*K-1)) =
m_F;
378 inline int idx(
int row,
int col,
int nrows,
int ncols)
const
380 return row*ncols+col;
385 for(
int i=0;i<R*K;i++)
387 for(
int j=0;j<R*K;j++)
402 for(
int i=0;i<R*K;i++)
413 for(
int i=0;i<N*K;i++)
415 for(
int j=0;j<
nV;j++)
420 for(
int i=0;i<N*K;i++)
422 for(
int j=0;j<
nV;j++)
528 for(
int i=0;i<R*K;i++)
536 for(
int j=0;j<=K;j++)
558 dlib::matrix<double,N,1>
x;
559 dlib::matrix<double,R,1> u;
562 x = dlib::colm(
m_X,i);
563 u = dlib::colm(
m_U,i);
570 dlib::set_subm(
m_resultfun.
getData(),dlib::range(N+1,N+R),dlib::range(i*P+j,i*P+j)) = u;
573 x = dlib::colm(
m_X,K);
574 u = dlib::colm(
m_U,K-1);
612 std::cout<<
"reinitialized qp";
630 return m_qproblem->isInfeasible()==qpOASES::BT_FALSE;
637 return m_qproblem->isSolved()==qpOASES::BT_TRUE;
688 double dt_int = Tend/(K*P);
adoreMatrix< T, n+1, 0 > & getData()
Definition: llinearpiecewisefunction.h:147
Definition: lq_oc_single_shooting.h:61
LQ_OC_single_shooting()
Definition: lq_oc_single_shooting.h:583
bool isSolved() const
Definition: lq_oc_single_shooting.h:635
t_uset_flat m_uset_flat
set point
Definition: lq_oc_single_shooting.h:131
qpOASES::int_t m_qpnWSR_in
Definition: lq_oc_single_shooting.h:154
t_lbu_hard m_lbu_hard
upper bound on state trace
Definition: lq_oc_single_shooting.h:117
t_y m_y
initial state
Definition: lq_oc_single_shooting.h:122
real_t m_qpA[nC *nV]
Definition: lq_oc_single_shooting.h:148
void update(const t_x0 &x0, const t_y &y, const t_lbx &lbx, const t_ubx &ubx, const t_lbu_hard &lbu_hard, const t_ubu_hard &ubu_hard, const t_ubeps &ubeps)
Definition: lq_oc_single_shooting.h:666
t_geps m_geps
quadratic weights for slack (will be put in Heps)
Definition: lq_oc_single_shooting.h:113
t_uset m_uset
Definition: lq_oc_single_shooting.h:130
t_wu_end m_wu_end
weights for inputs (will be put on main diagonal of Hu)
Definition: lq_oc_single_shooting.h:111
void compute_init()
Definition: lq_oc_single_shooting.h:473
dlib::matrix< real_t, N, N > t_Ad
number of linear system constraints in QP: upper and lower bounds for x and u separately due to slack
Definition: lq_oc_single_shooting.h:66
dlib::matrix< real_t, N, 1 > t_ubeps
upper bound on input trace, hard constraint going to ub
Definition: lq_oc_single_shooting.h:83
t_weps & weps()
Definition: lq_oc_single_shooting.h:710
void updateA2()
Definition: lq_oc_single_shooting.h:373
int getnV()
Definition: lq_oc_single_shooting.h:731
dlib::matrix< real_t, N, 1 > t_eps
the computed states [x(t_1),..., x(t_K)]
Definition: lq_oc_single_shooting.h:95
void updateE()
Definition: lq_oc_single_shooting.h:258
t_wu m_wu
weights for states at endpoint
Definition: lq_oc_single_shooting.h:110
real_t m_qpcputime_in
qpOASES: "The integer argument nWSR species the maximum number of working set recalculations to be p...
Definition: lq_oc_single_shooting.h:156
dlib::matrix< real_t, N *K, 1 > t_y_flat
desired states (reference) y=[y_0(t_1);y_1(t_1);y_2(t_1);y_0(t_2);y_1(t_2);y_2(t_2);....
Definition: lq_oc_single_shooting.h:87
real_t m_qplbA[nC]
Definition: lq_oc_single_shooting.h:151
dlib::matrix< real_t, N, K+1 > t_X
the computed control [u(t_0),...,u(t_K-1)]
Definition: lq_oc_single_shooting.h:93
t_Ad m_Ad_p
discrete time input matrix
Definition: lq_oc_single_shooting.h:103
void updateqplb()
Definition: lq_oc_single_shooting.h:428
t_eps & eps()
Definition: lq_oc_single_shooting.h:725
void initialize_userdata_matrices()
qpOASES: "If cputime is not the null pointer, it contains the maximum allowed CPU time in seconds for...
Definition: lq_oc_single_shooting.h:159
void setMaxCPUTime(real_t value)
Definition: lq_oc_single_shooting.h:729
dlib::matrix< real_t, N, K > t_ubx
lower bound on state trace
Definition: lq_oc_single_shooting.h:78
static const int nC
number of variables in the QP: var=[u;eps]
Definition: lq_oc_single_shooting.h:65
void delete_memory()
Definition: lq_oc_single_shooting.h:194
dlib::matrix< real_t, R *K, R *K > m_H11
first part of overall linear cost term, g=[F^T Hx E - F^T Hx y; geps]=[g1;geps]
Definition: lq_oc_single_shooting.h:140
void updateH11()
Definition: lq_oc_single_shooting.h:357
t_wx & wx()
Definition: lq_oc_single_shooting.h:706
t_resultfun m_resultfun
powers of Ad, so these don't have to be recomputed online
Definition: lq_oc_single_shooting.h:106
dlib::matrix< real_t, R, K > t_lbu
upper bound on state trace
Definition: lq_oc_single_shooting.h:79
dlib::matrix< real_t, R *K, 1 > m_g1
Definition: lq_oc_single_shooting.h:139
int idx(int row, int col, int nrows, int ncols) const
Definition: lq_oc_single_shooting.h:378
bool m_system_changed
the slack
Definition: lq_oc_single_shooting.h:98
dlib::matrix< real_t, R, K > t_lbu_hard
upper bound on input trace
Definition: lq_oc_single_shooting.h:81
t_Bd & Bd_p()
Definition: lq_oc_single_shooting.h:703
virtual ~LQ_OC_single_shooting()
Definition: lq_oc_single_shooting.h:598
void updateg1()
Definition: lq_oc_single_shooting.h:331
t_Ad & Ad()
Definition: lq_oc_single_shooting.h:700
real_t m_qpH[nV *nV]
Definition: lq_oc_single_shooting.h:146
static const int nV
Definition: lq_oc_single_shooting.h:64
dlib::matrix< real_t, N, 1 > t_weps
weights for inputs at endpoint
Definition: lq_oc_single_shooting.h:74
void updateHeps()
Heps:=diag(weps);.
Definition: lq_oc_single_shooting.h:321
dlib::matrix< real_t, N, K > t_y
initial state
Definition: lq_oc_single_shooting.h:86
qpOASES::int_t m_qpnWSR_out
qpOASES: "The integer argument nWSR species the maximum number of working set recalculations to be p...
Definition: lq_oc_single_shooting.h:155
int getnWSR()
Definition: lq_oc_single_shooting.h:727
dlib::matrix< real_t, R *K, 1 > t_uset_flat
set point
Definition: lq_oc_single_shooting.h:89
qpOASES::QProblem * getQProblem()
Definition: lq_oc_single_shooting.h:728
t_ubx m_ubx
lower bound on state trace
Definition: lq_oc_single_shooting.h:116
void updateqpg()
Definition: lq_oc_single_shooting.h:400
t_lbx & lbx()
Definition: lq_oc_single_shooting.h:713
t_ubeps m_ubeps
upper bound on input trace, hard constraint going to ub
Definition: lq_oc_single_shooting.h:119
t_y & y()
Definition: lq_oc_single_shooting.h:720
real_t m_qpub[nV]
Definition: lq_oc_single_shooting.h:150
void updateSystem(const t_Ad &Ad, const t_Bd &Bd, const t_wx &wx, const t_wu &wu)
Definition: lq_oc_single_shooting.h:642
void setSystemChanged(bool value)
Definition: lq_oc_single_shooting.h:680
void retrieveSolution()
Definition: lq_oc_single_shooting.h:525
t_ubu_hard & ubu_hard()
Definition: lq_oc_single_shooting.h:716
dlib::matrix< real_t, N, 1 > t_geps
quadratic weights for slack (will be put in Heps)
Definition: lq_oc_single_shooting.h:75
adore::mad::LLinearPiecewiseFunctionM< double, N+R > t_resultfun
discrete time input matrix
Definition: lq_oc_single_shooting.h:68
dlib::matrix< real_t, N *(K+1), 1 > t_X_flat
the computed states [x(t_1),..., x(t_K)]
Definition: lq_oc_single_shooting.h:94
t_lbx m_lbx
linear weights for slack
Definition: lq_oc_single_shooting.h:115
t_uset & uset()
Definition: lq_oc_single_shooting.h:721
dlib::matrix< real_t, R, K > t_ubu_hard
lower bound on input trace, hard constraint going to lb
Definition: lq_oc_single_shooting.h:82
dlib::matrix< real_t, R *K, R *K > m_Hu
Definition: lq_oc_single_shooting.h:137
t_Bd & Bd()
Definition: lq_oc_single_shooting.h:701
t_lbu_hard & lbu_hard()
Definition: lq_oc_single_shooting.h:715
real_t m_qpg[nV]
Definition: lq_oc_single_shooting.h:147
void compute()
Definition: lq_oc_single_shooting.h:606
t_Bd m_Bd_p
system matrix for interpolation
Definition: lq_oc_single_shooting.h:104
t_ubx & ubx()
Definition: lq_oc_single_shooting.h:714
t_wx_end & wx_end()
Definition: lq_oc_single_shooting.h:708
qpOASES::QProblem * m_qproblem
state soft constraints equation lower bound
Definition: lq_oc_single_shooting.h:145
void updateA1()
Definition: lq_oc_single_shooting.h:365
t_wu_end & wu_end()
Definition: lq_oc_single_shooting.h:709
dlib::matrix< real_t, N, R > t_Bd
discrete time system matrix future version: one Ad,Bd per time step
Definition: lq_oc_single_shooting.h:67
dlib::matrix< real_t, R *K, 1 > t_U_flat
the computed control [u(t_0),...,u(t_K-1)]
Definition: lq_oc_single_shooting.h:92
std::vector< t_Ad * > m_Ad_powers
input matrix for interpolation
Definition: lq_oc_single_shooting.h:105
void updateF()
Definition: lq_oc_single_shooting.h:273
dlib::matrix< real_t, N *K, R *K > m_F
[pow(Ad,1);pow(Ad,2);...;pow(Ad,k)]*x0
Definition: lq_oc_single_shooting.h:135
t_U m_U
desired states (reference) y=[y_0(t_1);y_1(t_1);y_2(t_1);y_0(t_2);y_1(t_2);y_2(t_2);....
Definition: lq_oc_single_shooting.h:125
void updateqpub()
Definition: lq_oc_single_shooting.h:439
dlib::matrix< real_t, N, 1 > t_x0
lbeps==0, softconstraints for x and/or u if ubeps>0
Definition: lq_oc_single_shooting.h:85
dlib::matrix< real_t, R, K > t_U
set point
Definition: lq_oc_single_shooting.h:91
void setMaxnWSR(int value)
Definition: lq_oc_single_shooting.h:730
t_geps & geps()
Definition: lq_oc_single_shooting.h:711
dlib::matrix< real_t, R, K > t_ubu
lower bound on input trace
Definition: lq_oc_single_shooting.h:80
t_wx m_wx
Definition: lq_oc_single_shooting.h:108
qpOASES::real_t real_t
Definition: lq_oc_single_shooting.h:63
t_y_flat m_y_flat
desired states (reference) y=[y_0(t_1);y_1(t_1);y_2(t_1);y_0(t_2);y_1(t_2);y_2(t_2);....
Definition: lq_oc_single_shooting.h:123
t_x0 m_x0
lbeps==0, softconstraints for x and/or u if ubeps>0
Definition: lq_oc_single_shooting.h:121
t_Ad & Ad_p()
Definition: lq_oc_single_shooting.h:702
real_t m_qpxOpt[nV]
Definition: lq_oc_single_shooting.h:153
dlib::matrix< real_t, N, 1 > t_wx_end
weights for states (will be put on main diagonal of Hx)
Definition: lq_oc_single_shooting.h:71
t_X m_X
Definition: lq_oc_single_shooting.h:127
void updateqpH()
Definition: lq_oc_single_shooting.h:383
void updateqpubA()
Definition: lq_oc_single_shooting.h:453
int getnC()
Definition: lq_oc_single_shooting.h:732
void updateHx()
Hx:=diag(wx)
Definition: lq_oc_single_shooting.h:288
t_wx_end m_wx_end
weights for states (will be put on main diagonal of Hx)
Definition: lq_oc_single_shooting.h:109
t_ubeps & ubeps()
Definition: lq_oc_single_shooting.h:717
void updateSystem(const t_Ad &Ad, const t_Bd &Bd, const t_wx &wx, const t_wu &wu, const t_weps &weps)
Definition: lq_oc_single_shooting.h:650
real_t m_qpubA[nC]
Definition: lq_oc_single_shooting.h:152
t_resultfun & result_fun()
Definition: lq_oc_single_shooting.h:704
void updateAd()
Definition: lq_oc_single_shooting.h:244
dlib::matrix< real_t, N, 1 > t_wx
function for result interpolation
Definition: lq_oc_single_shooting.h:70
t_weps m_weps
weights for inputs at endpoint
Definition: lq_oc_single_shooting.h:112
void initialize_interface_variables()
Definition: lq_oc_single_shooting.h:203
t_eps m_eps
Definition: lq_oc_single_shooting.h:129
dlib::matrix< real_t, N, K > t_lbx
linear weights for slack
Definition: lq_oc_single_shooting.h:77
dlib::matrix< real_t, N *K, 1 > m_E
set point
Definition: lq_oc_single_shooting.h:134
void updateqpA()
Definition: lq_oc_single_shooting.h:411
t_x0 & x0()
Definition: lq_oc_single_shooting.h:719
void interpolateSolution()
Definition: lq_oc_single_shooting.h:556
void initialize_intermediate_matrices()
Definition: lq_oc_single_shooting.h:213
void compute_hotstart()
Definition: lq_oc_single_shooting.h:505
dlib::matrix< real_t, R, K > t_uset
desired states (reference) y=[y_0(t_1);y_1(t_1);y_2(t_1);y_0(t_2);y_1(t_2);y_2(t_2);....
Definition: lq_oc_single_shooting.h:88
dlib::matrix< real_t, R, 1 > t_wu_end
weights for inputs (will be put on main diagonal of Hu)
Definition: lq_oc_single_shooting.h:73
real_t getCPUTime()
Definition: lq_oc_single_shooting.h:726
dlib::matrix< real_t, R, 1 > t_wu
weights for states at endpoint
Definition: lq_oc_single_shooting.h:72
t_Ad m_Ad
Definition: lq_oc_single_shooting.h:101
real_t m_qpcputime_out
qpOASES: "If cputime is not the null pointer, it contains the maximum allowed CPU time in seconds for...
Definition: lq_oc_single_shooting.h:157
void updateHu()
Hu:=diag(wu)
Definition: lq_oc_single_shooting.h:305
real_t m_qplb[nV]
Definition: lq_oc_single_shooting.h:149
void updateSystem(const t_Ad &Ad, const t_Bd &Bd, const t_wx &wx, const t_wu &wu, const t_weps &weps, const t_geps &geps)
Definition: lq_oc_single_shooting.h:658
void initialize_memory()
Definition: lq_oc_single_shooting.h:183
t_Bd m_Bd
discrete time system matrix future version: one Ad,Bd per time step
Definition: lq_oc_single_shooting.h:102
t_X_flat m_X_flat
the computed states [x(t_1),..., x(t_K)]
Definition: lq_oc_single_shooting.h:128
dlib::matrix< real_t, N *K, nV > m_A2
state soft constraints equation upper bound
Definition: lq_oc_single_shooting.h:143
t_U & U()
Definition: lq_oc_single_shooting.h:724
t_U_flat m_U_flat
the computed control [u(t_0),...,u(t_K-1)]
Definition: lq_oc_single_shooting.h:126
dlib::matrix< real_t, N, N > m_Heps
Definition: lq_oc_single_shooting.h:138
void setEndTime(double Tend)
Definition: lq_oc_single_shooting.h:684
t_wu & wu()
Definition: lq_oc_single_shooting.h:707
dlib::matrix< real_t, N *K, N *K > m_Hx
[B,0,0,..0;AB,B,0,...0;AAB,AB,B,...,0;...;pow(A,k-1)B,pow(A,k-2)B,pow(A,k-3),B,......
Definition: lq_oc_single_shooting.h:136
void update(const t_x0 &x0, const t_y &y, const t_lbx &lbx, const t_ubx &ubx)
Definition: lq_oc_single_shooting.h:673
dlib::matrix< real_t, N *K, nV > m_A1
upper left part of overal quadratic cost term H=[Hu + F^T Hx F, 0; 0, Heps]=[H11,0;0,...
Definition: lq_oc_single_shooting.h:142
t_X & X()
Definition: lq_oc_single_shooting.h:723
bool isFeasible() const
Definition: lq_oc_single_shooting.h:628
t_ubu_hard m_ubu_hard
lower bound on input trace, hard constraint going to lb
Definition: lq_oc_single_shooting.h:118
adoreMatrix< T, 1, n > linspace(T x0, T x1)
Definition: adoremath.h:91
void define_integrator_chain(dlib::matrix< T, d, d > &Ad, dlib::matrix< T, d, 1 > &Bd, T dt)
Definition: lq_oc_single_shooting.h:32
x
Definition: adore_set_goal.py:30
Definition: areaofeffectconverter.h:20