%% This script is to sample a 20-step routing experts and calculate the expectation 
clear all;
addpath C:\Users\huansun\Documents\InternshipAtIBM\EntireModelOfTicketResolution\
globalvars_edgefile;



%load K3model_normtktvec_localvolum_onlyedgeintrain_edgefileEXPORDER_alphauniforminit_meantransinit.mat;
%load 'K3model_onlyedgeintrain_correcttest_fixbetaByResln_1_15_alphauniforminit_iter1000.mat';  %% All the variables saved for the mixture model with the new RA
   
load 'K6model_onlyedgeintrain_iter1000.mat'  %% your new trained parameters for 

rand('state',100);

%% Calculate the routing steps in training data



fprintf('Finish predicting in the training dataset....\n');

%load 'AIXTestTransferFile_Corrected';
 load 'PrunedNewTestTransferFile_AIX';  %% load your transfer data in TestTransferFile 
 testtransferfile=newtesttransferfile;

% load 'PrunedSingleEdge_testtransferfile';
% 
% testtransferfile=prunedtesttransferfile;

transferfile=load('C:\Users\huansun\Documents\InternshipAtIBM\NetworkSimulation\NetworkSimulation\AIXDataForMudhakar\TransferReceiverPair_Edge.txt');

testtktvector=load('C:\Users\huansun\Documents\InternshipAtIBM\NetworkSimulation\NetworkSimulation\AIXDataForMudhakar\FinalRemZeroAllTestingVectorTop2K.txt');

fprintf('Now begins estimating the routing steps in training data:\n');

testrealtktroutinglen=[];
testpredrouteseq=[];
testpredroutelen=[];

testtranstkts=unique(testtransferfile(:,3));


testtktvector=testtktvector./repmat(sqrt(sum(testtktvector.^2,2)),1,veclen);

%Tktvectors=load('TicketsContent_wordvector.txt'); % 10462*2000 matrix 
%Tktvectors=Tktvectors./repmat(sqrt(sum(Tktvectors.^2,2)),1,veclen);

Expvectorsnew=Expvectors;
M0=size(Expvectors,2);
gammapriornew=gammaprior;


TktClassAsso=[testtktvector ones(size(testtktvector,1),1)]*w_class(1:end-M0,:);
ExpClassAsso=Expvectorsnew*w_class(end-M0+1:end,:);

maxtime=50;  %% you can change the maxtime to 100 to do 100 times
maxstep=10;
finalreslen=[];
finalresseq=[];
finalpredprob=[];
finalpredtransprob=[];
tktnum=[];

for j=1:maxtime
    testpredrouteseq=[];
    testpredroutelen=[];
    testrealtktroutinglen=[];
    testpredseqprob=[];
    
    testpredseqtransprob=[];
    
for i=1:numel(testtranstkts)
  % for i=1523
   
   
     fprintf('The %d -th ticket\n',i);
     tempexp=find(testtransferfile(:,3)==testtranstkts(i));
    % inflag=find(expertsintrain==testtransferfile(tempexp(1),1));
   
    if (testtranstkts(i)>0 && numel(find(ExpTransTktFile(:,1)==testtransferfile(tempexp(1),1))) ~=0)

        posi=find(testtransferfile(:,3)==testtranstkts(i));
        testrealtktroutinglen=[testrealtktroutinglen;numel(posi)+1];
    
      
      
 templength=predroutingseq_sixall(testtransferfile(posi(1),1),testtranstkts(i),TktClassAsso,ExpClassAsso,maxstep,gammapriornew,testtktvector);

        
      % templength=predroutingseq_nooutmodi_omitexp_maxopt_edgefile_byMaxPrior(testtransferfile(posi(1),1),testtranstkts(i),TktClassAsso,ExpClassAsso,maxstep,gammapriornew,testtktvector);
              
        if (numel(templength)==0)
            disp('No Estimation!! Something is WRONG!')
            break;
        end
        [templength numel(posi)+1]
        
        testpredroutelen=[testpredroutelen;templength];

        tktnum=[tktnum;testtranstkts(i) ];
        
        
    end
  
end
finalreslen=[finalreslen testpredroutelen];

fprintf('the %d time\n',j);
end






disp('all, basic mixture, no more iter');




posi=find(testrealtktroutinglen>1);
Totalnum=numel(posi);

disp('MAE and standard deviation: ');
MAE=[mean(abs(round(sum(finalreslen(posi,:),2)/maxtime)-testrealtktroutinglen(posi))) std(abs(round(sum(finalreslen(posi,:),2)/maxtime)-testrealtktroutinglen(posi)))] 
%[mean(abs(round(sum(finalreslen(posi,:),2)/maxtime)-testrealtktroutinglen(posi))./testrealtktroutinglen(posi)) std(abs(round(sum(finalreslen(posi,:),2)/maxtime)-testrealtktroutinglen(posi))./testrealtktroutinglen(posi))]
disp('step loss: ')
SL=numel(find(abs(round(sum(finalreslen(posi,:),2)/maxtime)-testrealtktroutinglen(posi))>1))./numel(posi)


