

% set the probabilities for the variables in the pDTMC model for each
% vector in Matlab code: 
y = [0.4053, 0.5946, 0.0001];     %y = {y1, y2, y3} and each probability is associated to a state in the DTMC i.e., {s1, s3, s7} 
x = [0.579, 0.0005, 0.4205];     %x = {x1, x2, x3} -> {s8, s9, s4}
w = [0.9998, 0.0002];          %w = {w1, w2} -> {s8, s9}
z = [0.25065, 0.00125 ,0.7481];   %z = {z1, z2, z3} -> {s6, s9, s8}
k = [0.9996, 0.0004];        %k = {k1, k2} -> {s8, s9} 

ws = [0.2, 0.55, 0.25];  % the known outgoing transition probabilites for state s1, (Web server)
as = [0.7, 0.3];         % the known outgoing transition probabilites for state s2, (Application Server)

% Requirements
Req = {'(-160*w1*y2*x1-160*w1*y2*x2+77*k1*z1*y1+160*y2*x1-77*z1*y1-77*z2*y1+160*w1*y2+160*y1)/160' 
       '(160*y2*x1 - 77*z1*y1 - 77*z2*y1 + 160*y1)/160'
       '(508*y1 + 385*y1*z3 + 1000*x1*y2 + 1000*y2*x3*w1)/1000'
       '(-384*y2*x1 - 384*y2*x2 + 231*z1*y1 + 3194*y1 + 544*y2)/32'
       '(-640*y2*x1 - 640*y2*x2 + 539*z1*y1 - 640*y2)/160'};

% oneMinusBeta lowerBound, upperBound and step
oneMinusBeta = [0.7 0.99 0.01];

%N = [5000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000];            % the number of iterations for each simulation of the system, i.e., for N = 5000, 10000, 20000, 30000, …, 100000 steps, 
%N = [500, 1000, 2500, 5000];
%N = [50, 100, 250, 500];
N = [500, 5000];
n = 2;                   % number of elements in N 


Transitions = zeros(17, 11);

% open the file with permission to append
% fid = fopen('Table3.txt','a');
% fprintf(fid, 'N        5K        10K        20K        30K        40K        50K        60K        70K        80K        90K        100K\n');


