demo_iterativeLQR01.m 8.2 KB
Newer Older
Sylvain Calinon's avatar
Sylvain Calinon committed
1
function demo_iterativeLQR01
2 3 4
% Controller retrieval through an iterative solution of linear quadratic optimal control (finite horizon, 
% unconstrained linear MPC), by relying on a Gaussian mixture model (GMM) encoding of position and velocity data, 
% including comparison with batch LQR.
Sylvain Calinon's avatar
Sylvain Calinon committed
5
%
6 7 8 9
% 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
10
%
11
% @article{Calinon16JIST,
Sylvain Calinon's avatar
Sylvain Calinon committed
12
%   author="Calinon, S.",
13 14
%   title="A Tutorial on Task-Parameterized Movement Learning and Retrieval",
%   journal="Intelligent Service Robotics",
15 16 17 18 19 20
%		publisher="Springer Berlin Heidelberg",
%		doi="10.1007/s11370-015-0187-9",
%		year="2016",
%		volume="9",
%		number="1",
%		pages="1--29"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
% }
% 
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon (http://calinon.ch/) and Danilo Bruno (danilo.bruno@iit.it)
% 
% 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/>.
Sylvain Calinon's avatar
Sylvain Calinon committed
39 40 41

addpath('./m_fcts/');

42

Sylvain Calinon's avatar
Sylvain Calinon committed
43 44 45 46
%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
model.nbStates = 5; %Number of Gaussians in the GMM
model.nbVarPos = 2; %Dimension of position data (here: x1,x2)
Sylvain Calinon's avatar
Sylvain Calinon committed
47
model.nbDeriv = 2; %Number of static and dynamic features (nbDeriv=2 for [x,dx] and u=ddx)
Sylvain Calinon's avatar
Sylvain Calinon committed
48 49
model.nbVar = model.nbVarPos * model.nbDeriv; %Dimension of state vector
model.dt = 0.01; %Time step duration
Sylvain Calinon's avatar
Sylvain Calinon committed
50 51 52 53
model.rfactor = 0.1 * model.dt^model.nbDeriv;	%Control cost in LQR
nbSamples = 3; %Number of demonstrations
nbRepros = 5; %Number of reproductions
nbData = 200; %Number of datapoints
Sylvain Calinon's avatar
Sylvain Calinon committed
54 55 56 57 58

%Control cost matrix
R = eye(model.nbVarPos) * model.rfactor;


Sylvain Calinon's avatar
Sylvain Calinon committed
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
%% Dynamical System settings (discrete version)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% %Integration with Euler method 
% Ac1d = diag(ones(model.nbDeriv-1,1),1); 
% Bc1d = [zeros(model.nbDeriv-1,1); 1];
% A = kron(eye(model.nbDeriv)+Ac1d*model.dt, eye(model.nbVarPos)); 
% B = kron(Bc1d*model.dt, eye(model.nbVarPos));

%Integration with higher order Taylor series expansion
A1d = zeros(model.nbDeriv);
for i=0:model.nbDeriv-1
	A1d = A1d + diag(ones(model.nbDeriv-i,1),i) * model.dt^i * 1/factorial(i); %Discrete 1D
end
B1d = zeros(model.nbDeriv,1); 
for i=1:model.nbDeriv
	B1d(model.nbDeriv-i+1) = model.dt^i * 1/factorial(i); %Discrete 1D
end
A = kron(A1d, eye(model.nbVarPos)); %Discrete nD
B = kron(B1d, eye(model.nbVarPos)); %Discrete nD

% %Conversion with control toolbox
% Ac1d = diag(ones(model.nbDeriv-1,1),1); %Continuous 1D
% Bc1d = [zeros(model.nbDeriv-1,1); 1]; %Continuous 1D
% Cc1d = [1, zeros(1,model.nbDeriv-1)]; %Continuous 1D
% sysd = c2d(ss(Ac1d,Bc1d,Cc1d,0), model.dt);
% A = kron(sysd.a, eye(model.nbVarPos)); %Discrete nD
% B = kron(sysd.b, eye(model.nbVarPos)); %Discrete nD


89
%% Load handwriting data
Sylvain Calinon's avatar
Sylvain Calinon committed
90
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
load('data/2Dletters/G.mat');
Sylvain Calinon's avatar
Sylvain Calinon committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
Data=[];
for n=1:nbSamples
	s(n).Data=[];
	for m=1:model.nbDeriv
		if m==1
			dTmp = spline(1:size(demos{n}.pos,2), demos{n}.pos, linspace(1,size(demos{n}.pos,2),nbData)); %Resampling
		else
			dTmp = gradient(dTmp) / model.dt; %Compute derivatives
		end
		s(n).Data = [s(n).Data; dTmp];
	end
	Data = [Data s(n).Data]; 
end


%% Learning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%model = init_GMM_kmeans(Data,model);
Sylvain Calinon's avatar
Sylvain Calinon committed
110 111 112 113 114 115 116 117 118 119 120 121
model = init_GMM_kbins(Data,model,nbSamples);

% %Initialization based on position data
% model0 = init_GMM_kmeans(Data(1:model.nbVarPos,:), model);
% [~, GAMMA2] = EM_GMM(Data(1:model.nbVarPos,:), model0);
% model.Priors = model0.Priors;
% for i=1:model.nbStates
% 	model.Mu(:,i) = Data * GAMMA2(i,:)';
% 	DataTmp = Data - repmat(model.Mu(:,i),1,nbData*nbSamples);
% 	model.Sigma(:,:,i) = DataTmp * diag(GAMMA2(i,:)) * DataTmp';
% end

Sylvain Calinon's avatar
Sylvain Calinon committed
122 123 124 125 126 127
%Refinement of parameters
[model, H] = EM_GMM(Data, model);
%Set list of states according to first demonstration (alternatively, an HSMM can be used)
[~,qList] = max(H(:,1:nbData),[],1); %works also for nbStates=1


Sylvain Calinon's avatar
Sylvain Calinon committed
128
%% Iterative LQR reproduction (finite horizon, discrete version)
Sylvain Calinon's avatar
Sylvain Calinon committed
129 130 131
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P = zeros(model.nbVar,model.nbVar,nbData);
P(:,:,end) = inv(model.Sigma(:,:,qList(nbData)));
132
d = zeros(model.nbVar, nbData);
Sylvain Calinon's avatar
Sylvain Calinon committed
133 134 135
for t=nbData-1:-1:1
	Q = inv(model.Sigma(:,:,qList(t)));
	P(:,:,t) = Q - A' * (P(:,:,t+1) * B / (B' * P(:,:,t+1) * B + R) * B' * P(:,:,t+1) - P(:,:,t+1)) * A;
