%
% Copyright (c) 2011, Kai Guo, Prakash Ishwar, and Janusz Konrad. All
% rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% 1) Redistributions of source code must retain the above copyright
%    notice, this list of conditions and the following disclaimer.
%
% 2) Redistributions in binary form must reproduce the above copyright
%    notice, this list of conditions and the following disclaimer in the
%    documentation and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
% HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
% LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
% DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
% THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%

% main function for action classification
clear
clc

load sample_data.mat

Final=cell(9,10);% store the classified label of each test segment

for name_test=1:9
    name_test
    for act_test=1:10
        Cov_test=Cov_dataset_all{name_test,act_test};
        CC=zeros(1,size(Cov_test,3));
        
        %generate A matrix:
        A=[];
        Class=[];
        for j=1:10
            for i=1:9
                if (i-name_test)^2+(j-act_test)^2~=0
                   TrainCov=Cov_dataset_all{i,j};
                   for kk=1:size(TrainCov,3)
                       temp1=TrainCov(:,:,kk);
                       [UU,DD]=eig(temp1);
                       temp2=UU*diag(log(diag(DD)))*UU';
                       A=[A,Cov2Vec(temp2)];
                       Class=[Class,j];
                   end
                end
            end
        end
        L=size(A,2);
        
        % action recognition:
        for k=1:size(Cov_test,3)
            temp1=Cov_test(:,:,k);
            [UU,DD]=eig(temp1);
            temp2=UU*diag(log(diag(DD)))*UU';
            y=Cov2Vec(temp2);
            
%===============cplex=========================

             ep=0.01*ones(length(y),1);
             B=[A,-A];
             c=ones(size(B,2),1);
             % Build model
             cplex = Cplex('action');
             cplex.Model.sense = 'minimize';
             cplex.Model.obj   = c;
             cplex.Model.lb    = zeros(size(B,2),1);
             cplex.Model.ub    = 1000000*ones(size(B,2),1);
             cplex.Model.lhs   = y-ep;
             cplex.Model.rhs   = y+ep;
             cplex.Model.A     = B;
             cplex.Param.lpmethod.Cur=1;
             cplex.DisplayFunc=1;
             % Solve model
             cplex.solve();
             x=cplex.Solution.x;
             s=x(1:length(x)/2)-x(length(x)/2+1:end);

%======================================================

            err=zeros(1,10);%error of each class
            for kk=1:10
                ss=zeros(length(s),1);
                index=find(Class==kk);
                ss(index,1)=s(index,1);
                yy=A*ss;
                err(kk)=norm(yy-y);
            end
            [pp,qq]=min(err);
            CC(k)=qq;
            
        end
        Final{name_test,act_test}=CC;
    end
end

%%%%%%%%
