public State Generate(DeviceNode[] root) { State newState = new State(root); foreach (var item in States) { if (item == newState) { return(item); } } States.Add(newState); List <Dictionary <State, double> > stat = new List <Dictionary <State, double> >(); List <double?> lambdas = new List <double?>(); for (int i = 0; i < root.Length; ++i) { Dictionary <State, double> sIntencivity1 = new Dictionary <State, double>(); if (root[i].onProc != 0) { DeviceNode[] next = new DeviceNode[root.Length]; root.CopyTo(next, 0); ushort t1 = next[i].onProc; ushort t2 = next[i].queue; if (next[i].queue == 0) { next[i].onProc--; } else { next[i].queue--; } for (int j = 0; j < next[i].Transition.Count; j++) { next[i].Transition.Keys.ElementAt(j).AddTask(); sIntencivity1.Add(Generate(next), next[i].Transition.Values.ElementAt(j)); next[i].Transition.Keys.ElementAt(j).RemoveTask(); } next[i].onProc = t1; next[i].queue = t2; State st = new State(next); lambdas.Add(1.0 / root[i].tau); } else { lambdas.Add(null); } stat.Add(sIntencivity1); } Dictionary <State, double> sIntencivity2 = new Dictionary <State, double>(); for (int i = 0; i < lambdas.Count; i++) { if (lambdas[i] != null) { foreach (var item in stat[i]) { if (item.Key != newState) { bool contain = false; foreach (var comp in sIntencivity2.Keys) { if (comp == item.Key) { contain = !contain; break; } } if (!contain) { sIntencivity2.Add(item.Key, newState.DeviceStates[i].onProc * item.Value * lambdas[i] ?? 0); } else { sIntencivity2[item.Key] += newState.DeviceStates[i].onProc * item.Value * lambdas[i] ?? 0; } } } } } Dictionary <State, double> sProbabilities = new Dictionary <State, double>(); double lambda = 0; for (int i = 0; i < sIntencivity2.Count; i++) { if (sIntencivity2.Keys.ElementAt(i) != newState) { lambda += sIntencivity2.Values.ElementAt(i); } } double T = 1 / lambda; double deltaT = T * DeviceNode.minTau / 5; double p0 = Math.Exp(-lambda * deltaT); sProbabilities.Add(newState, p0); double[] xi = new double[sIntencivity2.Count]; double xiSum = 0; for (int i = 0; i < sIntencivity2.Count; i++) { xi[i] = 1 - Math.Exp(-sIntencivity2.Values.ElementAt(i) * deltaT); xiSum += xi[i]; } for (int i = 0; i < sIntencivity2.Count; i++) { sProbabilities.Add(sIntencivity2.Keys.ElementAt(i), (1 - p0) * (xi[i] / xiSum)); } foreach (var item in sProbabilities) { probabilities[findIndex(item.Key), findIndex(newState)] = item.Value; } return(newState); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; DeviceNode CP = new DeviceNode(2, 6, 2); DeviceNode NB = new DeviceNode(1); DeviceNode OM = new DeviceNode(3); DeviceNode SB = new DeviceNode(6); DeviceNode NA = new DeviceNode(25); DeviceNode DC = new DeviceNode(50); CP.Transition.Add(NB, 1); //CP.Transition.Add(CP, 0.5); NB.Transition.Add(CP, 0.5); NB.Transition.Add(OM, 0.25); // if removing for test, change value from 0.25 to 0.5 OM.Transition.Add(NB, 1); NB.Transition.Add(SB, 0.25); // SB.Transition.Add(NB, 0.4); // SB.Transition.Add(DC, 0.3); // can be removed for tests SB.Transition.Add(NA, 0.3); // NA.Transition.Add(SB, 1); // DC.Transition.Add(SB, 1); // M m = new M(CP, NB, OM, SB, NA, DC); double[] firstState = new double[462]; double[] res = new double[462]; firstState[0] = 1; for (int i = 0; i < 10000; i++) { res = multiply(m.probabilities, firstState); res.CopyTo(firstState, 0); } double[] load = new double[8]; for (int i = 0; i < 462; i++) { if (m.States[i].DeviceStates[0].onProc >= 1) { load[2] += res[i] / 2; } if (m.States[i].DeviceStates[0].onProc == 2) { load[1] += res[i]; } if (m.States[i].DeviceStates[0].onProc == 1) { load[0] += res[i]; } for (int j = 0; j < 5; j++) { if (m.States[i].DeviceStates[j + 1].onProc >= 1) { load[3 + j] += res[i]; } } } int theMostPossible = 0; double MostPossibility = 0.0; int iter = 0; foreach (var item in res) { if (MostPossibility < item) { MostPossibility = item; theMostPossible = iter; } iter++; } Console.WriteLine(m.States[theMostPossible]); Console.WriteLine("Load of CP core0 = " + load[0] + "\n"); Console.WriteLine("Load of CP core1 = " + load[1] + "\n"); Console.WriteLine("Load of CP = " + load[2] + "\n"); Console.WriteLine("Load of NB = " + load[3] + "\n"); Console.WriteLine("Load of OM = " + load[4] + "\n"); Console.WriteLine("Load of SB = " + load[5] + "\n"); Console.WriteLine("Load of NA = " + load[6] + "\n"); Console.WriteLine("Load of DC = " + load[7] + "\n"); Console.Read(); }