-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_lfd_primp_trajectory.m
128 lines (102 loc) · 3.53 KB
/
benchmark_lfd_primp_trajectory.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
% Benchmark script for PRIMP and store learned trajectory distribution
%
% Author
% Sipu Ruan, 2023
close all; clear; clc;
add_paths()
% Name of the dataset
dataset_name = 'panda_arm';
demo_type = load_dataset_param(dataset_name);
%% Run benchmark for each demo type
id = [5, 6, 7, 8, 10];
for i = 1:length(id)
run_benchmark(dataset_name, demo_type{id(i)});
end
function run_benchmark(dataset_name, demo_type)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tunable parameters
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
% Number of samples on distribution
n_sample = 5;
% Number of time steps
n_step = 50;
% Group name
group_name = 'PCG';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_folder = strcat("../data/", dataset_name, "/", demo_type, "/");
result_folder = strcat("../result/benchmark/", dataset_name, "/",...
demo_type, "/");
result_prefix = strcat(result_folder, 'primp_', group_name, '/');
mkdir(result_folder);
mkdir(result_prefix);
%% Load and parse demo data
argin.n_step = n_step;
argin.data_folder = data_folder;
argin.group_name = 'SE';
filenames = dir(strcat(argin.data_folder, "*.json"));
g_demo = parse_demo_trajectory(filenames, argin);
% Load random via/goal poses
trials = load_random_trials(result_folder);
n_trial = length(trials.t_via{1});
disp("Loaded randomly generated configurations!");
%% Benchmark
% Compute trajectory distribution from demonstrations
[g_mean, cov_t] = get_pdf_from_demo(g_demo, group_name);
for i = 1:n_trial
clc;
disp('Benchmark: PRIMP (ours)')
disp(['Dataset: ', dataset_name])
disp(['Demo type: ', demo_type])
disp([num2str(i/(n_trial) * 100), '%'])
% Initiate class
param.n_sample = n_sample;
param.group_name = group_name;
primp_obj = PRIMP(g_mean.matrix, cov_t, param);
% Condition on via points
for j = 1:length(trials.t_via)
[mean_cond, cov_cond] = primp_obj.get_condition_pdf(...
trials.t_via{j}(i), trials.g_via{j}(:,:,i),...
trials.cov_via{j}(:,:,i));
end
g_samples = primp_obj.get_samples();
% Convert to pose
pose_samples = cell(n_sample, 1);
for m = 1:n_sample
n_step = length(g_samples{m});
pose_samples{m} = nan(n_step, 7);
for n = 1:n_step
pose_samples{m}(n,:) = homo2pose_quat(g_samples{m}(:,:,n));
end
end
pose_cond_mean = nan(n_step, 7);
for m = 1:n_step
pose_cond_mean(m,:) = homo2pose_axang(mean_cond(:,:,m));
end
cov_cond_step = zeros(6, 6, n_step);
for m = 1:n_step
idx_block = 6*(m-1)+1:6*m;
cov_cond_step(:,:,m) = cov_cond(idx_block, idx_block);
end
% Store learned trajectory
% Mean and covariance
prob_data.num_steps = n_step;
prob_data.mean = permute(mean_cond, [3,1,2]);
prob_data.covariance_joint = cov_cond;
prob_data.covariance_step = permute(cov_cond_step, [3,1,2]);
json_data = jsonencode(prob_data);
fid = fopen(strcat(result_prefix, 'reference_density_', num2str(i),...
'.json'), 'w');
fprintf(fid, '%s', json_data);
fclose(fid);
writematrix(pose_cond_mean, strcat(result_prefix,...
'reference_density_mean_', num2str(i),'.csv'));
% Samples
sample_data.num_samples = n_sample;
sample_data.num_steps = n_step;
sample_data.samples = pose_samples;
json_data = jsonencode(sample_data);
fid = fopen(strcat(result_prefix, 'samples_', num2str(i),'.json'), 'w');
fprintf(fid, '%s', json_data);
fclose(fid);
end
end