%%% Monte Carlo Dynamic Classifier tool - MCDC tool
% http://www.ismvideo.org/NDE0UP/MCDCtool/en/index.html

% Copyright (C) 2014 Gareth W. Peters, Tomoko Matsui, Francois Septier, and Akira Tamamori

% This program is free software; you can redistribute it and/or modify it under the terms
% of the GNU General Public License as published by the Free Software Foundation; either
% version 2 of the License, or (at your option) any later version.

% This program 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 this
% program; if not, see <http://www.gnu.org/licenses/>.

%
% MotionCapturePMCMC2Estimation.m
%

rng('shuffle');

%
% Training Settings
%

grids = { ...
  [-30:2:30], ...
  [-30:2:30]  ...
};

stateKernelGens = { ...
  RBFKernelGenerator(UniformDistribution(0.01, 10), UniformDistribution(0.01, 10)), ...
  RBFKernelGenerator(UniformDistribution(0.01, 10), UniformDistribution(0.01, 10))  ...
};

obsKernelGens = {
  RBFKernelGenerator(UniformDistribution(0.01, 10), UniformDistribution(0.01, 10)), ...
  RBFKernelGenerator(UniformDistribution(0.01, 10), UniformDistribution(0.01, 10)), ...
  RBFKernelGenerator(UniformDistribution(0.01, 10), UniformDistribution(0.01, 10))  ...
};

stateMeanFuncs = { ...
  @(x) x(1, :), ...
  @(x) x(2, :)  ...
};

obsMeanFuncs = { ...
  @(x) zeros(1, size(x, 2)), ...
  @(x) zeros(1, size(x, 2)), ...
  @(x) zeros(1, size(x, 2))  ...
};

dt = 1.0;
x0 = zeros(1, 2);
N  = 500;
J  = 200;

kernelGeneratorStrategy = @RBFKernelGeneratorStrategyChoice2;
mcdcStrategy            = @MCDCStrategyChoice2;
gridDimForGramMatrix    = 1;
splineHandle            = @GenericSpline;

%
% Load state and observation time series from motion capture data
%

files = dir('data/amc_files/*.amc');

for file = files'

  amcFile = ['data/amc_files/' file.name];
  amcId = strsplit(file.name, '.');
  amcId = amcId(1);

  [x, y] = MotionCapture(amcFile, 1:3, 1:3);
  aspect = Aspect();
  aspect.LogFileName = strjoin(['logs/' amcId '.log'], '');
  aspect.MatFileNamePrefix = strjoin(['logs/' amcId], '');
  aspect.SavesIntermediateMat = true;
  aspect.IntermediateMatInterval = 100;

  disp(aspect.LogFileName);
  disp(aspect.MatFileNamePrefix);

  [ IDX, SKP, OKP, FV, GV, XE, YE, loglik ] = MCDCTrain( ...
    PMCMC2(kernelGeneratorStrategy, mcdcStrategy), ...
    grids, ...
    stateKernelGens, ...
    obsKernelGens, ...
    stateMeanFuncs, ...
    obsMeanFuncs, ...
    gridDimForGramMatrix, ...
    splineHandle, ...
    x0, ...
    [], ... % No auxiliary states
    [], ... % No controls
    y', ...
    N, ...
    J, ...
    0, ...
    aspect ...
  );

  save(strjoin(['logs/' amcId '.mat'], ''));

end

quit;
