demo_TPGMR_LQR01.m 7.23 KB
Newer Older
Milad Malekzadeh's avatar
Milad Malekzadeh committed
1
function demo_TPGMR_LQR01
Milad Malekzadeh's avatar
Milad Malekzadeh committed
2 3 4 5 6 7
% Demonstration a task-parameterized probabilistic model encoding movements in the form of virtual spring-damper
% systems acting in multiple frames of reference. Each candidate coordinate system observes a set of
% demonstrations from its own perspective, by extracting an attractor path whose variations depend on the
% relevance of the frame through the task. This information is exploited to generate a new attractor path
% corresponding to new situations (new positions and orientation of the frames), while the predicted covariances
% are exploited by a linear quadratic regulator (LQR) to estimate the stiffness and damping feedback terms of
Milad Malekzadeh's avatar
Milad Malekzadeh committed
8 9
% the spring-damper systems, resulting in a minimal intervention control strategy.
%
Sylvain Calinon's avatar
Sylvain Calinon committed
10
% This example presents the results for a time-based GMR reference retrieval process combined with a finite horizon LQR.
Milad Malekzadeh's avatar
Milad Malekzadeh committed
11
%
Sylvain Calinon's avatar
Sylvain Calinon committed
12 13
% Sylvain Calinon, 2015
% http://programming-by-demonstration.org/lib/
Milad Malekzadeh's avatar
Milad Malekzadeh committed
14
%
Milad Malekzadeh's avatar
Milad Malekzadeh committed
15 16
% This source code is given for free! In exchange, I would be grateful if you cite
% the following reference in any academic publication that uses this code or part of it:
Milad Malekzadeh's avatar
Milad Malekzadeh committed
17 18 19 20 21 22 23 24 25 26 27
%
% @inproceedings{Calinon14ICRA,
%   author="Calinon, S. and Bruno, D. and Caldwell, D. G.",
%   title="A task-parameterized probabilistic model with minimal intervention control",
%   booktitle="Proc. {IEEE} Intl Conf. on Robotics and Automation ({ICRA})",
%   year="2014",
%   month="May-June",
%   address="Hong Kong, China",
%   pages="3339--3344"
% }

28 29
addpath('./m_fcts/');

Sylvain Calinon's avatar
Sylvain Calinon committed
30

Milad Malekzadeh's avatar
Milad Malekzadeh committed
31 32 33 34 35
%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
model.nbStates = 3; %Number of Gaussians in the GMM
model.nbFrames = 2; %Number of candidate frames of reference
model.nbVar = 3; %Dimension of the datapoints in the dataset (here: t,x1,x2)
Milad Malekzadeh's avatar
Milad Malekzadeh committed
36
model.dt = 0.01; %Time step
Milad Malekzadeh's avatar
Milad Malekzadeh committed
37
nbRepros = 8; %Number of reproductions with new situations randomly generated
38
rFactor = 1E-2; %Weighting term for the minimization of control commands in LQR
Milad Malekzadeh's avatar
Milad Malekzadeh committed
39 40 41 42 43


%% Load 3rd order tensor data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Load 3rd order tensor data...');
Milad Malekzadeh's avatar
Milad Malekzadeh committed
44 45 46 47
% The MAT file contains a structure 's' with the multiple demonstrations. 's(n).Data' is a matrix data for
% sample n (with 's(n).nbData' datapoints). 's(n).p(m).b' and 's(n).p(m).A' contain the position and
% orientation of the m-th candidate coordinate system for this demonstration. 'Data' contains the observations
% in the different frames. It is a 3rd order tensor of dimension D x P x N, with D=3 the dimension of a
Milad Malekzadeh's avatar
Milad Malekzadeh committed
48 49 50 51 52
% datapoint, P=2 the number of candidate frames, and N=200x4 the number of datapoints in a trajectory (200)
% multiplied by the number of demonstrations (5).
load('data/DataLQR01.mat');


