% Probability matrix
% Modes N= Normal, T=Tachycardia, B=Bradycardia
tic;
% p_NN=0.9;
% p_NT=0.05;
% p_NB=0.05;
% 
% p_BN=0.9;
% p_BT=0.05;
% p_BB=0.05;
% 
% p_TN=0.9;
% p_TT=0.05;
% p_TB=0.05;
% 
% Prob = [p_NN,p_NT,p_NB;p_TN,p_TT,p_TB;p_BN,p_BT,p_BB];
time=0;

% Matrix of n-combinations (n_comb) of 3 modes
n_comb=10;
[COMB]=combinations_failure(n_comb);

% Initial probability distribution per mode [N T B]
% Init = [0.4, 0.3, 0.3];
pr_final = 0;
pr_final_after = 0;
exp_energy = 0;
pr_failure = 0.1;


fprintf(1,'Total number of iterations %d\n', size(COMB,1));
for i=1:size(COMB,1),
    clear TH_N_FAILURE;
    c_state_HEART=TH_N(1,1);
    % Take the composition
    [TH_COMPOSITION TH_STATES] = composition_failure(N_STATES_N, TH_N, TP, IM, c_state_PACEMAKER, c_state_HEART,COMB(i,:));
    
    %TH_COMPOSITION = composition(N_STATES_N, TH_N, TP, IM, c_state_PACEMAKER, c_state_HEART);
    
    % Check the correctness of the path
    correct=monitor(TH_COMPOSITION,60,120);
    
    [prob_val] = prob_computation_failure(pr_failure,COMB(i,:));
    if correct==0,
        % Computing probabilities 
        pr_final=pr_final+prob_val;
    end
%     
%         % Check the correctness of the path
%     correct=monitor(TH_COMPOSITION,60,120);
%     
%      % Computing probabilities 
%     [prob_val_after] = prob_computation(Prob,COMB(i,:));
%     if correct==0,
%        pr_final_after=pr_final_after+prob_val_after*Init(COMB(i,1)+1);
%     end
    
   exp_energy=exp_energy+count_all_beats(TH_COMPOSITION)*prob_val;
    
fprintf(1,'Iteration number %d of %d\n',i, size(COMB,1));    
end

time=toc;
fprintf(1,'Running time of the experiment %d\n',time);