<< Statistics`ContinuousDistributions`

n = 4;

icrange = {.25,1};

failrange = {10,40};

acceptrange = {1,2};

\(CreateRates[ic_List, fails_List, accept_List] := 
	
      Module[
			{icrates = 
            Table[Random[Real, {1/ic[\([2]\)], 1/ic[\([1]\)]}], {i, n}, {j, 
                n - 1}], 
			failrates\  = 
            Table[Random[Real, {1/fails[\([2]\)], 1/fails[\([1]\)]}], {i, 
                n}], \ acceptrates = 
            Table[Random[Real, {1/accept[\([2]\)], 1/accept[\([1]\)]}], {i, 
                n}], 
			rates\  = \ Table[0, {n}]}, 

		Do[
          rates[\([i]\)]\  = \ 
						Append[
              Insert[icrates[\([i]\)], failrates[\([i]\)], 
                i], 
											acceptrates[\([i]\)]], {i, 1, 
            n}]; 
		rates]\)

WhichOne[probabilities_] :=

	Module[{event, eventcheck = 0,m = 0, problist},

		event = Random[];

		

		problist = Flatten[probabilities];

		While[eventcheck  < event,

			m = m+1;

			eventcheck = eventcheck + problist[[m]]

			];

		If[Mod[m,n+1]==0,

			to = n+1; from = Quotient[m,n+1],

			to = Mod[m,n+1];from = Quotient[m,n+1]+1;

			];

		(*Print["to = ",to,"  from = ",from];*)

		]

grid[n_] := Module[{dim = n, i},

	Graphics[

	{Table[{Line[{{i,0},{i,dim}}],

			Line[{{0,i},{dim,i}}]},{i,0,dim}],

	 Table[Line[{{dim+i,0},{dim+i,dim}}],{i,1,2}],

	 Table[Line[{{dim+1,i},{dim+2,i}}],{i,0,dim}]},		

	 AspectRatio -> Automatic]]

Conn[x_, y_, c_] :=

   Module[{col,txt,i,j},

    col = Which[c == 0, RGBColor[0,1,0],  (*Acceptance*)

		 	    c == 1, RGBColor[0,.7,.8], (*direct connection*)

		 	    c == 2, RGBColor[.8,.2,.5], (*indirect connection*)

		 	    c == 3, RGBColor[0,0,0]];  (*failure*)

	j = Which[y == n+1,	x,

			  y != n+1, y];

	i = Which[y == n+1,	n+2,

			  y != n+1, x];		   	

	txt = Which[y == n+1, StringForm["`1`",from],

				x == y, StringForm[""],

				y != n+1, StringForm["`1`->`2`",i,j]];

					 	

	Graphics[{{col, Rectangle[{i-1,j-1},{i,j}]},

			  Line[{{i,j},{i-1,j},{i-1,j-1},{i,j-1},

			       {i,j}}],

			       {RGBColor[0,0,0],

			          Text[txt,{i-.5,j-.5}]}},

			  AspectRatio -> Automatic] ]

\(\(DropConn[k_, connlist_List, case_] := 
	
        Module[{del\  = {}, i, m}, 
		\ \ If[
            case\  == \ False, 
						Do[
              AppendTo[del, 
                Position[connlist, Conn[k, i, m]]], 
								{i, 1, 
                n}, {m, 1, 2}], 
		\ \ \ \ \ \ \ \ 	Do[
              AppendTo[del, 
                Position[connlist, Conn[k, i, m]]], 
								{i, 1, 
                n + 1}, {m, 0, 
                3}]
		\ \ \ \ ]; 
\ \ \ \ \ \ \ \ \ del\  = \ 
            Flatten[del, 1]; 
				Delete[connlist, 
            del]
	];\)\)

time = 0;
graph  =  {grid[n]};
descendants  =  Table[0, {n}, {n}];
failures  =  failuretime = Table[0, {n}];
eventno  =  0;
\(	\(exprates = 
        CreateRates[icrange, failrange, acceptrange];\)\)
 mu = Apply[Plus, Flatten[exprates]];
  eventprobs  =  exprates/mu;