%1. Begin n simulations of the system for N iterations
for i = 1:n
    
    % These are the counters for each state, counting the number of out going transtions 
    state_0 = 0; 
    state_0_to_1 = 0;
    state_0_to_3 = 0; 
    state_0_to_7 = 0; 
    state_1 = 0;
    state_1_to_1 = 0; 
    state_1_to_2 = 0; 
    state_1_to_8 = 0; 
    state_2 = 0;
    state_2_to_5 = 0; 
    state_2_to_8 = 0; 
    state_3 = 0;
    state_3_to_4 = 0; 
    state_3_to_8 = 0; 
    state_3_to_9 = 0; 
    state_4 = 0; 
    state_4_to_8 = 0; 
    state_4_to_9 = 0; 
    state_5 = 0; 
    state_5_to_6 = 0; 
    state_5_to_8 = 0; 
    state_5_to_9 = 0; 
    state_6 = 0; 
    state_6_to_8 = 0; 
    state_6_to_9 = 0; 
    state_7 = 0; 
    state_8 = 0; 
    state_9 = 0; 
    
    %2. For the i-th simulation execute the DTMC workflow
    for j = 1:N(i)
        
        %2.1 call the function exe_State(y) to create a multinomial
        %    distribution  for the probabilities given in 'y' and the function then randomly
        %    selects a state 
        
        state_0 = exe_State(y); 
        
        % 2.2 If the selected state is s1 then increment by 1 the number of
        %     transitions going from state_0_to_s1 (state_0_to_1 + 1) and execute the process of
        %     transitioning from s1 by calling the function
        %     exe_State1(ws,as,z,k,state_0_to_1,state_1_to_8,state_1_to_2,state_2_to_5,state_2_to_8,state_5_to_6,state_5_to_8,state_5_to_9),
        %     passing all the parameters that might be associated with this path. 
        if(state_0 == 1)
            state_0_to_1 = state_0_to_1 + 1; 
            [return_s1_from_s1,return_s2_from_s1,return_s5_from_s2,return_s6_from_s5,return_s8_from_s1, return_s8_from_s2 ,return_s8_from_s5 ,return_s9_from_s5, return_s8_from_s6, return_s9_from_s6] = exe_State1(ws,as,z,k,state_1_to_1,state_1_to_2,state_1_to_8, state_2_to_5, state_2_to_8, state_5_to_6,state_5_to_8,state_5_to_9, state_6_to_8, state_6_to_9);
            state_1_to_1 = return_s1_from_s1; state_1_to_2 = return_s2_from_s1; state_2_to_5 = return_s5_from_s2; state_5_to_6 = return_s6_from_s5; 
            state_1_to_8 = return_s8_from_s1; state_2_to_8 = return_s8_from_s2; state_5_to_8 = return_s8_from_s5; state_5_to_9 = return_s9_from_s5; 
            state_6_to_8 = return_s8_from_s6; state_6_to_9 = return_s9_from_s6; 
        
        % 2.3 If the selected state is s3 then increment the number of outgoing transitions from s3 by 1 (state_3 + 1), and execute the process of transitioning 
        %     from s3, by calling the function exe_State3(x,w,state_4,
        %     state_8, state_9) and passing all the parameters that might
        %     be associated with this path
        
        elseif (state_0 == 2)
            state_0_to_3 = state_0_to_3 + 1; 
            
            [return_s4_from_s3,return_s8_from_s3,return_s9_from_s3, return_s8_from_s4, return_s9_from_s4] = exe_State3(x,w,state_3_to_4, state_3_to_8, state_3_to_9, state_4_to_8, state_4_to_9); 
            state_3_to_4 = return_s4_from_s3; state_3_to_8 = return_s8_from_s3; state_3_to_9 = return_s9_from_s3; 
            state_4_to_8 = return_s8_from_s4; state_4_to_9 = return_s9_from_s4; 
           
          % 2.4 If the selected state is s7, then increment the number of outgoing transitions to s7 by 1 (state_7 + 1)  
          else 
            state_0_to_7 = state_0_to_7 + 1; 
%                 p = 'Http 503 Server Unavailable';
%                 disp(p); 
       end
           
    end
    
    
        Transitions(1,i) = state_0_to_1;
        Transitions(2,i) = state_0_to_3;
        Transitions(3,i) = state_0_to_7;
        Transitions(4,i) = state_1_to_2;
        Transitions(5,i) = state_1_to_8;
        Transitions(6,i) = state_2_to_5;
        Transitions(7,i) = state_2_to_8;
        Transitions(8,i) = state_5_to_6;
        Transitions(9,i) = state_5_to_8;
        Transitions(10,i) = state_5_to_9;
        Transitions(11,i) = state_6_to_8;
        Transitions(12,i) = state_6_to_9;
        Transitions(13,i) = state_3_to_4;
        Transitions(14,i) = state_3_to_8;
        Transitions(15,i) = state_3_to_9;
        Transitions(16,i) = state_4_to_8;
        Transitions(17,i) = state_4_to_9;
        
    
   itr = N(i); 
   fName = ['input-' num2str(itr) '.txt']; 
   fid = fopen(fName,'wt');  
        
   for m = 1:size(Req)
       
   
   
   disp(Req(m,:)); 
   fprintf(fid,'%s\n',Req{m,:});
   disp(oneMinusBeta); 
   fprintf(fid,'%0.2f ',oneMinusBeta);
   

    disp(itr); 
    
    disp(['x 3 ', num2str(state_0_to_3)]);
    fprintf(fid,'\n%s\n',['x 3 ', num2str(state_0_to_3)]);
    disp(num2str(state_3_to_8));
    fprintf(fid,'%s\n',num2str(state_3_to_8));
    disp(num2str(state_3_to_9));
    fprintf(fid,'%s\n',num2str(state_3_to_9));
    disp(num2str(state_3_to_4));
    fprintf(fid,'%s\n',num2str(state_3_to_4));
    
    disp(['y 3 ', num2str(state_0_to_1 + state_0_to_3 + state_0_to_7)]);
    fprintf(fid,'%s\n',['y 3 ', num2str(state_0_to_1 + state_0_to_3 + state_0_to_7)]);
    disp(num2str(state_0_to_1));
    fprintf(fid,'%s\n',num2str(state_0_to_1));
    disp(num2str(state_0_to_3));
    fprintf(fid,'%s\n',num2str(state_0_to_3));
    disp(num2str(state_0_to_7));
    fprintf(fid,'%s\n',num2str(state_0_to_7));
    
    disp(['z 3 ', num2str(state_2_to_5)]);
    fprintf(fid,'%s\n',['z 3 ', num2str(state_2_to_5)]);
    disp(num2str(state_5_to_6));
    fprintf(fid,'%s\n',num2str(state_5_to_6));
    disp(num2str(state_5_to_9));
    fprintf(fid,'%s\n',num2str(state_5_to_9));
    disp(num2str(state_5_to_8));
    fprintf(fid,'%s\n',num2str(state_5_to_8));
    
    disp(['k 2 ', num2str(state_5_to_6)]);
    fprintf(fid,'%s\n',['k 2 ', num2str(state_5_to_6)]);
    disp(num2str(state_6_to_8));
    fprintf(fid,'%s\n',num2str(state_6_to_8));
    disp(num2str(state_6_to_9));
    fprintf(fid,'%s\n',num2str(state_6_to_9));
    
    disp(['w 2 ', num2str(state_3_to_4)]);
    fprintf(fid,'%s\n',['w 2 ', num2str(state_3_to_4)]);
    disp(num2str(state_4_to_8));
    fprintf(fid,'%s\n',num2str(state_4_to_8));
    disp(num2str(state_4_to_9));
    fprintf(fid,'%s\n',num2str(state_4_to_9));
    
    disp('END');
    fprintf(fid,'%s\n','END');
   
