%CPNNDEMO	The example of CPNN algorithm.
%
%    Description
%    The cpnn has a network structure similar to Modha's neural network.
%    But is trained in a supervised manner. The real lable distributions
%    are known when training the neural network. Then a new distribution 
%    can be predicted based on the cpnn structure.
%
%    Statement
%    CPNN is only suitable for totally ordered labels(such as the age).
%    And it requires that the label must have numerical significance.
%    Thus it cannot be applied to the general LDL problem.
%
%    See also
%    CPNN, CPNNTRAIN, CPNNPREDICT
%
%    Copyright: Xin Geng (xgeng@seu.edu.cn)
%    School of Computer Science and Engineering, Southeast University
%    Nanjing 211189, P.R.China
 
clear;
clc;
% Load the trainData and cpnnTestData.
load trainData;
load cpnnTestData;

% Set parameters in cpnn structure.
cpnnStructure.hNumber = 50; % the number of hidden layer, default: 50.
cpnnStructure.iNumber = 262; % the number of input layer, default: 262.
cpnnStructure.epochs = 100; % the number of iteration times, default: 100.
cpnnStructure.goal = 5 ; % accurate to five decimal places, default: 5.
cpnnStructure.showResult = true; %whether show the result. True for show, false for not.

% Bulid cpnn structure
cpnnStructure=cpnn(cpnnStructure);

% Set parameters in the process of training cpnn mode.
para.itaP = 1.2;
para.itaN = 0.5;

% Train the cpnn structure.
mode=cpnntrain(trainFeature,trainDistribution,cpnnStructure,para);

prediction = [];
for i=1:testNum
    testfeature = testFeature(i,:);
    % Use trained structure to predict the distribution.
    distribution=cpnnpredict(testfeature,mode);
    % Show the comparisons between the real distribution and the predicted distribution.
    dist(:,1)=kldist(testDistribution(i,:),distribution);
    dist(:,2)=euclideandist(testDistribution(i,:),distribution);  
    dist(:,3)=sorensendist(testDistribution(i,:),distribution);
    dist(:,4)=squaredxdist(testDistribution(i,:),distribution);
    dist(:,5)=fidelity(testDistribution(i,:),distribution);
    dist(:,6)=intersection(testDistribution(i,:),distribution);
    % Draw the picture of the real and prediced distribution.
    drawdistribution(testDistribution(i,:),distribution,dist);
    sign=input('Press any key to continue:');
end
