%% This script is to initiate the model


globalvars_edgefile

fprintf('Initializing model....\n');

phi=[];

betapara=[];

%% out of memory error in the above function. So we initialize once instead of every time to initialize. 

transexperts=unique(ExpTransTktFile(:,1));   
   

for i=1:size(ExpTransTktFile,1)
    temprand=randn(1,K);
    phi=[phi; [ExpTransTktFile(i,[1,3]) exp(temprand)./sum(exp(temprand))]];
end

betaindxflag=[];
betaparanew=[]; % with TRC

betapara_VE=[]; % with VE in C+
betapara_PR=[]; % with PR in C+

j=1;
for i=1:size(ExpTransTktFile,1)
        
       currentexpert=ExpTransTktFile(i,1);
       nextexpert=ExpTransTktFile(i,2);
       
       currneigh=find(EdgeConn(currentexpert,:))';
       currneighNUM=numel(currneigh);
       tempposi=find(currneigh==ExpTransTktFile(i,2));
       
        currTktvectors=repmat([Tktvectors(ExpTransTktFile(i,3),:) 1],currneighNUM,1);
        currvectors=[currTktvectors Expvectors(currneigh,:)];
        
        probest0=sigmoid(currvectors*w_class);
       % probest=probest0.^2/sum(probest0.^2);
       probest=probest0;
       probest_VE=probest0;
       probest_PR=probest0;
       
       posi1=find(probest0>=0.5);
       posi2=find(probest0<0.5);
       
       %probest(posi1)=0.99;
       %probest(posi1)=(1-0.000001)*ones(numel(posi1),1)/numel(posi1);
       probest_VE(posi1)=volumtran(currentexpert,currneigh(posi1))'/sum(volumtran(currentexpert,currneigh(posi1)));
       probest_VE(posi2)=0.000000001;
       
       probest_PR(posi1)=ones(numel(posi1),1)/numel(posi1);
       probest_PR(posi2)=0.000000001;       
       
       
       probest(posi1)=TRC_ij(currentexpert,currneigh(posi1))'/sum(TRC_ij(currentexpert,currneigh(posi1)));
       probest(posi2)=0.000000001;
       
       if numel(posi1)==0
          % probest(posi2)=ones(numel(posi2),1)/numel(posi2);
          probest(posi2)=TRC_ij(currentexpert,currneigh(posi2))'/sum(TRC_ij(currentexpert,currneigh(posi2)));          
          probest_VE(posi2)=volumtran(currentexpert,currneigh(posi2))'/sum(volumtran(currentexpert,currneigh(posi2)));
          probest_PR(posi2)=ones(numel(posi2),1)/numel(posi2);
       end
     
       
       
        betapara_VE=[betaparanew;[ones(currneighNUM,1)*currentexpert ones(currneighNUM,1)*ExpTransTktFile(i,3) currneigh probest_VE probest0>=0.5]];
        
        betapara_PR=[betaparanew;[ones(currneighNUM,1)*currentexpert ones(currneighNUM,1)*ExpTransTktFile(i,3) currneigh probest_PR probest0>=0.5]];        
    
        
        betaparanew=[betaparanew;[ones(currneighNUM,1)*currentexpert ones(currneighNUM,1)*ExpTransTktFile(i,3) currneigh probest probest0>=0.5]];
        betaindxflag=[betaindxflag;[j currneighNUM j+tempposi-1 numel(posi1) numel(find(currneigh(posi1)==nextexpert))] ];  % whether there is any expert to solve the task
        j=j+currneighNUM;    
end

posi=find(sum(EdgeConn,2));
TRCtran=EdgeConn;
TRCtran(posi,:)=TRC_ij(posi,:)./(sum(TRC_ij(posi,:),2)*ones(1,NumExperts));













%gamma=sparse(gamma_elem(:,1),gamma_elem(:,2),gamma_elem(:,3),NumExperts,K,numel(TransExpertIndex)*K);

 gammaprior=exp(randn(NumExperts,K));
 gammaprior=gammaprior./(sum(gammaprior,2)*ones(1,K));
 
 %gammaprior_init=gammaprior;
 %save 'gammprior_init_all_AIX' 'gammaprior_init';
 
 
%  gammaprior=zeros(size(gammaprior0));
%  gammaprior(transexperts,:)=gammaprior0(transexperts,:);
 
 

% save 'phi_initial' 'phi';
% save 'betapara_initial' 'betapara';
% save 'gammaprior_initial' 'gammaprior';
% save 'betaparanew_initial' 'betaparanew';
% save 'betaindxflag' 'betaindxflag';

% save 'phi_initial_alledge' 'phi';
% %save 'betapara_initial_alledge' 'betapara';
% save 'gammaprior_initial_alledge' 'gammaprior';
% save 'betaparanew_initial_alledge' 'betaparanew';
% save 'betaindxflag_alledge' 'betaindxflag';

% % load phi_initial;
% % load betapara_initial;
% % load gammaprior_initial;
% % load betaparanew_initial;
% % load betaindxflag;
betaindxflag=uint32(betaindxflag);

fprintf('*************Initialization done!***********\n');