136
	d(:,t) = (A' - A'*P(:,:,t+1) * B / (R + B' * P(:,:,t+1) * B) * B' ) * (P(:,:,t+1) * (A * model.Mu(:,qList(t)) - model.Mu(:,qList(t+1))) + d(:,t+1));
Sylvain Calinon's avatar
Sylvain Calinon committed
137
end
138
%Reproduction with feedback (FB) and feedforward (FF) terms
Sylvain Calinon's avatar
Sylvain Calinon committed
139
for n=1:nbRepros
Sylvain Calinon's avatar
Sylvain Calinon committed
140
	X = Data(:,1) + [randn(model.nbVarPos,1)*2E0; zeros(model.nbVar-model.nbVarPos,1)];
141
	r(n).X0 = X;
Sylvain Calinon's avatar
Sylvain Calinon committed
142
	for t=1:nbData
143 144 145 146 147
		r(n).Data(:,t) = X; %Log data
		K = (B' * P(:,:,t) * B + R) \ B' * P(:,:,t) * A; %FB term
		M = -(B' * P(:,:,t) * B + R) \ B' * (P(:,:,t) * (A * model.Mu(:,qList(t)) - model.Mu(:,qList(t))) + d(:,t)); %FF term
		u = K * (model.Mu(:,qList(t)) - X) + M; %Acceleration command with FB and FF terms
		X = A * X + B * u; %Update of state vector
Sylvain Calinon's avatar
Sylvain Calinon committed
148 149 150 151
	end
end


152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
%% Batch LQR reproduction (for comparison)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Create single Gaussian N(MuQ,SigmaQ) based on optimal state sequence q, see Eq. (27)
MuQ = reshape(model.Mu(:,qList), model.nbVar*nbData, 1); 
SigmaQ = (kron(ones(nbData,1), eye(model.nbVar)) * reshape(model.Sigma(:,:,qList), model.nbVar, model.nbVar*nbData)) .* kron(eye(nbData), ones(model.nbVar));
%Build Sx and Su matrices for batch LQR, see Eq. (35)
Su = zeros(model.nbVar*nbData, model.nbVarPos*(nbData-1));
Sx = kron(ones(nbData,1),eye(model.nbVar)); 
M = B;
for n=2:nbData
	id1 = (n-1)*model.nbVar+1:nbData*model.nbVar;
	Sx(id1,:) = Sx(id1,:) * A;
	id1 = (n-1)*model.nbVar+1:n*model.nbVar; 
	id2 = 1:(n-1)*model.nbVarPos;
	Su(id1,id2) = M;
	M = [A*M(:,1:model.nbVarPos), M];
end
%Set matrices to compute the damped weighted least squares estimate, see Eq. (37)
SuInvSigmaQ = Su' / SigmaQ;
Rq = SuInvSigmaQ * Su + kron(eye(nbData-1),R);
%Reproductions
for n=1:nbRepros
	X = r(n).X0;
 	rq = SuInvSigmaQ * (MuQ-Sx*X);
 	u = Rq \ rq; 
	r2(n).Data = reshape(Sx*X+Su*u, model.nbVar, nbData);
end


Sylvain Calinon's avatar
Sylvain Calinon committed
181 182
%% Plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 184 185 186 187 188 189
figure('position',[10 10 1300 500],'color',[1 1 1]); 
%Plot position
subplot(1,2,1); hold on; 
plotGMM(model.Mu(1:2,:), model.Sigma(1:2,1:2,:), [0.5 0.5 0.5], .3);
for n=1:nbSamples
	plot(Data(1,(n-1)*nbData+1:n*nbData), Data(2,(n-1)*nbData+1:n*nbData), '-','color',[.7 .7 .7]);
end
Sylvain Calinon's avatar
Sylvain Calinon committed
190
for n=1:nbRepros
191 192
	h(1) = plot(r(n).Data(1,:), r(n).Data(2,:), '-','linewidth',2,'color',[.8 0 0]); %Reproduction with iterative LQR
	h(2) = plot(r2(n).Data(1,:), r2(n).Data(2,:), '--','linewidth',2,'color',[0 .8 0]); %Reproduction with batch LQR
Sylvain Calinon's avatar
Sylvain Calinon committed
193 194
end
axis equal; 
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
xlabel('x_1'); ylabel('x_2');
legend(h,'Iterative LQR','Batch LQR');

%Plot velocity
subplot(1,2,2); hold on; 
plotGMM(model.Mu(3:4,:), model.Sigma(3:4,3:4,:), [0.5 0.5 0.5], .3);
for n=1:nbSamples
	plot(Data(3,(n-1)*nbData+1:n*nbData), Data(4,(n-1)*nbData+1:n*nbData), '-','color',[.7 .7 .7]);
end
for n=1:nbRepros
	plot(r(n).Data(3,:), r(n).Data(4,:), '-','linewidth',2,'color',[.8 0 0]); %Reproduction with iterative LQR
	plot(r(n).Data(3,1), r(n).Data(4,1), '.','markersize',18,'color',[.6 0 0]);
	plot(r2(n).Data(3,:), r2(n).Data(4,:), '--','linewidth',2,'color',[0 .8 0]); %Reproduction with batch LQR
end
plot(0,0,'k+');
axis equal;
xlabel('dx_1'); ylabel('dx_2');

Sylvain Calinon's avatar
Sylvain Calinon committed
213 214

%print('-dpng','graphs/demo_iterativeLQR01.png');
215 216
%pause;
%close all;
Sylvain Calinon's avatar
Sylvain Calinon committed
217