% Tested on Windows 7 64bit. Visual Studio 8.0

addpath('../bin/openMP/'); 

seed = 57123;
% load data
load ArmGestureDiscrete.mat;
seqs = cellfun(@(x) downsample(x',2)', seqs, 'UniformOutput', false);
labels = cellfun(@(x) int32(unique(x)), labels);

% split data
Ny = histc(labels,0:5);
idx_train=[]; idx_valid=[]; idx_test=[];
for y=0:5
    Iy = find(labels==y);
    sz = floor(Ny(y+1)/5);
    idx_test = horzcat(idx_test,Iy(1:sz));
    idx_valid = horzcat(idx_valid,Iy(sz+1:2*sz));
    idx_train = horzcat(idx_train,Iy(2*sz+1:end));    
end
splits.seqs_train = seqs(idx_train); splits.labels_train = labels(idx_train);
splits.seqs_valid = seqs(idx_valid); splits.labels_valid = labels(idx_valid);
splits.seqs_test = seqs(idx_test);   splits.labels_test = labels(idx_test);

% For demonstration purpose, we use one fifth of the training split. We
% also do not perform validation of several parameters.
splits.seqs_train   = splits.seqs_train(1:5:end);
splits.labels_train = splits.labels_train(1:5:end);

fprintf('> Sample distribution\n');
disp(tabulate(splits.labels_train));

 
%% Experiment with HCRF
params_hcrf.modelType = 'hcrf';
params_hcrf.optimizer = 'lbfgs';
params_hcrf.nbHiddenStates = 4;
params_hcrf.regFactorL2 = 1.0;
params_hcrf.maxIterOpt = 100; 
params_hcrf.seed = seed;

R_hcrf = experimentHCRF(splits,params_hcrf,2:20);


%% Experiment with Linked HCRF
params_lhcrf.modelType = 'mvhcrf';
params_lhcrf.graphType = 'linked'; 
params_lhcrf.optimizer = 'lbfgs';
params_lhcrf.inferenceEngine = 'JT'; 
params_lhcrf.nbViews = 2;
params_lhcrf.nbHiddenStates = [4 4]; 
params_lhcrf.rawFeatureIndex = {[0:3,8:13], [4:7,14:19]}; % divided into L and R arms
params_lhcrf.regFactorL2 = 1.0;
params_lhcrf.maxIterOpt = 100; 
params_lhcrf.seed = seed;

H = {}; for i=4:8, H{end+1} = [i i]; end
R_lhcrf = experimentLHCRF(splits,params_lhcrf,H);
 