%     disp(['No. of transitions associated with NO->1, after ',num2str(itr), ' iteration = ' , num2str(state_0_to_1)]);
%     disp(['No. of transitions associated with NO->3, after ',num2str(itr), ' iteration = ' , num2str(state_0_to_3)]);
%     disp(['No. of transitions associated with NO->7, after ',num2str(itr), ' iteration = ' , num2str(state_0_to_7)]);
%     
%     disp(['No. of transitions associated with N1->2, after ',num2str(itr), ' iteration = ' , num2str(state_1_to_2)]);
%     disp(['No. of transitions associated with N1->8, after ',num2str(itr), ' iteration = ' , num2str(state_1_to_8)]);
%     
%     disp(['No. of transitions associated with N2->5, after ',num2str(itr), ' iteration = ' , num2str(state_2_to_5)]); 
%     disp(['No. of transitions associated with N2->8, after ',num2str(itr), ' iteration = ' , num2str(state_2_to_8)]); 
%     
%     disp(['No. of transitions associated with N5->6, after ',num2str(itr), ' iteration = ' , num2str(state_5_to_6)]); 
%     disp(['No. of transitions associated with N5->8, after ',num2str(itr), ' iteration = ' , num2str(state_5_to_8)]); 
%     disp(['No. of transitions associated with N5->9, after ',num2str(itr), ' iteration = ' , num2str(state_5_to_9)]); 
%     
%     disp(['No. of transitions associated with N6->8, after ',num2str(itr), ' iteration = ' , num2str(state_6_to_8)]); 
%     disp(['No. of transitions associated with N6->9, after ',num2str(itr), ' iteration = ' , num2str(state_6_to_9)]); 
%     
%     disp(['No. of transitions associated with N3->4, after ',num2str(itr), ' iteration = ' , num2str(state_3_to_4)]); 
%     disp(['No. of transitions associated with N3->8, after ',num2str(itr), ' iteration = ' , num2str(state_3_to_8)]); 
%     disp(['No. of transitions associated with N3->9, after ',num2str(itr), ' iteration = ' , num2str(state_3_to_9)]); 
%     
%     disp(['No. of transitions associated with N4->8, after ',num2str(itr), ' iteration = ' , num2str(state_4_to_8)]); 
%     disp(['No. of transitions associated with N4->9, after ',num2str(itr), ' iteration = ' , num2str(state_4_to_9)]); 
%     
%     disp(' '); 
    
    end
    fclose(fid);
end 

