public Distribution calcGroupedDistr(List<HashSet<MDPState>> ListHashMDP, Distribution Distr, MDPState Initial, MDPState Target, MDPState Safe, MDP mdp) { Distribution toReturn = new Distribution(Distr.Event); double toInit = 0; double toTarget = 0; double[] combined = new double[ListHashMDP.Count]; //MDPState[] groupstates = new MDPState[seperation.Count]; foreach (KeyValuePair<double, MDPState> transition in Distr.States) { if (TargetStates.Contains(transition.Value)) { toTarget += transition.Key; } else if (InitState == transition.Value) { toInit += transition.Key; } else { for (int i = 0; i < ListHashMDP.Count; i++) { if (ListHashMDP[i].Contains(transition.Value)) { combined[i] += transition.Key; break; } } } } double toSafe = 1 - toTarget - toInit; for (int i = 0; i < ListHashMDP.Count; i++) { toSafe -= combined[i]; } if (toInit != 0) { toReturn.AddProbStatePair(Math.Round(toInit, 5), Initial); } if (toTarget != 0) { toReturn.AddProbStatePair(Math.Round(toTarget, 5), Target); } if (toSafe != 0) { toReturn.AddProbStatePair(Math.Round(toSafe, 5), Safe); } //toReturn.AddProbStatePair(toSafe, Safe); for (int j = 0; j < ListHashMDP.Count; j++) { if (combined[j] != 0) { MDPState state = mdp.States[StateFromHashset(ListHashMDP[j]).ID]; toReturn.AddProbStatePair(Math.Round(combined[j], 5), state); //toReturn.AddProbStatePair(Math.Round(combined[j], 5), StateFromHashset(ListHashMDP[j])); } } return toReturn; }
public MDP BuildQuotientMDP(VerificationOutput VerificationOutput) { //return this; MDP toReturn = new MDP(Precision, MAX_DIFFERENCE); //todo change to set List<KeyValuePair<string, string>> BoundaryOneTransition = new List<KeyValuePair<string, string>>(); //todo change to set List<Distribution> ProbTransitions = new List<Distribution>(); Dictionary<string, List<Distribution>> GlobalProbTransitions = new Dictionary<string, List<Distribution>>(); StringDictionary<bool> visited = new StringDictionary<bool>(States.Count); List<KeyValuePair<HashSet<string>, MDPState>> sccs = new List<KeyValuePair<HashSet<string>, MDPState>>(); Dictionary<string, int> preorder = new Dictionary<string, int>(); Dictionary<string, int> lowlink = new Dictionary<string, int>(); //HashSet<string> scc_found = new HashSet<string>(); Stack<MDPState> TaskStack = new Stack<MDPState>(); //Dictionary<string, List<string>> OutgoingTransitionTable = new Dictionary<string, List<string>>(); Stack<MDPState> stepStack = new Stack<MDPState>(1024); visited.Add(InitState.ID, false); TaskStack.Push(InitState); //# Preorder counter int preor = 0; do { while (TaskStack.Count > 0) { MDPState pair = TaskStack.Peek(); string v = pair.ID; if (visited.GetContainsKey(v) && visited.GetContainsKey(v)) { TaskStack.Pop(); continue; } if (!preorder.ContainsKey(v)) { preorder.Add(v, preor); preor++; } bool done = true; List<Distribution> list = pair.Distributions; List<MDPState> nonProbTrans = new List<MDPState>(); List<Distribution> ProbTrans = new List<Distribution>(); for (int i = 0; i < list.Count; i++) { if (list[i].IsTrivial()) { nonProbTrans.Add(list[i].States[0].Value); } else { ProbTrans.Add(list[i]); } } if (ProbTrans.Count > 0 && !GlobalProbTransitions.ContainsKey(v)) { GlobalProbTransitions.Add(v, ProbTrans); ProbTransitions.AddRange(ProbTrans); } for (int k = nonProbTrans.Count - 1; k >= 0; k--) { MDPState step = nonProbTrans[k]; string tmp = step.ID; if (visited.ContainsKey(tmp)) { //if this node is still not visited if (!preorder.ContainsKey(tmp)) { //only put the first one to the work list stack. //if there are more than one node to be visited, //simply ignore them and keep its event step in the list. if (done) { TaskStack.Push(step); done = false; } } } else { visited.Add(tmp, false); //OutgoingTransitionTable.Add(tmp, new List<string>(8)); //only put the first one into the stack. if (done) { TaskStack.Push(step); done = false; } } } if (done) { int lowlinkV = preorder[v]; int preorderV = preorder[v]; bool selfLoop = false; for (int j = 0; j < nonProbTrans.Count; j++) { string w = nonProbTrans[j].ID; if (w == v) { selfLoop = true; } if (!visited.GetContainsKey(w)) { if (preorder[w] > preorderV) { lowlinkV = Math.Min(lowlinkV, lowlink[w]); } else { lowlinkV = Math.Min(lowlinkV, preorder[w]); } } else //in this case, there is a tau transition leading to an SCC; must add the transition into the toReturn automaton { BoundaryOneTransition.Add(new KeyValuePair<string, string>(v, w)); } } lowlink[v] = lowlinkV; TaskStack.Pop(); HashSet<string> scc = new HashSet<string>(); if (lowlinkV == preorderV) { scc.Add(v); visited.SetValue(v, true); while (stepStack.Count > 0 && preorder[stepStack.Peek().ID] > preorderV) { string s = stepStack.Pop().ID; scc.Add(s); visited.SetValue(s, true); } MDPState newstate = new MDPState(toReturn.States.Count.ToString()); if (scc.Count > 1 || (scc.Count == 1 && selfLoop)) { newstate.AddDistribution(new Distribution(Constants.TAU, newstate)); //add self loop: sun jun } sccs.Add(new KeyValuePair<HashSet<string>, MDPState>(scc, newstate)); toReturn.AddState(newstate); if (scc.Contains(InitState.ID)) { toReturn.SetInit(newstate); } foreach (MDPState state in TargetStates) { if (scc.Contains(state.ID)) { toReturn.AddTargetStates(newstate); } } } else { stepStack.Push(pair); } } } if (ProbTransitions.Count > 0) { foreach (Distribution step in ProbTransitions) { foreach (KeyValuePair<double, MDPState> pair in step.States) { string stateID = pair.Value.ID; if (!visited.ContainsKey(stateID)) { TaskStack.Push(pair.Value); visited.Add(stateID, false); } } } ProbTransitions.Clear(); } } while (TaskStack.Count > 0); foreach (KeyValuePair<string, string> pair in BoundaryOneTransition) { MDPState source = null; MDPState target = null; foreach (KeyValuePair<HashSet<string>, MDPState> sccstate in sccs) { if (sccstate.Key.Contains(pair.Key)) { source = sccstate.Value; } if (sccstate.Key.Contains(pair.Value)) { target = sccstate.Value; } } toReturn.AddDistribution(source.ID, new Distribution(Constants.TAU, target)); VerificationOutput.ReducedMDPTransitions++; } foreach (KeyValuePair<string, List<Distribution>> pair in GlobalProbTransitions) { MDPState source = null; foreach (KeyValuePair<HashSet<string>, MDPState> sccstate in sccs) { if (sccstate.Key.Contains(pair.Key)) { source = sccstate.Value; break; } } foreach (Distribution distribution in pair.Value) { Distribution disNew = new Distribution(distribution.Event); foreach (KeyValuePair<double, MDPState> state in distribution.States) { foreach (KeyValuePair<HashSet<string>, MDPState> sccstate in sccs) { if (sccstate.Key.Contains(state.Value.ID)) { disNew.AddProbStatePair(state.Key, sccstate.Value); VerificationOutput.ReducedMDPTransitions++; break; } } } toReturn.AddDistribution(source.ID, disNew); } } VerificationOutput.ReducedMDPStates = toReturn.States.Count; return toReturn; }
protected MDP BuildMDP_SCC_Cut() { Stack<KeyValuePair<MDPConfiguration, MDPState>> working = new Stack<KeyValuePair<MDPConfiguration, MDPState>>(1024); string initID = InitialStep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair<MDPConfiguration, MDPState>(InitialStep as MDPConfiguration, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); Stack<MDPState> stepStack = new Stack<MDPState>(1024); //MDPState2DRAStateMapping = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); const int VISITED_NOPREORDER = -1; const int SCC_FOUND = -2; //DFS data, which mapping each state to an int[] of size 3, first is the pre-order, second is the lowlink, last one is DRAState Dictionary<string, int[]> DFSData = new Dictionary<string, int[]>(Ultility.Ultility.MC_INITIAL_SIZE); DFSData.Add(initID, new int[] { VISITED_NOPREORDER, 0 }); //build the MDP while identifying the SCCs List<HashSet<MDPState>> SCCs = new List<HashSet<MDPState>>(); //Dictionary<int, List<MDPState>> scc2out = new Dictionary<int, List<MDPState>>(); List<HashSet<MDPState>> scc2out = new List<HashSet<MDPState>>(); List<HashSet<MDPState>> scc2input = new List<HashSet<MDPState>>(); //Dictionary<int, HashSet<MDPState>> scc2input = new Dictionary<int, HashSet<MDPState>>(); //Dictionary<string, int> preorder = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); //Dictionary<string, int> lowlink = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); int preordercounter = 0; Dictionary<string, List<MDPConfiguration>> ExpendedNode = new Dictionary<string, List<MDPConfiguration>>(); bool reachTarget = true; do { if (CancelRequested) { this.VerificationOutput.NoOfStates = this.VerificationOutput.NoOfStates + mdp.States.Count; return mdp; } KeyValuePair<MDPConfiguration, MDPState> pair = working.Peek(); MDPConfiguration evt = pair.Key; //int DRAState = pair.Key.state; string currentID = pair.Key.GetID(); MDPState currentState = pair.Value; List<Distribution> outgoing = currentState.Distributions; //if (!preorder.ContainsKey(currentID)) //{ // preorder.Add(currentID, preorderCounter); // preorderCounter++; //} int[] nodeData = DFSData[currentID]; if (nodeData[0] == VISITED_NOPREORDER) { nodeData[0] = preordercounter; preordercounter++; } bool done = true; if (ExpendedNode.ContainsKey(currentID)) { if (reachTarget) { currentState.ReachTarget = reachTarget; } else { reachTarget = currentState.ReachTarget; } List<MDPConfiguration> list = ExpendedNode[currentID]; if (list.Count > 0) { for (int k = list.Count - 1; k >= 0; k--) { MDPConfiguration step = list[k]; string stepID = step.GetID(); //if (!preorder.ContainsKey(stepID)) if (DFSData[stepID][0] == VISITED_NOPREORDER) { if (done) { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, mdp.States[stepID])); done = false; list.RemoveAt(k); } } else { MDPState s = mdp.States[stepID]; if (s.ReachTarget) { reachTarget = true; currentState.ReachTarget = reachTarget; if (s.SCCIndex >= 0) { scc2input[s.SCCIndex].Add(s); } } list.RemoveAt(k); } } } } else { reachTarget = false; int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //MDPConfiguration[] steps = evt.MakeOneMoveLocal(); IEnumerable<MDPConfiguration> steps = evt.MakeOneMoveLocal(); //NOTE: here we play a trick for deadlock case: if a deadlock exist in the MDP, we will make a //self loop transition to remove the deadlock. Deadlock is meaningless in MDP. if (evt.IsDeadLock) { //List<MDPConfiguration> stepsList = new List<MDPConfiguration>(steps); working.Pop(); nodeData[0] = SCC_FOUND; continue; //stepsList.Add(CreateSelfLoopStep(evt)); //steps = stepsList.ToArray(); //HasDeadLock = true; } List<MDPConfiguration> stepsList = new List<MDPConfiguration>(steps); //List<PCSPEventDRAPair> product = Next(steps, DRAState); this.VerificationOutput.Transitions += stepsList.Count; for (int k = stepsList.Count - 1; k >= 0; k--) { MDPConfiguration step = stepsList[k]; string tmp = step.GetID(); bool target = false; ExpressionValue v = EvaluatorDenotational.Evaluate(ReachableStateCondition, step.GlobalEnv); MDPState nextState; if ((v as BoolConstant).Value) { target = true; if (!DFSData.Keys.Contains(tmp)) { DFSData.Add(tmp, new int[] { SCC_FOUND, SCC_FOUND }); // nextState = new MDPState(tmp); // mdp.States.Add(tmp, nextState); //preordercounter++; } //else if(DFSData[tmp][0] == VISITED_NOPREORDER) //{ // DFSData[tmp][0] = preordercounter; // preordercounter++; //} } //int nextIndex = VisitedWithID.Count; int[] data; if (mdp.States.TryGetValue(tmp, out nextState)) { nextState.TrivialPre = false; DFSData.TryGetValue(tmp, out data); if (!target && data[0] == VISITED_NOPREORDER) { //if (mdp.States.TryGetValue(tmp, out nextState)) //{ // if (!preorder.ContainsKey(tmp)) // { if (done) { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, nextState)); done = false; stepsList.RemoveAt(k); } else { stepsList[k] = step; } } else { stepsList.RemoveAt(k); MDPState s = mdp.States[tmp]; if (s.ReachTarget) { reachTarget = true; currentState.ReachTarget = reachTarget; if (s.SCCIndex >= 0 && !scc2input[s.SCCIndex].Contains(s)) { scc2input[s.SCCIndex].Add(s); } } } } else { if (!target) { DFSData.Add(tmp, new int[] { VISITED_NOPREORDER, 0 }); } nextState = new MDPState(tmp); mdp.States.Add(tmp, nextState); if (done) { if (target) { mdp.AddTargetStates(nextState); reachTarget = true; } else { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, nextState)); } done = false; stepsList.RemoveAt(k); } else { if (target) { stepsList.RemoveAt(k); mdp.AddTargetStates(nextState); reachTarget = true; } else { stepsList[k] = step; } } } MDPConfiguration pstep = step; if (pstep.DisIndex == -1) { if (currentDistriIndex >= 0) { //note here no pre is stored currentState.Distributions.Add(newDis); newDis = new Distribution(Constants.TAU); } if (nextState.ID != currentID)//note here self loop distribution is removed; so just used for PMAX { Distribution newTrivialDis = new Distribution(pstep.Event, nextState); currentState.Distributions.Add(newTrivialDis); } } else if (currentDistriIndex != -1 && pstep.DisIndex != currentDistriIndex) { //note here no pre is stored currentState.Distributions.Add(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(pstep.Probability, nextState); nextState.TrivialPre = false; } else { newDis.AddProbStatePair(pstep.Probability, nextState); nextState.TrivialPre = false; } currentDistriIndex = pstep.DisIndex; } if (currentDistriIndex >= 0) { currentState.Distributions.Add(newDis); } ExpendedNode.Add(currentID, stepsList); } if (done) { int lowlinkV = nodeData[0]; int preorderV = lowlinkV; bool selfLoop = false; foreach (Distribution list in outgoing) { foreach (KeyValuePair<double, MDPState> state in list.States) { string w = state.Value.ID; if (w == currentID) { selfLoop = true; } int[] wdata = DFSData[w]; if (wdata[0] != SCC_FOUND) { if (wdata[0] > preorderV) { lowlinkV = Math.Min(lowlinkV, wdata[1]); } else { lowlinkV = Math.Min(lowlinkV, wdata[0]); } } } } nodeData[1] = lowlinkV; working.Pop(); if (lowlinkV == preorderV) { if (currentState.ReachTarget) { HashSet<MDPState> SCC = new HashSet<MDPState>(); //List<HashSet<MDPState>> Groupings = new List<HashSet<MDPState>>(); //HashSet<MDPState> Grouping = new HashSet<MDPState>(); //bool reduceScc = false; //Grouping.Add(currentState); SCC.Add(currentState); //int count = 1; //dtmcSCCstates.Add(currentID); nodeData[0] = SCC_FOUND; while (stepStack.Count > 0 && DFSData[stepStack.Peek().ID][0] > preorderV) { MDPState s = stepStack.Pop(); s.ReachTarget = true; string sID = s.ID; //Grouping.Add(s); SCC.Add(s); //dtmcSCCstates.Add(sID); DFSData[sID][0] = SCC_FOUND; //count++; } if (SCC.Count > 1 || selfLoop) { SCCs.Add(SCC); HashSet<MDPState> inputs = new HashSet<MDPState>(); inputs.Add(currentState); int Count = SCCs.Count; foreach (MDPState state in SCC) { state.SCCIndex = Count - 1; } scc2input.Add(inputs); HashSet<MDPState> outputs = new HashSet<MDPState>(); //input.Add(currentState); foreach (MDPState state in SCC) { foreach (Distribution distr in state.Distributions) { foreach (KeyValuePair<double, MDPState> KVpair in distr.States) { if (!SCC.Contains(KVpair.Value)) { outputs.Add(KVpair.Value); } } } } //List<MDPState> outofscc = new List<MDPState>(outputs); scc2out.Add(outputs); } //} } else { //dtmcSCCstates.Add(currentID); nodeData[0] = SCC_FOUND; while (stepStack.Count > 0 && DFSData[stepStack.Peek().ID][0] > preorderV) { string sID = stepStack.Pop().ID; DFSData[sID][0] = SCC_FOUND; } } } else { stepStack.Push(pair.Value); } } } while (working.Count > 0); RemoveOneTran(SCCs, scc2input); for (int i = 0; i < SCCs.Count; i++) { FindMECDistribution(SCCs[i]); } if(BFS_CUT) { BFS_Cut(SCCs, scc2out, scc2input); } else { //DFS_CutAutoAdjustTreeLike(SCCs, scc2out, scc2input); DFS_CutAutoAdjust(SCCs, scc2out, scc2input); //DFS_Cut(SCCs, scc2out, scc2input); } VerificationOutput.NoOfStates = VerificationOutput.NoOfStates + mdp.States.Count; int counter = 0; BuildPRE(mdp, ref counter); return mdp; }
protected MDP BuildMDP_SCC_Cut_Mutlicores() { Stack<KeyValuePair<MDPConfiguration, MDPState>> working = new Stack<KeyValuePair<MDPConfiguration, MDPState>>(1024); string initID = InitialStep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair<MDPConfiguration, MDPState>(InitialStep as MDPConfiguration, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); Stack<MDPState> stepStack = new Stack<MDPState>(1024); //MDPState2DRAStateMapping = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); const int VISITED_NOPREORDER = -1; const int SCC_FOUND = -2; const int SCCBound = 20; //DFS data, which mapping each state to an int[] of size 3, first is the pre-order, second is the lowlink, last one is DRAState Dictionary<string, int[]> DFSData = new Dictionary<string, int[]>(Ultility.Ultility.MC_INITIAL_SIZE); DFSData.Add(initID, new int[] { VISITED_NOPREORDER, 0 }); //build the MDP while identifying the SCCs List<List<MDPState>> SCCs = new List<List<MDPState>>(64); Dictionary<int, List<MDPState>> scc2out = new Dictionary<int, List<MDPState>>(); Dictionary<int, HashSet<MDPState>> scc2input = new Dictionary<int, HashSet<MDPState>>(); //Dictionary<string, int> preorder = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); //Dictionary<string, int> lowlink = new Dictionary<string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE); int preordercounter = 0; Dictionary<string, List<MDPConfiguration>> ExpendedNode = new Dictionary<string, List<MDPConfiguration>>(); bool reachTarget = true; do { if (CancelRequested) { this.VerificationOutput.NoOfStates = this.VerificationOutput.NoOfStates + mdp.States.Count; return mdp; } KeyValuePair<MDPConfiguration, MDPState> pair = working.Peek(); MDPConfiguration evt = pair.Key; //int DRAState = pair.Key.state; string currentID = pair.Key.GetID(); MDPState currentState = pair.Value; List<Distribution> outgoing = currentState.Distributions; //if (!preorder.ContainsKey(currentID)) //{ // preorder.Add(currentID, preorderCounter); // preorderCounter++; //} int[] nodeData = DFSData[currentID]; if (nodeData[0] == VISITED_NOPREORDER) { nodeData[0] = preordercounter; preordercounter++; } bool done = true; if (ExpendedNode.ContainsKey(currentID)) { if (reachTarget) { currentState.ReachTarget = reachTarget; } else { reachTarget = currentState.ReachTarget; } List<MDPConfiguration> list = ExpendedNode[currentID]; if (list.Count > 0) { for (int k = list.Count - 1; k >= 0; k--) { MDPConfiguration step = list[k]; string stepID = step.GetID(); //if (!preorder.ContainsKey(stepID)) if (DFSData[stepID][0] == VISITED_NOPREORDER) { if (done) { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, mdp.States[stepID])); done = false; list.RemoveAt(k); } } else { MDPState s = mdp.States[stepID]; if (s.ReachTarget) { reachTarget = true; currentState.ReachTarget = reachTarget; if (s.SCCIndex >= 0) { scc2input[s.SCCIndex].Add(s); } } list.RemoveAt(k); } } } } else { reachTarget = false; int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //MDPConfiguration[] steps = evt.MakeOneMoveLocal(); IEnumerable<MDPConfiguration> steps = evt.MakeOneMoveLocal(); //NOTE: here we play a trick for deadlock case: if a deadlock exist in the MDP, we will make a //self loop transition to remove the deadlock. Deadlock is meaningless in MDP. if (evt.IsDeadLock) { //List<MDPConfiguration> stepsList = new List<MDPConfiguration>(steps); working.Pop(); nodeData[0] = SCC_FOUND; continue; //stepsList.Add(CreateSelfLoopStep(evt)); //steps = stepsList.ToArray(); //HasDeadLock = true; } List<MDPConfiguration> stepsList = new List<MDPConfiguration>(steps); //List<PCSPEventDRAPair> product = Next(steps, DRAState); this.VerificationOutput.Transitions += stepsList.Count; for (int k = stepsList.Count - 1; k >= 0; k--) { MDPConfiguration step = stepsList[k]; string tmp = step.GetID(); bool target = false; ExpressionValue v = EvaluatorDenotational.Evaluate(ReachableStateCondition, step.GlobalEnv); MDPState nextState; if ((v as BoolConstant).Value) { target = true; if (!DFSData.Keys.Contains(tmp)) { DFSData.Add(tmp, new int[] { SCC_FOUND, SCC_FOUND }); // nextState = new MDPState(tmp); // mdp.States.Add(tmp, nextState); //preordercounter++; } //else if(DFSData[tmp][0] == VISITED_NOPREORDER) //{ // DFSData[tmp][0] = preordercounter; // preordercounter++; //} } //int nextIndex = VisitedWithID.Count; int[] data; if (mdp.States.TryGetValue(tmp, out nextState)) { DFSData.TryGetValue(tmp, out data); if (!target && data[0] == VISITED_NOPREORDER) { //if (mdp.States.TryGetValue(tmp, out nextState)) //{ // if (!preorder.ContainsKey(tmp)) // { if (done) { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, nextState)); done = false; stepsList.RemoveAt(k); } else { stepsList[k] = step; } } else { stepsList.RemoveAt(k); MDPState s = mdp.States[tmp]; if (s.ReachTarget) { reachTarget = true; currentState.ReachTarget = reachTarget; if (s.SCCIndex >= 0) { scc2input[s.SCCIndex].Add(s); } } } } else { if (!target) { DFSData.Add(tmp, new int[] { VISITED_NOPREORDER, 0 }); } nextState = new MDPState(tmp); mdp.States.Add(tmp, nextState); if (done) { if (target) { mdp.AddTargetStates(nextState); reachTarget = true; } else { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, nextState)); } done = false; stepsList.RemoveAt(k); } else { if (target) { stepsList.RemoveAt(k); mdp.AddTargetStates(nextState); reachTarget = true; } else { stepsList[k] = step; } } } MDPConfiguration pstep = step; if (pstep.DisIndex == -1) { if (currentDistriIndex >= 0) { //currentState.AddDistribution(newDis); //note here no pre is stored currentState.Distributions.Add(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(pstep.Event, nextState); //currentState.AddDistribution(newTrivialDis); currentState.Distributions.Add(newTrivialDis); } else if (currentDistriIndex != -1 && pstep.DisIndex != currentDistriIndex) { //currentState.AddDistribution(newDis); currentState.Distributions.Add(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(pstep.Probability, nextState); } else { newDis.AddProbStatePair(pstep.Probability, nextState); } currentDistriIndex = pstep.DisIndex; } if (currentDistriIndex >= 0) { //currentState.AddDistribution(newDis); currentState.Distributions.Add(newDis); } ExpendedNode.Add(currentID, stepsList); } if (done) { int lowlinkV = nodeData[0]; int preorderV = lowlinkV; bool selfLoop = false; foreach (Distribution list in outgoing) { foreach (KeyValuePair<double, MDPState> state in list.States) { string w = state.Value.ID; if (w == currentID) { selfLoop = true; } //if (!MDPState2DRAStateMapping.ContainsKey(w)) //{ // if (preorder[w] > preorderV) // { // lowlinkV = Math.Min(lowlinkV, lowlink[w]); // } // else // { // lowlinkV = Math.Min(lowlinkV, preorder[w]); // } //} int[] wdata = DFSData[w]; if (wdata[0] != SCC_FOUND) { if (wdata[0] > preorderV) { lowlinkV = Math.Min(lowlinkV, wdata[1]); } else { lowlinkV = Math.Min(lowlinkV, wdata[0]); } } } } nodeData[1] = lowlinkV; working.Pop(); if (lowlinkV == preorderV) { if (currentState.ReachTarget) { List<MDPState> SCC = new List<MDPState>(); //List<HashSet<MDPState>> Groupings = new List<HashSet<MDPState>>(); HashSet<MDPState> Grouping = new HashSet<MDPState>(); //bool reduceScc = false; Grouping.Add(currentState); SCC.Add(currentState); //int count = 1; //dtmcSCCstates.Add(currentID); nodeData[0] = SCC_FOUND; while (stepStack.Count > 0 && DFSData[stepStack.Peek().ID][0] > preorderV) { MDPState s = stepStack.Pop(); s.ReachTarget = true; string sID = s.ID; //Grouping.Add(s); SCC.Add(s); //dtmcSCCstates.Add(sID); DFSData[sID][0] = SCC_FOUND; //count++; } if (SCC.Count > 1 || selfLoop) { SCCs.Add(SCC); HashSet<MDPState> inputs = new HashSet<MDPState>(); inputs.Add(currentState); int Count = SCCs.Count; foreach (var state in SCC) { state.SCCIndex = Count - 1; } scc2input.Add(currentState.SCCIndex, inputs); HashSet<MDPState> outputs = new HashSet<MDPState>(); //input.Add(currentState); foreach (MDPState state in SCC) { foreach (var distr in state.Distributions) { foreach (var KVpair in distr.States) { if (!SCC.Contains(KVpair.Value)) { outputs.Add(KVpair.Value); } } } } List<MDPState> outofscc = new List<MDPState>(outputs); scc2out.Add(SCCs.IndexOf(SCC), outofscc); } //} } else { //dtmcSCCstates.Add(currentID); nodeData[0] = SCC_FOUND; while (stepStack.Count > 0 && DFSData[stepStack.Peek().ID][0] > preorderV) { string sID = stepStack.Pop().ID; DFSData[sID][0] = SCC_FOUND; } } } else { stepStack.Push(pair.Value); } } } while (working.Count > 0); //List<string> EndComponents = new List<string>(SCCs.Count); //foreach (List<MDPState> scc in SCCs) var localMapSCCsWithOutputSmallSCC = new Dictionary<List<MDPState>, List<MDPState>>(); while (SCCs.Count > 0) { for (int i = SCCs.Count - 1; i >= 0; i--) { List<MDPState> SCC = SCCs[i]; int index; //foreach(var state in SCC) //{ // index = state.SCCIndex; // break; //} //FindMECDistribution(SCC); if (SCC.Count <= SCCBound) { // SCCReductionGaussianSparseMatrix(new List<MDPState>(SCC), scc2out[index]); localMapSCCsWithOutputSmallSCC.Add(new List<MDPState>(SCC), scc2out[i]); SCCs.RemoveAt(i); } else { List<MDPState> SCCi = new List<MDPState>(SCCs[i]); int Counter = 0; var localMapSCCsWithOutputSubCutSCC = new Dictionary<List<MDPState>, List<MDPState>>(); while (Counter < SCCi.Count - SCCBound) { List<MDPState> group = SCCi.GetRange(Counter, SCCBound); HashSet<MDPState> outputs = new HashSet<MDPState>(); //input.Add(currentState); foreach (MDPState state in group) { foreach (var Distr in state.Distributions) { foreach (KeyValuePair<double, MDPState> KVpair in Distr.States) { MDPState nextState = KVpair.Value; if (!group.Contains(nextState)) { outputs.Add(nextState); } } } } // List<MDPState> output = new List<MDPState>(outputs); //SCCReductionGaussianSparseMatrix(group, output); localMapSCCsWithOutputSubCutSCC.Add(group, new List<MDPState>(outputs)); //SCCReduction(group, output); Counter += SCCBound; } Parallel.ForEach(localMapSCCsWithOutputSubCutSCC, subscc2Out => SCCReductionGaussianSparseMatrix(subscc2Out.Key, subscc2Out.Value, true)); List<MDPState> Group = SCCi.GetRange(Counter, SCCi.Count - Counter); HashSet<MDPState> Outputs = new HashSet<MDPState>(); //input.Add(currentState); foreach (MDPState state in Group) { foreach (var Distr in state.Distributions) { foreach (var KVpair in Distr.States) { MDPState nextState = KVpair.Value; if (!Group.Contains(nextState)) { Outputs.Add(nextState); } } } } List<MDPState> Output = new List<MDPState>(Outputs); //GL: TODO: this one is not in parallel SCCReductionGaussianSparseMatrix(Group, Output, true); //search for the useful subset of SCC that connecting to input states after reduction. HashSet<MDPState> Working = scc2input[i]; HashSet<MDPState> visited = new HashSet<MDPState>(); do { HashSet<MDPState> newWorking = new HashSet<MDPState>(); foreach (var state in Working) { visited.Add(state); foreach (var distr in state.Distributions) { foreach (var tran in distr.States) { MDPState tranV = tran.Value; if (!visited.Contains(tranV) && SCCs[i].Contains(tranV)) { newWorking.Add(tranV); } } } } Working = newWorking; } while (Working.Count > 0); if (visited.Count > SCCBound && visited.Count < SCCs[i].Count) { SCCs[i] = new List<MDPState>(visited); } else { //List<MDPState> scc = new List<MDPState>(SCCs[i]); // SCCReductionGaussianSparseMatrix(scc, scc2out[index]); //SCCReductionMatlab(scc, scc2out[i]); localMapSCCsWithOutputSmallSCC.Add(new List<MDPState>(visited), scc2out[i]); SCCs.RemoveAt(i); } } } } Parallel.ForEach(localMapSCCsWithOutputSmallSCC, subscc2Out => SCCReductionGaussianSparseMatrix(subscc2Out.Key, subscc2Out.Value, true)); VerificationOutput.NoOfStates = VerificationOutput.NoOfStates + mdp.States.Count; int counter = 0; BuildPRE(mdp, ref counter); return mdp; }
protected MDP BuildMDP() { Stack<KeyValuePair<MDPConfiguration, MDPState>> working = new Stack<KeyValuePair<MDPConfiguration, MDPState>>(1024); string initID = InitialStep.GetID(); MDPState init = new MDPState(initID); working.Push(new KeyValuePair<MDPConfiguration, MDPState>(InitialStep as MDPConfiguration, init)); MDP mdp = new MDP(init, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE); do { if (CancelRequested) { VerificationOutput.NoOfStates = mdp.States.Count; return mdp; } KeyValuePair<MDPConfiguration, MDPState> current = working.Pop(); IEnumerable<MDPConfiguration> list = current.Key.MakeOneMoveLocal(); VerificationOutput.Transitions += list.Count(); int currentDistriIndex = -1; Distribution newDis = new Distribution(Constants.TAU); //for (int i = 0; i < list.Count; i++) foreach (var step in list) { //MDPConfiguration step = list[i]; string stepID = step.GetID(); MDPState nextState; if (!mdp.States.TryGetValue(stepID, out nextState)) { nextState = new MDPState(stepID); mdp.AddState(nextState); ExpressionValue v = EvaluatorDenotational.Evaluate(ReachableStateCondition, step.GlobalEnv); if ((v as BoolConstant).Value) { mdp.AddTargetStates(nextState); } else { working.Push(new KeyValuePair<MDPConfiguration, MDPState>(step, nextState)); } } if (step.DisIndex == -1) { if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); } Distribution newTrivialDis = new Distribution(step.Event); newTrivialDis.AddProbStatePair(1, nextState); current.Value.AddDistribution(newTrivialDis); } else if (currentDistriIndex != -1 && step.DisIndex != currentDistriIndex) { current.Value.AddDistribution(newDis); newDis = new Distribution(Constants.TAU); newDis.AddProbStatePair(step.Probability, nextState); } else { newDis.AddProbStatePair(step.Probability, nextState); } currentDistriIndex = step.DisIndex; } if (currentDistriIndex >= 0) { current.Value.AddDistribution(newDis); } } while (working.Count > 0); VerificationOutput.NoOfStates = mdp.States.Count; //mdp.BackUpTargetStates(); return mdp; }