-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtdb.m
executable file
·174 lines (132 loc) · 5.42 KB
/
dtdb.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description
%
% This file uses experimental data from the Franka manipulator to
% learn a LDS with inputs using different data-driven methods. In the
% end, it saves LQR gains for all data-driven models.
% The data-driven learning methods used are:
% 1. Least-squares (LS) unconstrained (possibly unstable) A and B
% matrix pair
% 2. A learned matrix pair [A, B] with SOC, that simultaneously
% learns a stable A, and a B matrix.
% 3. A learned matirx pair [A, B] with WLS, that learns a stable
% A, without updating the least-squares B matrix solution.
% 4. A learned matrix pair [A,B] with CG, that learns a stable A,
% without updating the least-squares B matrix solution.
%
%
%
% Given experimental data from the Franka manipulator, the code:
% 1. Combines all data (discontinuous) runs into one file
% 2. Computes the least-squares solution
% 3. Computes the SOC, WLS, and CG stable solutions
% 4. Calculates LQR gains for each method
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear; close all; clc; system = 'Franka';
algorithms_path = 'algorithms/'; % path for stable LDS algorithms
save_directory = 'results_dtdb/';
mkdir(save_directory);
% Whether resume training: 0 for new training, 1 for resuming training
status = 0;
addpath(algorithms_path);
options.graphic = 0;
options.posdef = 10e-12;
options.maxiter = 200000;
% settings;
nStates = 17; % Number of system states
nControl = 7; % Number of system inputs
if(status == 0)
LS_error = zeros(254, 27);
SC_error = zeros(254, 27);
WLS_error = zeros(254, 27);
CG_error = zeros(254, 27);
SOC_error = zeros(254, 27);
LS_time = zeros(254, 27);
SC_time = zeros(254, 27);
WLS_time = zeros(254, 27);
CG_time = zeros(254, 27);
SOC_time = zeros(254, 27);
else
CG_error = load('results_dtdb/CG_error.mat').CG_error;
CG_time = load('results_dtdb/CG_time.mat').CG_time;
SC_error = load('results_dtdb/SC_error.mat').SC_error;
SC_time = load('results_dtdb/SC_time.mat').SC_time;
LS_error = load('results_dtdb/LS_error.mat').LS_error;
LS_time = load('results_dtdb/LS_time.mat').LS_time;
WLS_error = load('results_dtdb/WLS_error.mat').WLS_error;
WLS_time = load('results_dtdb/WLS_time.mat').WLS_time;
SOC_error = load('results_dtdb/SOC_error.mat').SOC_error;
SOC_time = load('results_dtdb/SOC_time.mat').SOC_time;
end
dataRoot = 'datasets/dtdb/prepared/';
for seq = 0:285
disp(['current seq ', num2str(seq)]);
loc = [dataRoot, 'dtdb_', num2str(seq + 1), '.mat'];
data = double(load(loc).seq.') / 255.0;
[total_state, length] = size(data);
if(length > 300)
data = data(:, 1:300);
end
[data_U, data_S, data_V] = svd(data);
for stateNum = 3:30
SOCspace_S = data_S(1:stateNum, :);
SOCspace_data = SOCspace_S * data_V.';
disp(['dimension ', num2str(stateNum)])
X_ = SOCspace_data(:, 1:end - 1);
Y_ = SOCspace_data(:, 2:end);
% %% Compute LS (unconstrained) [A, B] solution
tStart = tic;
tStart_SC = tic;
LS = LDS(X_, Y_);
tLS = toc(tStart);
SC = eigenclip(LS, 1);
tSC = toc(tStart_SC);
ls_error = mean(mean(abs(Y_ - LS * X_)));
sc_error = mean(mean(abs(Y_ - SC * X_)));
LS_time(seq+1, stateNum - 2) = tLS;
SC_time(seq+1, stateNum - 2) = tSC;
LS_error(seq+1, stateNum - 2) = ls_error;
SC_error(seq+1, stateNum - 2) = sc_error;
save([save_directory, 'LS_time.mat'], 'LS_time');
save([save_directory, 'LS_error.mat'], 'LS_error');
save([save_directory, 'SC_time.mat'], 'SC_time');
save([save_directory, 'SC_error.mat'], 'SC_error');
%% Compute SOC (stable) [A, B] solution
timeSOC = tic;
[SOC, ~] = learnSOCmodel(X_,Y_, options);
tSOC = toc(timeSOC);
soc_error = mean(mean(abs(Y_ - SOC*X_)));
SOC_time(seq+1, stateNum - 2) = tSOC;
SOC_error(seq+1, stateNum - 2) = soc_error;
save([save_directory, 'SOC_time.mat'], 'SOC_time');
save([save_directory, 'SOC_error.mat'], 'SOC_error');
%% Compute WLS (stable) [A, B] solution
Pnew = [X_(:,1), Y_];
[U_wls,S_wls,V_wls] = svd(Pnew,0);
n = stateNum;
V_wls = V_wls(:,1:n);
S_wls = S_wls(1:n,1:n);
U_wls = U_wls(:,1:n);
timeWLS = tic;
[WLS, ~, ~, ~] = learnWLSmodel(V_wls,S_wls,1,0);
tWLS = toc(timeWLS);
%
wls_error = mean(mean(abs(S_wls*V_wls(2:end,:)' - A_WLS * S_wls*V_wls(1:end-1,:)')));
WLS_error(seq+1, stateNum - 2) = error_wls;
WLS_time(seq+1, stateNum - 2) = tWLS;
%
save([save_directory, 'WLS_time.mat'], 'WLS_time');
save([save_directory, 'WLS_error.mat'], 'WLS_error');
%% Compute CG (stable) [A, B] solution
timeCG = tic;
[A, ~, ~, ~] = learnCGModel(X_, Y_, 1, 0);
tCG = toc(timeCG);
cg_error = mean(mean(abs(Y_ - A_CG*X_)));
CG_error(seq+1, stateNum - 2) = cg_error;
CG_time(seq+1, stateNum - 2) = tCG;
save([save_directory, 'CG_time.mat'], 'CG_time');
save([save_directory, 'CG_error.mat'], 'CG_error');
end
end