-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpcinterface_test.cpp
119 lines (103 loc) · 3.59 KB
/
mpcinterface_test.cpp
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
#include <Eigen/Core>
#include "mpcinterface.h"
#include "gtest/gtest.h"
TEST(MPCInterfaceTest, TestInitializedMatrixDimensions) {
// Initialize horizon length
int H = 5;
// Initialize state dimension
int n = 3;
// Initialize input dimension
int m = 2;
// Initialize MPCInterface object
MPCInterface mpc(H, n, m);
EXPECT_EQ(mpc.Q.rows(), n);
EXPECT_EQ(mpc.Q.cols(), n);
EXPECT_EQ(mpc.R.rows(), m);
EXPECT_EQ(mpc.R.cols(), m);
EXPECT_EQ(mpc.P.rows(), n);
EXPECT_EQ(mpc.P.cols(), n);
EXPECT_EQ(mpc.x_min.size(), n);
EXPECT_EQ(mpc.x_max.size(), n);
EXPECT_EQ(mpc.u_min.size(), m);
EXPECT_EQ(mpc.u_max.size(), m);
EXPECT_EQ(mpc.A.rows(), n);
EXPECT_EQ(mpc.A.cols(), n);
EXPECT_EQ(mpc.B.rows(), n);
EXPECT_EQ(mpc.B.cols(), m);
EXPECT_EQ(mpc.x_ref.cols(), H+1);
EXPECT_EQ(mpc.x_ref.rows(), n);
EXPECT_EQ(mpc.u_ref.cols(), H+1);
EXPECT_EQ(mpc.u_ref.rows(), m);
}
TEST(MPCInterfaceTest, TestCanonicalMatrixDimensions) {
// Initialize horizon length
int H = 5;
// Initialize state dimension
int n = 3;
// Initialize input dimension
int m = 2;
// Initialize MPCInterface object
MPCInterface mpc(H, n, m);
// Generate Canonical Form
MPCInterface::CanonicalForm canonical_form = mpc.getCanonicalForm();
EXPECT_EQ(canonical_form.P_bar.rows(), H * (n + m) + n);
EXPECT_EQ(canonical_form.P_bar.cols(), H * (n + m) + n);
EXPECT_EQ(canonical_form.q_bar.size(), H * (n + m) + n);
EXPECT_EQ(canonical_form.A_bar.rows(), H * n + H * (n + m));
EXPECT_EQ(canonical_form.A_bar.cols(), H * (n + m));
EXPECT_EQ(canonical_form.l_bar.size(), H * n + H * (n + m));
EXPECT_EQ(canonical_form.u_bar.size(), H * n + H * (n + m));
}
TEST(MPCInterfaceTest, TestCanonicalForm) {
int H = 2;
int n = 2;
int m = 1;
MPCInterface mpc(H, n, m);
mpc.Q = Eigen::MatrixXd::Identity(n, n);
mpc.R = Eigen::MatrixXd::Identity(m, m);
mpc.P = 2*Eigen::MatrixXd::Identity(n, n);
mpc.A = Eigen::MatrixXd::Identity(n, n);
mpc.A(0, 1) = 1;
mpc.B(0, 0) = 0;
mpc.B(1, 0) = 1;
mpc.x_ref(0, 0) = 1;
mpc.x_ref(1, 0) = 2;
mpc.x_ref(0, 1) = 2;
mpc.x_ref(1,1) = 3;
mpc.x_ref(0, 2) = 3;
mpc.x_ref(1,2) = 4;
mpc.u_ref(0, 0) = 1;
mpc.u_ref(0, 1) = 0;
mpc.x_min(0) = -10;
mpc.x_min(1) = -10;
mpc.x_max(0) = 10;
mpc.x_max(1) = 10;
mpc.u_min(0) = -2;
mpc.u_max(0) = 2;
MPCInterface::CanonicalForm canonical_form = mpc.getCanonicalForm();
Eigen::MatrixXd expected_P_bar(8, 8);
expected_P_bar << 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 2, 0,
0, 0, 0, 0, 0, 0, 0, 2;
EXPECT_EQ(canonical_form.P_bar, expected_P_bar);
Eigen::VectorXd expected_q_bar(8);
expected_q_bar << -1, -2, -2, -3, -1, 0, -6, -8;
EXPECT_EQ(canonical_form.q_bar, 2*expected_q_bar);
Eigen::MatrixXd expected_A_bar(10, 6);
expected_A_bar << 1, 1, 0, 0, 0, 0,
0, 1, 1, 0, 0, 0,
0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1;
EXPECT_EQ(canonical_form.A_bar, expected_A_bar);
}