\(\ \(\(While[time\  < 20, 
	\(eventno++\); 
	
      time = time + Random[ExponentialDistribution[mu]]; 
	
      WhichOne[eventprobs]; 
	
      Which[to\  == \ 
          from, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*\ 
          failure\ occured\ *) 
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[
          Apply[Plus, failures] == 
            0, \ \  (*no\ other\ failures*) 
											graph\  = \ {grid[n]}; 
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants\  = \ Table[0, {n}, {n}]	 (*reset\ *) 
	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]; 
	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 	If[
          failures[\([to]\)]\  != \ 
            1, \ \ \ 
					\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*no\ previous\ error\ in\ process*) 
		\ \ 	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([to]\)]\  = \ 
            Table[0, {n}]; 	 (*\ 
            reset\ *) 
											\ graph = \ 
            DropConn[to, graph, 
              False]; 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ failures[\([to]\)]\  = \ 1; \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*\ 
            record\ failure\ *) 
											AppendTo[graph, \ 
            Conn[to, to, 
              3]]; 			
		\ \ 	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ failuretime[\([to]\)]\  = \ 
            time
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]; 
				\ \ \ \ \ \ \ \ txt\  = 
          StringForm["Event# `1`: Failure at `2`", eventno, 
            from]; 
				\ \ \ \ \ \ \ \ \ Show[graph, 
          PlotLabel\  -> \ 
            
            txt], \ 		\ \ \ \ \ \ 
		\ \ 
		\ \ \ \ \ \ \ \ \ to\  == \ n + 1, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*Acceptance\ test\ occured\ *) 
								graph\  = \ 
          Union[AppendTo[graph, \ 
              Conn[from, to, 0]]]; 
			\ \ \ \ \ \ \ \ \ \ \ \ txt\  = 
          StringForm["Event# `1`: Acceptance Test at `2`", eventno, 
            from]; 
				\ \ \ \ \ \ \ \ \ Show[graph, 
          PlotLabel\  -> \ txt]; 
			\ \ \ \ \ \ \ \ \ \ \ \ If[
          failures[\([from]\)]	 == \ 
            0, 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ graph\  = \ 
            DropConn[from, graph, \ 
              True]; 
				\ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([from]\)]\  = \ Table[
              0, {n}]
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\ ; \ \ 							 (*no\ failure - reset*) 
			\ \ \ \ \ \ \ \ \ \ \ Do[
          If[failures[\([i]\)]\  == \ 
                1\  && 
					\ \ \ \ \ \ \ \ \ \ \ \ \ \((descendants[\([i, from]\)]\  == \ 
                    1\  || 
					\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 	i\  == \ 
                    from)\), 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Print["Error detected in process ", 
              i]; 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([i]\)]\  = \ Table[0, {n}]; 
					\ \ \ \ \ \ \ \ \ \ \ \ \ graph\  = \ 
              DropConn[i, graph, 
                True]; 
					\ \ \ \ \ \ \ \ \ \ \ \ \ failuretime[\([i]\)]\  = \ \(failures[\([i]\)] = 
                0\)
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ], {i, 1, 
            n}
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ], 
		
			\ \ \ True, \  (*\ connection\ occured\ *) 
		\ \ \ \ \ \ \ \ \ \ graph\  = \ 
          Union[AppendTo[graph, \ 
              Conn[from, to, 
                1]]]; 
			\ \ \ \ \ \ \ \ \ descendants[\([from, 
              to]\)]\  = \ 
          1; 
			\ \ \ \ \ \ \ Do[
			\ \ \ \ \ \ \ \ \ \ \ \ \ If[\((descendants[\([j, from]\)]\  == \ 1)\)\  && \((j\  != \ 
                  to\ )\)\  && 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \((descendants[\([j, 
                      to]\)]\  == \ 
                  0)\), 
												graph\  = \ 
              Union[
                AppendTo[graph, \ 
                  Conn[j, to, 
                    2]]]; 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([j, to]\)]\  = \ 
              1
			\ \ 	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ],  (*End\ If\ *) 
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ {j, 1, n}]; \  (*\ 
          End\ Do\ *) 
								graph\  = \ 
          Union[graph]; 
								txt\  = 
          StringForm["Event# `1`: from `2` to `3`", \ eventno, \ from, 
            to]; 
								Show[graph, \ 
          PlotLabel\  -> \ txt]
	]; \ \ \ \ \ \ \  (*\ 
        End\ of\ Which\ *) 
]\)\(;\)\(\ \ \ \ \ \ \ \ \ \ \ \)\( (*\ 
      End\ of\ While\ *) \)\(
\)\)\)