alpaqa 0.0.1
Nonconvex constrained optimization
riskaverse-mpc.cpp
Go to the documentation of this file.
1#include <memory>
3
4namespace alpaqa {
5namespace problems {
6
8 unsigned nu = 2;
9 unsigned nx = 4;
10 unsigned ns = 2;
11 unsigned ny = 3;
12 unsigned n = nu + ns + ny;
13 unsigned m = 3;
14 real_t Ts = 0.05;
15
18
21
23
24 template <class VecX>
25 auto u(VecX &&x) const {
26 return x.segment(0, nu);
27 }
28 template <class VecX>
29 auto s(VecX &&x) const {
30 return x.segment(nu, ns);
31 }
32 template <class VecX>
33 auto y(VecX &&x) const {
34 return x.segment(nu + ns, ny);
35 }
36
37 Box get_C() const {
38 Box C{vec(n), vec(n)};
39 u(C.lowerbound).fill(-10);
40 u(C.upperbound).fill(10);
41 s(C.lowerbound).fill(-inf);
42 s(C.upperbound).fill(inf);
43 y(C.lowerbound).fill(0);
44 y(C.upperbound).fill(inf);
45 return C;
46 }
47
48 Box get_D() const {
49 Box D{vec(m), vec(m)};
50 D.lowerbound.fill(-inf);
51 D.upperbound.fill(0);
52 return D;
53 }
54
55 using Diag = Eigen::DiagonalMatrix<real_t, Eigen::Dynamic, Eigen::Dynamic>;
56
58 A = alpaqa::mat::Identity(nx, nx);
59 A(0, 2) = Ts;
60 A(1, 3) = Ts;
61 B = alpaqa::mat::Zero(nx, nu);
62 B(2, 0) = Ts;
63 B(3, 1) = Ts;
64
65 Q = Diag(nx);
66 Q.diagonal().fill(10);
67 R = Diag(nu);
68 R.diagonal().fill(1);
69
70 x0 = vec(nx);
71 x0.fill(10);
72 }
73
74 auto mpc_dynamics(crvec x, crvec u) const {
75 return A * x + B * u;
76 };
77
78 real_t f(crvec ux) const { return s(ux)(0); }
79 void grad_f(crvec ux, rvec grad_f) const {
80 (void)ux;
81 grad_f.setZero();
82 s(grad_f)(0) = 1;
83 }
84 void g(crvec ux, rvec g_u) const {
85 g_u(0) = y(ux)(0) - y(ux)(1) - s(ux)(0);
86 g_u(1) =
87 mpc_dynamics(x0, u(ux)).dot(Q * mpc_dynamics(x0, u(ux))) - s(ux)(1);
88 g_u(2) = x0.dot(Q * x0) + u(ux).dot(R * u(ux)) -
89 (y(ux)(0) - y(ux)(1) - y(ux)(2) - s(ux)(1));
90 }
91 void grad_g(crvec ux, crvec v, rvec grad_u_v) const {
92 alpaqa::mat grad = alpaqa::mat::Zero(n, m);
93 s(grad.col(0))(0) = -1;
94 y(grad.col(0))(0) = 1;
95 y(grad.col(0))(1) = -1;
96
97 u(grad.col(1)) = 2 * B.transpose() * (Q * (A * x0 + B * u(ux)));
98 s(grad.col(1))(1) = -1;
99
100 u(grad.col(2)) = 2 * (R * u(ux));
101 s(grad.col(2))(1) = 1;
102 y(grad.col(2))(0) = -1;
103 y(grad.col(2))(1) = 1;
104 y(grad.col(2))(2) = 1;
105
106 grad_u_v = grad * v;
107 }
108};
109
111 auto rptr = std::make_shared<RiskaverseProblem>();
112 auto &r = *rptr;
113 return Problem{
114 r.n,
115 r.m,
116 r.get_C(),
117 r.get_D(),
118 [rptr](crvec ux) { return rptr->f(ux); },
119 [rptr](crvec ux, rvec g_u) { rptr->grad_f(ux, g_u); },
120 [rptr](crvec ux, rvec g_u) { rptr->g(ux, g_u); },
121 [rptr](crvec ux, crvec v, rvec grad_u_v) {
122 rptr->grad_g(ux, v, grad_u_v);
123 },
124 {},
125 {},
126 {},
127 };
128}
129
130} // namespace problems
131} // namespace alpaqa
Problem riskaverse_mpc_problem()
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
constexpr real_t inf
Definition: vec.hpp:26
realmat mat
Default type for matrices.
Definition: vec.hpp:20
realvec vec
Default type for vectors.
Definition: vec.hpp:14
double real_t
Default floating point type.
Definition: vec.hpp:8
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
Problem description for minimization problems.
unsigned int n
Number of decision variables, dimension of x.
void grad_g(crvec ux, crvec v, rvec grad_u_v) const
Eigen::DiagonalMatrix< real_t, Eigen::Dynamic, Eigen::Dynamic > Diag
auto mpc_dynamics(crvec x, crvec u) const
void g(crvec ux, rvec g_u) const
void grad_f(crvec ux, rvec grad_f) const