% function [net SP SS AC] = simEpi( P , T, Test, T2 , nnetType, type)
% Create and train a nnet of type 'nnetType'.
% nnetType can be:
%   ffbp -  Feedforward Backpropagation Network
%   rbf -   Radial Basis Network
%   lrn -   Layer Recurrent Network
%   elman - Elman Network
%   fftd -  Feed-forward Time-delay
%   dtd -   Distributed Time-delay
%
% The param type, defines the variables of training depending on nnet
% propose (S - Single Patient, D - Different Patient, M - Multiple Patient)
%
% Copyright 2007-2008 Rui P. Costa (http://student.dei.uc.pt/~racosta)
%
function [net SP SS AC] = simEpi( P , T, Test, T2 , nnetType, type)

pos=findstr(nnetType, ' ');

switch(nnetType(1:(pos(1)-1)))
    case 'ffbp'
        switch (type(1))
            case 'S'
                net = newff(P, T, 15);
                net.trainFcn='trainlm';
                net.trainParam.epochs = 100;
                net.trainParam.goal = 0.00001;
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net = train(net,P,T);
                
                A = sim(net,Test);
                [SP SS AC]=calcPerformance(A,T2);
                
            case 'D'    
                net = newff(P, T, 9);
                net.trainFcn='trainlm';
                net.trainParam.epochs = 100;
                net.trainParam.goal = 0.00001;
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net = train(net,P,T);

                A = sim(net,Test);
                [SP SS AC]=calcPerformance(A,T2);
                
            case 'M'    
                net = newff(P, T, 10);
                net.trainFcn='trainlm';
                net.trainParam.epochs = 100;
                net.trainParam.goal = 0.00001;
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net = train(net,P,T);

                A = sim(net,Test);
                [SP SS AC]=calcPerformance(A,T2);
        end        
        
    case 'lrn' %Recurrent nnet
        switch (type(1))
            case 'S'
                net = newlrn(P, T, 25);
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net.trainFcn = 'trainlm';
                net = train(net,P,T);
                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'D'    
                net = newlrn(P, T, 10);
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net.trainFcn = 'trainlm';
                net = train(net,P,T);
                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'M'    
                net = newlrn(P, T, 10);
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net.trainFcn = 'trainlm';
                net = train(net,P,T);
                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
        end         
          
        
    case 'elman' %Elman nnet
        switch (type(1))
            case 'S'
                net = newelm(P, T, 10);
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net.trainFcn = 'trainlm';
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'D'    
                net = newelm(P, T, 10);
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net.trainFcn = 'trainlm';
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'M'    
                net = newelm(P, T, 10);
                net.divideParam.trainRatio=1;
                net.divideParam.testRatio=0;
                net.divideParam.valRatio=0;
                net.trainFcn = 'trainlm';
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
        end      
        
    case 'rbf'
        switch (type(1))
            case 'S'
                net = newrbe(P,T,1.5);
                net.trainFcn = 'trainlm';
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'D'    
                net = newrbe(P,T,1.5);
                net.trainFcn = 'trainlm';
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'M'    
                net = newrbe(P,T,1.5);
                net.trainFcn = 'trainlm';
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
        end
        
    case 'fftd'
        switch (type(1))
            case 'S'
                net = newfftd(P,T,[0 1],10);
                net = train(net, P, T, [],[],[],[]);
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'D'    
                net = newfftd(P,T,[0 1],15);
                net = train(net, P, T, [],[],[],[]);
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'M'    
                net = newfftd(P,T,[0 1],15);
                net = train(net, P, T, [],[],[],[]);
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
        end
          
        
    case 'dtd'
        switch (type(1))
            case 'S'
                net = newdtdnn(P,T,10, {[0 1]});
                net = train(net, P, T, [],[],[],[]);
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'D'    
                net = newdtdnn(P,T,12, {[0 1]});
                net = train(net, P, T, [],[],[],[]);
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
                
            case 'M'    
                net = newdtdnn(P,T,15, {[0 1]});
                net = train(net, P, T, [],[],[],[]);
                net = train(net,P,T);

                A = sim(net,Test);

                [SP SS AC]=calcPerformance(A,T2);
        end
end