53
%% TP-GMM learning
Milad Malekzadeh's avatar
Milad Malekzadeh committed
54
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55
fprintf('Parameters estimation of TP-GMM with EM:');
Milad Malekzadeh's avatar
Milad Malekzadeh committed
56
model = init_tensorGMM_timeBased(Data, model); %Initialization
Milad Malekzadeh's avatar
Milad Malekzadeh committed
57 58 59 60 61
model = EM_tensorGMM(Data, model);


%% Reproduction with LQR for the task parameters used to train the model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62
disp('Reproductions with finite-horizon LQR...');
Milad Malekzadeh's avatar
Milad Malekzadeh committed
63 64 65 66
DataIn = [1:s(1).nbData] * model.dt;
for n=1:nbSamples
	%Retrieval of attractor path through task-parameterized GMR
	a(n) = estimateAttractorPath(DataIn, model, s(n));
Sylvain Calinon's avatar
Sylvain Calinon committed
67 68
	r(n) = reproduction_LQR_finiteHorizon(model, a(n), a(n).currTar(:,1), rFactor);
	r(n).Data = [DataIn; r(n).Data];
Milad Malekzadeh's avatar
Milad Malekzadeh committed
69 70 71 72 73
end


%% Reproduction with LQR for new task parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74
disp('New reproductions with finite-horizon LQR...');
Milad Malekzadeh's avatar
Milad Malekzadeh committed
75
for n=1:nbRepros
Milad Malekzadeh's avatar
Milad Malekzadeh committed
76
	for m=1:model.nbFrames
Milad Malekzadeh's avatar
Milad Malekzadeh committed
77
		%Random generation of new task parameters
Milad Malekzadeh's avatar
Milad Malekzadeh committed
78 79 80 81 82 83 84
		id=ceil(rand(2,1)*nbSamples);
		w=rand(2); w=w/sum(w);
		rTmp.p(m).b = s(id(1)).p(m).b * w(1) + s(id(2)).p(m).b * w(2);
		rTmp.p(m).A = s(id(1)).p(m).A * w(1) + s(id(2)).p(m).A * w(2);
	end
	%Retrieval of attractor path through task-parameterized GMR
	anew(n) = estimateAttractorPath(DataIn, model, rTmp);
Sylvain Calinon's avatar
Sylvain Calinon committed
85 86
	rnew(n) = reproduction_LQR_finiteHorizon(model, anew(n), anew(n).currTar(:,1), rFactor);
	rnew(n).Data = [DataIn; rnew(n).Data];
Milad Malekzadeh's avatar
Milad Malekzadeh committed
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
end


%% Plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure('position',[20,50,1300,500]);
xx = round(linspace(1,64,nbSamples));
clrmap = colormap('jet');
clrmap = min(clrmap(xx,:),.95);
limAxes = [-1.2 0.8 -1.1 0.9];
colPegs = [[.9,.5,.9];[.5,.9,.5]];

%DEMOS
subplot(1,3,1); hold on; box on; title('Demonstrations');
for n=1:nbSamples
	%Plot frames
	for m=1:model.nbFrames
		plot([s(n).p(m).b(2) s(n).p(m).b(2)+s(n).p(m).A(2,3)], [s(n).p(m).b(3) s(n).p(m).b(3)+s(n).p(m).A(3,3)], '-','linewidth',6,'color',colPegs(m,:));
		plot(s(n).p(m).b(2), s(n).p(m).b(3),'.','markersize',30,'color',colPegs(m,:)-[.05,.05,.05]);
	end
	%Plot trajectories
	plot(s(n).Data0(2,1), s(n).Data0(3,1),'.','markersize',12,'color',clrmap(n,:));
	plot(s(n).Data0(2,:), s(n).Data0(3,:),'-','linewidth',1.5,'color',clrmap(n,:));
end
axis(limAxes); axis square; set(gca,'xtick',[],'ytick',[]);

