Example #1
0
        public MDPStat BuildQuotientMDP(VerificationOutput VerificationOutput)
        {
            //return this;

            MDPStat toReturn = new MDPStat(Precision, MAX_DIFFERENCE);

            //todo change to set
            List <KeyValuePair <string, string> > BoundaryOneTransition = new List <KeyValuePair <string, string> >();

            //todo change to set
            List <DistributionStat> ProbTransitions = new List <DistributionStat>();

            Dictionary <string, List <DistributionStat> > GlobalProbTransitions = new Dictionary <string, List <DistributionStat> >();

            StringDictionary <bool> visited = new StringDictionary <bool>(States.Count);
            List <KeyValuePair <HashSet <string>, MDPStateStat> > sccs = new List <KeyValuePair <HashSet <string>, MDPStateStat> >();

            Dictionary <string, int> preorder = new Dictionary <string, int>();
            Dictionary <string, int> lowlink  = new Dictionary <string, int>();
            //HashSet<string> scc_found = new HashSet<string>();
            Stack <MDPStateStat> TaskStack = new Stack <MDPStateStat>();

            //Dictionary<string, List<string>> OutgoingTransitionTable = new Dictionary<string, List<string>>();
            Stack <MDPStateStat> stepStack = new Stack <MDPStateStat>(1024);

            visited.Add(InitState.ID, false);
            TaskStack.Push(InitState);

            //# Preorder counter
            int preor = 0;

            do
            {
                while (TaskStack.Count > 0)
                {
                    MDPStateStat 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 <DistributionStat> list         = pair.Distributions;
                    List <MDPStateStat>     nonProbTrans = new List <MDPStateStat>();
                    List <DistributionStat> ProbTrans    = new List <DistributionStat>();

                    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--)
                    {
                        MDPStateStat 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);
                            }

                            MDPStateStat newstate = new MDPStateStat(toReturn.States.Count.ToString());
                            if (scc.Count > 1 || (scc.Count == 1 && selfLoop))
                            {
                                newstate.AddDistribution(new DistributionStat(Constants.TAU, newstate)); //add self loop: sun jun
                            }
                            sccs.Add(new KeyValuePair <HashSet <string>, MDPStateStat>(scc, newstate));

                            toReturn.AddState(newstate);

                            if (scc.Contains(InitState.ID))
                            {
                                toReturn.SetInit(newstate);
                            }

                            foreach (MDPStateStat state in TargetStates)
                            {
                                if (scc.Contains(state.ID))
                                {
                                    toReturn.AddTargetStates(newstate);
                                }
                            }
                        }
                        else
                        {
                            stepStack.Push(pair);
                        }
                    }
                }

                if (ProbTransitions.Count > 0)
                {
                    foreach (DistributionStat step in ProbTransitions)
                    {
                        foreach (KeyValuePair <double, MDPStateStat> 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)
            {
                MDPStateStat source = null;
                MDPStateStat target = null;

                foreach (KeyValuePair <HashSet <string>, MDPStateStat> 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 DistributionStat(Constants.TAU, target));
                VerificationOutput.ReducedMDPTransitions++;
            }

            foreach (KeyValuePair <string, List <DistributionStat> > pair in GlobalProbTransitions)
            {
                MDPStateStat source = null;

                foreach (KeyValuePair <HashSet <string>, MDPStateStat> sccstate in sccs)
                {
                    if (sccstate.Key.Contains(pair.Key))
                    {
                        source = sccstate.Value;
                        break;
                    }
                }

                foreach (DistributionStat distribution in pair.Value)
                {
                    DistributionStat disNew = new DistributionStat(distribution.Event);
                    foreach (KeyValuePair <double, MDPStateStat> state in distribution.States)
                    {
                        foreach (KeyValuePair <HashSet <string>, MDPStateStat> 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);
        }
Example #2
0
        public DistributionStat calcGroupedDistr(List<HashSet<MDPStateStat>> ListHashMDP, DistributionStat Distr, MDPStateStat Initial, MDPStateStat Target, MDPStateStat Safe, MDPStat mdp)
        {
            DistributionStat toReturn = new DistributionStat(Distr.Event);

            double toInit = 0;
            double toTarget = 0;
            double[] combined = new double[ListHashMDP.Count];
            //MDPStateStat[] groupstates = new MDPStateStat[seperation.Count];

            foreach (KeyValuePair<double, MDPStateStat> 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)
                {
                    MDPStateStat 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;
        }
Example #3
0
        public DistributionStat calcGroupedDistr(List <HashSet <MDPStateStat> > ListHashMDP, DistributionStat Distr, MDPStateStat Initial, MDPStateStat Target, MDPStateStat Safe, MDPStat mdp)
        {
            DistributionStat toReturn = new DistributionStat(Distr.Event);

            double toInit   = 0;
            double toTarget = 0;

            double[] combined = new double[ListHashMDP.Count];
            //MDPStateStat[] groupstates = new MDPStateStat[seperation.Count];

            foreach (KeyValuePair <double, MDPStateStat> 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)
                {
                    MDPStateStat 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);
        }
Example #4
0
        public MDPStat BuildQuotientMDP(VerificationOutput VerificationOutput)
        {
            //return this;

            MDPStat toReturn = new MDPStat(Precision, MAX_DIFFERENCE);

            //todo change to set
            List<KeyValuePair<string, string>> BoundaryOneTransition = new List<KeyValuePair<string, string>>();

            //todo change to set
            List<DistributionStat> ProbTransitions = new List<DistributionStat>();

            Dictionary<string, List<DistributionStat>> GlobalProbTransitions = new Dictionary<string, List<DistributionStat>>();

            StringDictionary<bool> visited = new StringDictionary<bool>(States.Count);
            List<KeyValuePair<HashSet<string>, MDPStateStat>> sccs = new List<KeyValuePair<HashSet<string>, MDPStateStat>>();

            Dictionary<string, int> preorder = new Dictionary<string, int>();
            Dictionary<string, int> lowlink = new Dictionary<string, int>();
            //HashSet<string> scc_found = new HashSet<string>();
            Stack<MDPStateStat> TaskStack = new Stack<MDPStateStat>();

            //Dictionary<string, List<string>> OutgoingTransitionTable = new Dictionary<string, List<string>>();
            Stack<MDPStateStat> stepStack = new Stack<MDPStateStat>(1024);

            visited.Add(InitState.ID, false);
            TaskStack.Push(InitState);

            //# Preorder counter
            int preor = 0;

            do
            {
                while (TaskStack.Count > 0)
                {
                    MDPStateStat 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<DistributionStat> list = pair.Distributions;
                    List<MDPStateStat> nonProbTrans = new List<MDPStateStat>();
                    List<DistributionStat> ProbTrans = new List<DistributionStat>();

                    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--)
                    {
                        MDPStateStat 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);
                            }

                            MDPStateStat newstate = new MDPStateStat(toReturn.States.Count.ToString());
                            if (scc.Count > 1 || (scc.Count == 1 && selfLoop))
                            {
                                newstate.AddDistribution(new DistributionStat(Constants.TAU, newstate)); //add self loop: sun jun
                            }
                            sccs.Add(new KeyValuePair<HashSet<string>, MDPStateStat>(scc, newstate));

                            toReturn.AddState(newstate);

                            if (scc.Contains(InitState.ID))
                            {
                                toReturn.SetInit(newstate);
                            }

                            foreach (MDPStateStat state in TargetStates)
                            {
                                if (scc.Contains(state.ID))
                                {
                                    toReturn.AddTargetStates(newstate);
                                }
                            }
                        }
                        else
                        {
                            stepStack.Push(pair);
                        }
                    }
                }

                if (ProbTransitions.Count > 0)
                {
                    foreach (DistributionStat step in ProbTransitions)
                    {
                        foreach (KeyValuePair<double, MDPStateStat> 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)
            {
                MDPStateStat source = null;
                MDPStateStat target = null;

                foreach (KeyValuePair<HashSet<string>, MDPStateStat> 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 DistributionStat(Constants.TAU, target));
                VerificationOutput.ReducedMDPTransitions++;

            }

            foreach (KeyValuePair<string, List<DistributionStat>> pair in GlobalProbTransitions)
            {
                MDPStateStat source = null;

                foreach (KeyValuePair<HashSet<string>, MDPStateStat> sccstate in sccs)
                {
                    if (sccstate.Key.Contains(pair.Key))
                    {
                        source = sccstate.Value;
                        break;
                    }
                }

                foreach (DistributionStat distribution in pair.Value)
                {
                    DistributionStat disNew = new DistributionStat(distribution.Event);
                    foreach (KeyValuePair<double, MDPStateStat> state in distribution.States)
                    {
                        foreach (KeyValuePair<HashSet<string>, MDPStateStat> 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;
        }