function demo_HSMM01
% Variable duration model implemented as a hidden semi-Markov model
% (simplified version by encoding the state duration after EM).
%
% 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.
%
% @article{Rozo16Frontiers,
% author="Rozo, L. and Silv\'erio, J. and Calinon, S. and Caldwell, D. G.",
% title="Learning Controllers for Reactive and Proactive Behaviors in Human-Robot Collaboration",
% journal="Frontiers in Robotics and {AI}",
% year="2016",
% month="June",
% volume="3",
% number="30",
% pages="1--11",
% doi="10.3389/frobt.2016.00030"
% }
%
% 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 .
addpath('./m_fcts/');
%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
model.nbStates = 2; %Number of hidden states in the HSMM
nbData = 100; %Length of each trajectory
nbSamples = 10; %Number of demonstrations
minSigmaPd = 1E1; %Minimum variance of state duration (regularization term)
%% Load handwriting data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
demos=[];
load('data/2Dletters/V.mat');
Data=[];
for n=1:nbSamples
s(n).Data = spline(1:size(demos{n}.pos,2), demos{n}.pos, linspace(1,size(demos{n}.pos,2),nbData)); %Resampling
s(n).nbData = size(s(n).Data,2);
Data = [Data s(n).Data];
end
%% Learning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%model = init_GMM_kmeans(Data, model);
model = init_GMM_kbins(Data, model, nbSamples);
% %Random initialization
% model.Trans = rand(model.nbStates,model.nbStates);
% model.Trans = model.Trans ./ repmat(sum(model.Trans,2),1,model.nbStates);
% model.StatesPriors = rand(model.nbStates,1);
% model.StatesPriors = model.StatesPriors/sum(model.StatesPriors);
%Left-right model initialization
model.Trans = zeros(model.nbStates);
for i=1:model.nbStates-1
model.Trans(i,i) = 1-(model.nbStates/nbData);
model.Trans(i,i+1) = model.nbStates/nbData;
end
model.Trans(model.nbStates,model.nbStates) = 1.0;
model.StatesPriors = zeros(model.nbStates,1);
model.StatesPriors(1) = 1;
model.Priors = ones(model.nbStates,1);
[model, H] = EM_HMM(s, model);
%Removal of self-transition (for HSMM representation) and normalization
model.Trans = model.Trans - diag(diag(model.Trans)) + eye(model.nbStates)*realmin;
model.Trans = model.Trans ./ repmat(sum(model.Trans,2),1,model.nbStates);
%% Post-estimation of the state duration from data (for HSMM representation)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:model.nbStates
st(i).d=[];
end
[~,hmax] = max(H);
currState = hmax(1);
cnt = 1;
for t=1:length(hmax)
if (hmax(t)==currState)
cnt = cnt+1;
else
st(currState).d = [st(currState).d cnt];
cnt = 1;
currState = hmax(t);
end
end
st(currState).d = [st(currState).d cnt];
%Compute state duration as Gaussian distribution
for i=1:model.nbStates
model.Mu_Pd(1,i) = mean(st(i).d);
model.Sigma_Pd(1,1,i) = cov(st(i).d) + minSigmaPd;
end
%% Reconstruction of states probability sequence
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nbD = round(2 * nbData/model.nbStates); %Number of maximum duration step to consider in the HSMM (2 is a safety factor)
%Precomputation of duration probabilities
for i=1:model.nbStates
model.Pd(i,:) = gaussPDF([1:nbD], model.Mu_Pd(:,i), model.Sigma_Pd(:,:,i));
%The rescaling formula below can be used to guarantee that the cumulated sum is one (to avoid the numerical issues)
model.Pd(i,:) = model.Pd(i,:) / sum(model.Pd(i,:));
end
%Slow reconstruction of states sequence based on standard computation
%(in the iteration, a scaling factor c is used to avoid numerical underflow issues in HSMM, see Levinson'1986)
h = zeros(model.nbStates,nbData);
c = zeros(nbData,1); %scaling factor to avoid numerical issues
c(1)=1; %Initialization of scaling factor
for t=1:nbData
for i=1:model.nbStates
if t<=nbD
% oTmp = 1; %Observation probability for generative purpose
oTmp = prod(c(1:t) .* gaussPDF(s(1).Data(:,1:t), model.Mu(:,i), model.Sigma(:,:,i))); %Observation probability for standard HSMM
h(i,t) = model.StatesPriors(i) * model.Pd(i,t) * oTmp;
end
for d=1:min(t-1,nbD)
% oTmp = 1; %Observation probability for generative purpose
oTmp = prod(c(t-d+1:t) .* gaussPDF(s(1).Data(:,t-d+1:t), model.Mu(:,i), model.Sigma(:,:,i))); %Observation probability for standard HSMM
h(i,t) = h(i,t) + h(:,t-d)' * model.Trans(:,i) * model.Pd(i,d) * oTmp;
end
end
c(t+1) = 1/sum(h(:,t)); %Update of scaling factor
end
h = h ./ repmat(sum(h,1),model.nbStates,1);
% %Fast reconstruction of sequence for HSMM (version based on position and duration information)
% h = zeros(model.nbStates,nbData);
% [bmx, ALPHA, S, h(:,1)] = hsmm_fwd_init_hsum(s(1).Data(:,1), model);
% for t=2:nbData
% [bmx, ALPHA, S, h(:,t)] = hsmm_fwd_step_hsum(s(1).Data(:,t), model, bmx, ALPHA, S);
% end
% %Fast reconstruction of sequence for HSMM (version based on only duration information)
% h = zeros(model.nbStates,nbData);
% [ALPHA, S, h(:,1)] = hsmm_fwd_init_ts(model);
% for t=2:nbData
% [ALPHA, S, h(:,t)] = hsmm_fwd_step_ts(model, ALPHA, S);
% end
% h = h ./ repmat(sum(h,1),model.nbStates,1);
% %Manual reconstruction of sequence for HSMM based on stochastic sampling
% nbSt=0; currTime=0; iList=[];
% h = zeros(model.nbStates,nbData);
% while currTime