%% This file is to load various data 

addpath AIXDataForMudhakar\;
globalvars_edgefile;

fprintf('Loading data....\n');
%% load transfer file, A transfer Tkt to B,  as A Tkt B; ExpTransTktFile

load 'AIXTrainTransferFile_Corrected_Full';

% % load 'AIX_Tktvectors_tfidf';
% % 
% % Tktvectors=Tktvectors_tfidf;
% % 

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



veclen=size(Tktvectors,2);


posposi=find(ExpTransTktFileFull(:,3)>0);
ExpTransTktFile0=ExpTransTktFileFull(posposi,:);
NumExperts0=numel(unique(ExpTransTktFile0(:,1)));

%load('C:\Users\huansun\Documents\InternshipAtIBM\NetworkSimulation\NetworkSimulation\AIXDataForMudhakar\AIXTestTransferFile');% load testtransferfile
load AIXTestTransferFile_Corrected;


posi=find(testtransferfile(:,3)>0);
ExpTransTktFile=[ExpTransTktFile0;testtransferfile(posi,:)];
NumExperts=max(max(ExpTransTktFile(:,1:2)));

% We compute volumetran:
Edge=unique(ExpTransTktFile(:,1:2),'rows');

EdgeConn=zeros(NumExperts,NumExperts);

weightedEdge=zeros(NumExperts,NumExperts);

%EdgeKnowledge=sparse(NumExperts*NumExperts,veclen);

EdgeKnowledge=randn(size(Edge,1),veclen); % all the edges
EdgeKnowIndx=zeros(NumExperts*NumExperts,1);

for i=1:size(Edge,1)
    weight=sum(ExpTransTktFile0(:,1)==Edge(i,1) & ExpTransTktFile0(:,2)==Edge(i,2));
    %weightedEdge=[weightedEdge; [Edge(i,:) weight]];
    weightedEdge(Edge(i,1),Edge(i,2))=weight;
    if (weight>0)
    EdgeConn(Edge(i,1),Edge(i,2))=1;
    end
    
    
     EdgeKnowIndx((Edge(i,2)-1)*NumExperts+Edge(i,1))=i;
     
     tempposi1=find(ExpTransTktFile0(:,1)==Edge(i,1) & ExpTransTktFile0(:,2)==Edge(i,2));
     
     if (numel(tempposi1)>0)
     EdgeKnowledge(i,:)=mean(Tktvectors(ExpTransTktFile0(tempposi1,3),:),1);
     end
     
end

ExpTransTktFile=ExpTransTktFile0;
%weightedEdgenew=sparse(weightedEdge(:,1), weightedEdge(:,2),  weightedEdge(:,3), NumExperts, NumExperts, size(weightedEdge(:,1),1)+100);


posi=find(sum(weightedEdge,2));
volumtran=weightedEdge;
volumtran(posi,:)=volumtran(posi,:)./(sum(volumtran(posi,:),2)*ones(1,NumExperts));


totalvolume=sum(weightedEdge);

weightedEdgenew=EdgeConn.*repmat(totalvolume,NumExperts,1);

posi=find(sum(weightedEdgenew,2));
weightedEdgenew(posi,:)=weightedEdgenew(posi,:)./(sum(weightedEdgenew(posi,:),2)*ones(1,size(weightedEdgenew,2))); %% This is global volum



PureRandTrans=EdgeConn;
tempposi=find(sum(EdgeConn,2));
PureRandTrans(tempposi,:)=EdgeConn(tempposi,:)./(sum(EdgeConn(tempposi,:),2)*ones(1,NumExperts));



%% load Tktvectors, NumTkt * 2000 matrix ; for all transferred tickets; pay attention to the index
%Tktvectors=load('TicketsContent_wordvector.txt'); % 10462*2000 matrix 



%% load Expvectors, NumExp * 2000 matrix; for all experts, first estimate its expertise. first use our previous model to estimate 
Expvectors=rand(NumExperts,2000);



%% arrange ticket vectors for prob estimation TktSeqVecWithOne and target

negposi=find(ExpTransTktFileFull(:,3)<0);
Trainseq0=[ExpTransTktFile(:,3); unique(ExpTransTktFile(:,3));-ExpTransTktFileFull(negposi,3)];


[Trainseq, indx]=sort(Trainseq0,'ascend');

target=zeros(size(Trainseq,1),2);
posi=find(Trainseq(2:end)-Trainseq(1:end-1)~=0);

target(posi,1)=1;
target(end,1)=1;
target(:,2)=1-target(:,1);

SeqExpertMapping=[];

ExpTransTktFileFull0=ExpTransTktFileFull;

ExpTransTktFileFull0(negposi,3)=-ExpTransTktFileFull0(negposi,3);
tktseq=unique(ExpTransTktFileFull0(:,3));
for i=1:size(tktseq,1)
    posi=find(ExpTransTktFileFull(:,3)==tktseq(i));
    
    if (numel(posi)>0)
    SeqExpertMapping=[SeqExpertMapping;ExpTransTktFileFull(posi,1);ExpTransTktFileFull(posi(end),2)];
    else
       posi=find(ExpTransTktFileFull(:,3)==-tktseq(i)); 
       if (numel(posi)>0)
       SeqExpertMapping=[SeqExpertMapping;ExpTransTktFileFull(posi,1)];
       else
       disp('wrong infomation!!!')
       break;
       end
    
    end
    
end


TktSeqVecWithOne=[Tktvectors(Trainseq,:) ones(size(Trainseq))];



fprintf('*********Load data done!*********\n');


