demo_DSGMR01.m 4.46 KB
Newer Older
1
function demo_DSGMR01
2 3
% Gaussian mixture model (GMM), with Gaussian mixture regression(GMR) and dynamical systems 
% used for reproduction, with decay variable used as input (as in DMP).
Sylvain Calinon's avatar
Sylvain Calinon committed
4
%
5 6 7 8
% Writing code takes time. Polishing it and making it available to others takes longer! 
% If some parts of the code were useful for your research of for a better understanding 
% of the algorithms, please reward the authors by citing the related publications, 
% and consider making your own research available in this way.
Sylvain Calinon's avatar
Sylvain Calinon committed
9
%
10
% @article{Calinon16JIST,
Sylvain Calinon's avatar
Sylvain Calinon committed
11
%   author="Calinon, S.",
12 13
%   title="A Tutorial on Task-Parameterized Movement Learning and Retrieval",
%   journal="Intelligent Service Robotics",
14 15 16 17 18 19
%		publisher="Springer Berlin Heidelberg",
%		doi="10.1007/s11370-015-0187-9",
%		year="2016",
%		volume="9",
%		number="1",
%		pages="1--29"
Sylvain Calinon's avatar
Sylvain Calinon committed
20
% }
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
% 
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
% 
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
% 
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
% 
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
38 39 40

addpath('./m_fcts/');

Sylvain Calinon's avatar
Sylvain Calinon committed
41

42 43 44 45 46
%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
model.nbStates = 5; %Number of states in the GMM
model.nbVar = 3; %Number of variables [t,x1,x2]
nbData = 200; %Length of each trajectory
Sylvain Calinon's avatar
Sylvain Calinon committed
47
model.dt = 0.01; %Time step
48
nbSamples = 5; %Number of demonstrations
Sylvain Calinon's avatar
Sylvain Calinon committed
49
model.kP = 50; %Stiffness gain
50 51
model.kV = (2*model.kP)^.5; %Damping gain (with ideal underdamped damping ratio)
model.alpha = 1.0; %Decay factor
Sylvain Calinon's avatar
Sylvain Calinon committed
52 53 54
nbVarOut = model.nbVar-1; %Dimension of spatial variables
L = [eye(nbVarOut)*model.kP, eye(nbVarOut)*model.kV]; %Feedback term

55

56
%% Load handwriting data
57 58 59 60 61
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sIn(1) = 1; %Initialization of decay term
for t=2:nbData
	sIn(t) = sIn(t-1) - model.alpha * sIn(t-1) * model.dt; %Update of decay term (ds/dt=-alpha s)
end
Sylvain Calinon's avatar
Sylvain Calinon committed
62 63 64 65 66 67
% %Create transformation matrix to compute xhat = x + dx*kV/kP + ddx/kP
% K1d = [1, model.kV/model.kP, 1/model.kP];
% K = kron(K1d,eye(nbVarOut));
% %Create transformation matrix to compute [X; DX; DDX]
% D = (diag(ones(1,nbData-1),-1)-eye(nbData)) / model.dt;
% D(end,end) = 0;
68

69
load('data/2Dletters/G.mat');
Sylvain Calinon's avatar
Sylvain Calinon committed
70
Data=[]; Data0=[];
71
for n=1:nbSamples
Sylvain Calinon's avatar
Sylvain Calinon committed
72 73 74 75 76
	DataTmp = spline(1:size(demos{n}.pos,2), demos{n}.pos, linspace(1,size(demos{n}.pos,2),nbData)); %Resampling
	%s(n).Data = K * [DataTmp; DataTmp*D; DataTmp*D*D];
	s(n).Data = DataTmp;
	Data = [Data [sIn; s(n).Data]]; %Training data as [s;xhat]
	Data0 = [Data0 [sIn; DataTmp]]; %Original data as [s;x]
77 78 79 80 81 82 83 84
end


%% Learning and reproduction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%model = init_GMM_timeBased(Data, model);
model = init_GMM_kmeans(Data, model);
model = EM_GMM(Data, model);
Sylvain Calinon's avatar
Sylvain Calinon committed
85
[currTar, currSigma] = GMR(model, sIn, 1, 2:model.nbVar); %see Eq. (17)-(19)
86 87 88 89 90
%Motion retrieval with spring-damper system
x = Data(2:model.nbVar,1);
dx = zeros(nbVarOut,1);
for t=1:nbData
	%Compute acceleration, velocity and position
Sylvain Calinon's avatar
Sylvain Calinon committed
91
	ddx = L * [currTar(:,t)-x; -dx]; 
92 93 94 95 96
	dx = dx + ddx * model.dt;
	x = x + dx * model.dt;
	DataOut(:,t) = x;
end

Sylvain Calinon's avatar
Sylvain Calinon committed
97

98 99 100 101 102 103
%% Plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure('position',[10,10,1000,500]); 
%Plot GMM
subplot(1,2,1); hold on; box on; title('GMM');
plotGMM(model.Mu(2:model.nbVar,:), model.Sigma(2:model.nbVar,2:model.nbVar,:), [.8 0 0]);
Sylvain Calinon's avatar
Sylvain Calinon committed
104
plot(Data0(2,:),Data0(3,:),'.','markersize',8,'color',[.7 .7 .7]);
105 106 107 108
axis equal; set(gca,'Xtick',[]); set(gca,'Ytick',[]);
%Plot DS-GMR
subplot(1,2,2); hold on; box on; title('DS-GMR');
plotGMM(currTar, currSigma, [0 .8 0]);
Sylvain Calinon's avatar
Sylvain Calinon committed
109
plot(Data0(2,:),Data0(3,:),'.','markersize',8,'color',[.7 .7 .7]);
110 111 112 113 114
plot(currTar(1,:),currTar(2,:),'-','linewidth',1.5,'color',[0 .6 0]);
plot(DataOut(1,:),DataOut(2,:),'-','linewidth',3,'color',[0 .3 0]);
axis equal; set(gca,'Xtick',[]); set(gca,'Ytick',[]);

%print('-dpng','graphs/demo_DSGMR01.png');
Sylvain Calinon's avatar
Sylvain Calinon committed
115 116
pause;
close all;