%REPROS
114
subplot(1,3,2); hold on; box on; title('Repros with finite horizon LQR');
Milad Malekzadeh's avatar
Milad Malekzadeh committed
115 116 117 118 119 120 121 122 123 124 125 126
for n=1:nbSamples
	%Plot frames
	for m=1:model.nbFrames
		plot([s(n).p(m).b(2) s(n).p(m).b(2)+s(n).p(m).A(2,3)], [s(n).p(m).b(3) s(n).p(m).b(3)+s(n).p(m).A(3,3)], '-','linewidth',6,'color',colPegs(m,:));
		plot(s(n).p(m).b(2), s(n).p(m).b(3),'.','markersize',30,'color',colPegs(m,:)-[.05,.05,.05]);
	end
end
for n=1:nbSamples
	%Plot trajectories
	plot(r(n).Data(2,1), r(n).Data(3,1),'.','markersize',12,'color',clrmap(n,:));
	plot(r(n).Data(2,:), r(n).Data(3,:),'-','linewidth',1.5,'color',clrmap(n,:));
end
127 128 129 130
for n=1:nbSamples
	%Plot Gaussians
	plotGMM(r(n).Mu(2:3,:,1), r(n).Sigma(2:3,2:3,:,1), [.5 .5 .5],.8);
end
Milad Malekzadeh's avatar
Milad Malekzadeh committed
131 132 133
axis(limAxes); axis square; set(gca,'xtick',[],'ytick',[]);

%NEW REPROS
134
subplot(1,3,3); hold on; box on; title('New repros with finite horizon LQR');
Milad Malekzadeh's avatar
Milad Malekzadeh committed
135 136 137 138 139 140 141 142 143 144 145 146
for n=1:nbRepros
	%Plot frames
	for m=1:model.nbFrames
		plot([rnew(n).p(m).b(2) rnew(n).p(m).b(2)+rnew(n).p(m).A(2,3)], [rnew(n).p(m).b(3) rnew(n).p(m).b(3)+rnew(n).p(m).A(3,3)], '-','linewidth',6,'color',colPegs(m,:));
		plot(rnew(n).p(m).b(2), rnew(n).p(m).b(3), '.','markersize',30,'color',colPegs(m,:)-[.05,.05,.05]);
	end
end
for n=1:nbRepros
	%Plot trajectories
	plot(rnew(n).Data(2,1), rnew(n).Data(3,1),'.','markersize',12,'color',[.2 .2 .2]);
	plot(rnew(n).Data(2,:), rnew(n).Data(3,:),'-','linewidth',1.5,'color',[.2 .2 .2]);
end
147 148 149 150
for n=1:nbRepros
	%Plot Gaussians
	plotGMM(rnew(n).Mu(2:3,:,1), rnew(n).Sigma(2:3,2:3,:,1), [.5 .5 .5],.8);
end
Milad Malekzadeh's avatar
Milad Malekzadeh committed
151 152
axis(limAxes); axis square; set(gca,'xtick',[],'ytick',[]);

153 154 155 156 157
%print('-dpng','graphs/demo_TPGMR_LQR01.png');


%% Plot additional information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Milad Malekzadeh's avatar
Milad Malekzadeh committed
158
figure;
Milad Malekzadeh's avatar
Milad Malekzadeh committed
159 160 161 162 163 164 165 166 167 168 169 170 171
%Plot norm of control commands
subplot(1,2,1); hold on;
for n=1:nbRepros
	plot(DataIn, rnew(n).ddxNorm, 'k-', 'linewidth', 2);
end
xlabel('t'); ylabel('|ddx|');
%Plot strength of the stiffness term
subplot(1,2,2); hold on;
for n=1:nbRepros
	plot(DataIn, rnew(n).kpDet, 'k-', 'linewidth', 2);
end
xlabel('t'); ylabel('|Kp|');

172 173 174 175 176 177 178 179
% %Plot accelerations due to feedback and feedforward terms
% figure; hold on;
% n=1; k=1;
% plot(r(n).FB(k,:),'r-','linewidth',2);
% plot(r(n).FF(k,:),'b-','linewidth',2);
% legend('ddx feedback','ddx feedforward');
% xlabel('t'); ylabel(['ddx_' num2str(k)]);

Milad Malekzadeh's avatar
Milad Malekzadeh committed
180 181
%pause;
%close all;