-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
137 lines (101 loc) · 2.82 KB
/
main.m
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
% Q Learning for Optimal Control with LQR Criterion
% Author: Marios Papachristou
% AM: 03115101
% email: papachristoumarios@gmail.com
%% Parameters
% Actual System Dynamics
A = [0 1 0; 0 0 1; 0 0 0];
B = [0; 0; 1];
dim = size(A, 1) + size(B, 2);
% Sampling Period
Ts = 1;
sys = ss(A, B, zeros(size(A)), zeros(size(B)), Ts);
% Choose initial condition
x0 = 0.1 * ones(length(A), 1);
% LQR Parameters
rho = 1;
Q = eye(size(A));
%% Ideal LQR
% Solution to DARE
[Kid, Pid, e] = dlqr(A, B, Q, rho);
% Ideal H matrix
Hid = [Q + A' * Pid * A A' * Pid * B;
B' * Pid * A rho + B' * Pid * B];
% Number of iterations
Niter = 200;
time = 0 : Niter - 1;
epochs = 4;
gain_norms = zeros(epochs, 1);
H_norms = zeros(epochs, 1);
%% Q Learning
L = randn(size(B'));
Hp = zeros(dim, dim);
for ep = 1 : epochs
% Take random inputs
u_samples = randn(size(B, 2), Niter);
[H, K] = q_learning(A, B, L, Q, rho, Niter, Hp, x0, u_samples);
L = K;
Hp = H;
% Update norms plot
gain_norms(ep) = norm(L - Kid, 'fro');
H_norms(ep) = norm(H - Hid, 'fro');
% Plot results for epoch
Aq_c = A - B * K;
q_learning_model = ss(Aq_c, zeros(size(B)), zeros(size(A)), zeros(size(B)), Ts);
[~, ~, x_q] = lsim(q_learning_model, zeros(1, Niter), time, x0);
figure;
for i = 1 : length(x0)
subplot(2, 2, i);
stem(time, x_q(:, i), 'color', rand(1,3));
title(sprintf('Impulse responses for Q-Learned system for epoch %d', ep));
xlabel('Samples');
ylabel(sprintf('x_%d', i));
end
subplot(2,2,4);
stem(time, - K * x_q');
title('Input of Q-Learned System for the new policy');
xlabel('Samples');
ylabel('u');
% Simulate response under random noise
[~, ~, x_n] = lsim(sys, u_samples, time);
figure;
for i = 1 : length(x0)
subplot(2, 2, i);
stem(time, x_n(:, i), 'color', rand(1,3));
title(sprintf('Response for random input (Epoch %d)', ep));
xlabel('Samples');
ylabel(sprintf('x_%d', i));
end
subplot(2,2,4);
stem(time, u_samples);
title('Input');
xlabel('Samples');
ylabel('u');
end
%% Plot norm differences
figure;
plotyy(1:epochs, H_norms, 1 : epochs, gain_norms);
title('Norms difference from ideal (Frobenius)');
legend('|| H - H* ||', '|| K - K* ||');
xlabel('Epochs');
%% Ideal LQR
Aid_c = A - B * Kid;
ideal_model = ss(Aid_c, zeros(size(B)), zeros(size(A)), zeros(size(B)), Ts);
[~, ~, x_id] = lsim(ideal_model, zeros(1, Niter), time, x0);
figure;
hold on;
for i = 1 : length(x0)
stem(time, x_id(:, i), 'color', rand(1,3));
end
title('Impulse responses for ideal system')
legend('x1', 'x2', 'x3')
xlabel('Samples')
ylabel('State vector')
hold off
hold off
figure;
hold on
stem(time, - Kid * x_id');
title('Input of Ideal System')
xlabel('Samples')
hold off