-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.cpp
83 lines (70 loc) · 2.23 KB
/
main.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
/*
// solvers for Algebraic Riccati equation
// - Iteration (continuous)
// - Iteration (discrete)
// - Arimoto-Potter
//
// author: Horibe Takamasa
*/
#include <Eigen/Dense>
#include <iostream>
#include <time.h>
#include <vector>
#include "riccati_solver.h"
#define PRINT_MAT(X) std::cout << #X << ":\n" << X << std::endl << std::endl
int main() {
const uint dim_x = 4;
const uint dim_u = 1;
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(dim_x, dim_x);
Eigen::MatrixXd B = Eigen::MatrixXd::Zero(dim_x, dim_u);
Eigen::MatrixXd Q = Eigen::MatrixXd::Zero(dim_x, dim_x);
Eigen::MatrixXd R = Eigen::MatrixXd::Zero(dim_u, dim_u);
Eigen::MatrixXd P = Eigen::MatrixXd::Zero(dim_x, dim_x);
A(0, 1) = 1.0;
A(1, 1) = -15.0;
A(1, 2) = 10.0;
A(2, 3) = 1.0;
A(3, 3) = -15.0;
B(1, 0) = 10.0;
B(3, 0) = 1.0;
Q(0, 0) = 1.0;
Q(2, 2) = 1.0;
Q(3, 3) = 2.0;
R(0, 0) = 1.0;
PRINT_MAT(A);
PRINT_MAT(B);
PRINT_MAT(Q);
PRINT_MAT(R);
/* == iteration based Riccati solution (continuous) == */
std::cout << "-- Iteration based method (continuous) --" << std::endl;
clock_t start = clock();
solveRiccatiIterationC(A, B, Q, R, P);
clock_t end = clock();
std::cout << "computation time = " << (double)(end - start) / CLOCKS_PER_SEC
<< "sec." << std::endl;
PRINT_MAT(P);
/* == iteration based Riccati solution (discrete) == */
// discretization
const double dt = 0.001;
Eigen::MatrixXd I = Eigen::MatrixXd::Identity(dim_x, dim_x);
Eigen::MatrixXd Ad = Eigen::MatrixXd::Zero(dim_x, dim_x);
Ad = (I + 0.5 * dt * A) * (I - 0.5 * dt * A).inverse();
Eigen::MatrixXd Bd;
Bd = B * dt;
std::cout << "-- Iteration based method (discrete)--" << std::endl;
start = clock();
solveRiccatiIterationD(Ad, Bd, Q, R, P);
end = clock();
std::cout << "computation time = " << (double)(end - start) / CLOCKS_PER_SEC
<< "sec." << std::endl;
PRINT_MAT(P);
/* == eigen decomposition method (Arimoto-Potter algorithm) == */
std::cout << "-- Eigen decomposition mathod --" << std::endl;
start = clock();
solveRiccatiArimotoPotter(A, B, Q, R, P);
end = clock();
std::cout << "computation time = " << (double)(end - start) / CLOCKS_PER_SEC
<< "sec." << std::endl;
PRINT_MAT(P);
return 0;
}