Exemplo n.º 1
0
        public static EventBAPairSafetyPCSP GetInitialPairs(BuchiAutomata BA, MDPConfiguration initialStep)
        {
            List <string> intialBAStates = new List <string>();

            //HashSet<string> existed = new HashSet<string>();

            foreach (string s in BA.InitialStates)
            {
                List <string> next = BA.MakeOneMove(s, initialStep);

                foreach (string var in next)
                {
                    //if (!existed.Contains(var))
                    //{
                    //    existed.Add(var);
                    //    intialBAStates.Add(var);
                    //}
                    if (!intialBAStates.Contains(var))
                    {
                        intialBAStates.Add(var);
                    }
                }
            }

            return(new EventBAPairSafetyPCSP(initialStep, intialBAStates));
        }
Exemplo n.º 2
0
        public static List <LocalPair> NextLocal(BuchiAutomata BA, IEnumerable <ConfigurationBase> steps, string BAState)
        {
            List <LocalPair> product = new List <LocalPair>(steps.Count() * BA.States.Length);

            //for (int i = 0; i < steps.Length; i++)
            foreach (var step in steps)
            {
                //ConfigurationBase step = steps[i];
                List <string> states = BA.MakeOneMove(BAState, step);

                for (int j = 0; j < states.Count; j++)
                {
                    product.Add(new LocalPair(step, states[j]));
                }
            }

            return(product);
        }
Exemplo n.º 3
0
        public static List <EventBAPairSafetyPCSP> Next(BuchiAutomata BA, MDPConfiguration[] steps, List <string> BAStates)
        {
            List <EventBAPairSafetyPCSP> product = new List <EventBAPairSafetyPCSP>(steps.Length * BA.States.Length);

            for (int i = 0; i < steps.Length; i++)
            {
                List <string> targetStates = new List <string>();

                foreach (string state in BAStates)
                {
                    List <string> states = BA.MakeOneMove(state, steps[i]);
                    Common.Classes.Ultility.Ultility.Union(targetStates, states);
                }

                product.Add(new EventBAPairSafetyPCSP(steps[i], targetStates));
            }

            return(product);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Given one environment, get the initial states of the product of the system and the automata. Notice that the automata
        /// is allowed to make one move first. This is necessary to check the very first state of the system.
        /// </summary>
        /// <param name="initialStep"></param>
        /// <returns></returns>
        public static List <LocalPair> GetInitialPairsLocal(BuchiAutomata BA, ConfigurationBase initialStep)
        {
            List <LocalPair> toReturn = new List <LocalPair>();
            HashSet <string> existed  = new HashSet <string>();

            foreach (string s in BA.InitialStates)
            {
                List <string> next = BA.MakeOneMove(s, initialStep);

                foreach (string var in next)
                {
                    if (existed.Add(var))
                    {
                        toReturn.Add(new LocalPair(initialStep, var));
                    }
                }
            }

            return(toReturn);
        }
Exemplo n.º 5
0
        /// <summary>
        /// On the fly feasible checking,
        /// https://networkx.lanl.gov/Reference/networkx.component-pysrc.html#strongly_connected_components
        //  Returns list of strongly connected components in G.  Uses Tarjan's algorithm with Nuutila's modifications.
        //  Nonrecursive version of algorithm.
        //
        //  References:
        //
        //  R. Tarjan (1972). Depth-first search and linear graph algorithms. SIAM Journal of Computing 1(2):146-160.
        //
        //  E. Nuutila and E. Soisalon-Soinen (1994). On finding the strongly connected components in a directed graph.
        //  Information Processing Letters 49(1): 9-14.
        //  http://coblitz.codeen.org:3125/citeseer.ist.psu.edu/cache/papers/cs/549/http:zSzzSzwww.cs.hut.fizSz~enuzSzpszSzipl-scc.pdf/nuutila94finding.pdf
        //       neighbors=G.neighbors
        //       preorder={}
        //       lowlink={}
        //       scc_found={}
        //       scc_queue = []
        //       scc_list=[]
        //       i=0     # Preorder counter
        //       for source in G:
        //           if source not in scc_found:
        //               queue=[source]
        //               while queue:
        //                   v=queue[-1]
        //                   if v not in preorder:
        //                       i=i+1
        //                       preorder[v]=i
        //                   done=1
        //                   for w in neighbors(v):
        //                       if w not in preorder:
        //                           queue.append(w)
        //                           done=0
        //                           break
        //                   if done==1:
        //                       lowlink[v]=preorder[v]
        //                       for w in neighbors(v):
        //                           if w not in scc_found:
        //                               if preorder[w]>preorder[v]:
        //                                   lowlink[v]=min([lowlink[v],lowlink[w]])
        //                               else:
        //                                   lowlink[v]=min([lowlink[v],preorder[w]])
        //                       queue.pop()
        //                       if lowlink[v]==preorder[v]:
        //                           scc_found[v]=True
        //                           scc=[v]
        //                           while scc_queue and preorder[scc_queue[-1]]>preorder[v]:
        //                               k=scc_queue.pop()
        //                               scc_found[k]=True
        //                               scc.append(k)
        //                           scc_list.append(scc)
        //                       else:
        //                           scc_queue.append(v)
        //       scc_list.sort(lambda x, y: cmp(len(y),len(x)))
        //       return scc_list
        /// </summary>
        /// <returns>true if feasible, otherwise return false</returns>
        public void TarjanModelChecking()
        {
            VerificationOutput.CounterExampleTrace = null;

            //out-going table.
            Dictionary <string, List <string> > OutgoingTransitionTable = new Dictionary <string, List <string> >(Ultility.Ultility.MC_INITIAL_SIZE);

            //DFS Stack
            Stack <KeyValuePair <ConfigurationBase, string> > TaskStack = new Stack <KeyValuePair <ConfigurationBase, string> >(5000);

            //DFS data, which mapping each state to an int[] of size 2, first is the pre-order, second is the lowlink
            StringDictionary <int[]> DFSData = new StringDictionary <int[]>(Ultility.Ultility.MC_INITIAL_SIZE);

            List <KeyValuePair <ConfigurationBase, string> > initials = new List <KeyValuePair <ConfigurationBase, string> >();
            HashSet <string> existed = new HashSet <string>();

            foreach (string s in BA.InitialStates)
            {
                List <string> next = BA.MakeOneMove(s, InitialStep);

                foreach (string var in next)
                {
                    //if (!existed.Contains(var))
                    //{
                    //    existed.Add(var);
                    //    initials.Add(new KeyValuePair<ConfigurationBase, string>(InitialStep, var));
                    //}
                    if (existed.Add(var))
                    {
                        initials.Add(new KeyValuePair <ConfigurationBase, string>(InitialStep, var));
                    }
                }
            }

            if (initials.Count == 0)
            {
                VerificationOutput.VerificationResult = VerificationResultType.VALID;
                return;
            }

            for (int z = 0; z < initials.Count; z++)
            {
                KeyValuePair <ConfigurationBase, string> initState = initials[z];
                TaskStack.Push(initState);
                string ID = initState.Key.GetIDWithEvent() + Constants.SEPARATOR + initState.Value;
                DFSData.Add(ID, new int[] { VISITED_NOPREORDER, 0 });
                OutgoingTransitionTable.Add(ID, new List <string>(8));
            }

            List <string> StronglyConnectedComponets = new List <string>(1024);
            Stack <KeyValuePair <ConfigurationBase, string> > stepStack = new Stack <KeyValuePair <ConfigurationBase, string> >(1024);

            //# Preorder counter
            int i = 0;

            //store the expended event step of a node to avoid multiple invocation of the make one move.
            Dictionary <string, List <KeyValuePair <ConfigurationBase, string> > > ExpendedNode = new Dictionary <string, List <KeyValuePair <ConfigurationBase, string> > >(1024);

            do
            {
                if (CancelRequested)
                {
                    VerificationOutput.NoOfStates = DFSData.Count; // VisitedWithID.Count;
                    return;
                }

                KeyValuePair <ConfigurationBase, string> pair = TaskStack.Peek();
                ConfigurationBase config   = pair.Key;
                string            v        = pair.Key.GetIDWithEvent() + Constants.SEPARATOR + pair.Value;
                List <string>     outgoing = OutgoingTransitionTable[v];

                int[] nodeData = DFSData.GetContainsKey(v);

                if (nodeData[0] == VISITED_NOPREORDER)
                {
                    nodeData[0] = i;
                    i++;
                }

                bool done = true;

                if (ExpendedNode.ContainsKey(v))
                {
                    List <KeyValuePair <ConfigurationBase, string> > list = ExpendedNode[v];
                    if (list.Count > 0)
                    {
                        //transverse all steps
                        for (int k = list.Count - 1; k >= 0; k--)
                        {
                            KeyValuePair <ConfigurationBase, string> step = list[k];

                            //if the step is a unvisited step
                            string tmp = step.Key.GetIDWithEvent() + Constants.SEPARATOR + step.Value;
                            if (DFSData.GetContainsKey(tmp)[0] == VISITED_NOPREORDER)
                            {
                                if (done)
                                {
                                    TaskStack.Push(step);
                                    done = false;
                                    list.RemoveAt(k);
                                }
                            }
                            else
                            {
                                list.RemoveAt(k);
                            }
                        }
                    }
                }
                else
                {
                    IEnumerable <ConfigurationBase> list = config.MakeOneMove();
                    List <KeyValuePair <ConfigurationBase, string> > product = new List <KeyValuePair <ConfigurationBase, string> >();

                    foreach (ConfigurationBase step in list)
                    {
                        List <string> states = BA.MakeOneMove(pair.Value, step);

                        for (int j = 0; j < states.Count; j++)
                        {
                            product.Add(new KeyValuePair <ConfigurationBase, string>(step, states[j]));
                        }
                    }

                    //count the transitions visited
                    VerificationOutput.Transitions += product.Count;

                    for (int k = product.Count - 1; k >= 0; k--)
                    {
                        KeyValuePair <ConfigurationBase, string> step = product[k];
                        string tmp  = step.Key.GetIDWithEvent() + Constants.SEPARATOR + step.Value;
                        int[]  data = DFSData.GetContainsKey(tmp);
                        if (data != null)
                        {
                            outgoing.Add(tmp);
                            if (data[0] == VISITED_NOPREORDER)
                            {
                                if (done)
                                {
                                    TaskStack.Push(step);
                                    done = false;
                                    product.RemoveAt(k);
                                }
                                else
                                {
                                    product[k] = step;
                                }
                            }
                            else
                            {
                                product.RemoveAt(k);
                            }
                        }
                        else
                        {
                            DFSData.Add(tmp, new int[] { VISITED_NOPREORDER, 0 });
                            OutgoingTransitionTable.Add(tmp, new List <string>(8));
                            outgoing.Add(tmp);

                            if (done)
                            {
                                TaskStack.Push(step);
                                done = false;
                                product.RemoveAt(k);
                            }
                            else
                            {
                                product[k] = step;
                            }
                        }
                    }

                    ExpendedNode.Add(v, product);
                }

                if (done)
                {
                    int lowlinkV  = nodeData[0];
                    int preorderV = lowlinkV;

                    bool selfLoop = false;
                    for (int j = 0; j < outgoing.Count; j++)
                    {
                        string w = outgoing[j];
                        if (w == v)
                        {
                            selfLoop = true;
                        }

                        int[] wdata = DFSData.GetContainsKey(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;

                    TaskStack.Pop();

                    if (lowlinkV == preorderV)
                    {
                        StronglyConnectedComponets.Add(v);
                        nodeData[0] = SCC_FOUND;

                        //checking for buchi-fair
                        bool BuchiFair = pair.Value.EndsWith(Constants.ACCEPT_STATE);

                        if (stepStack.Count > 0)
                        {
                            KeyValuePair <ConfigurationBase, string> valuePair = stepStack.Peek();
                            string k = valuePair.Key.GetIDWithEvent() + Constants.SEPARATOR + valuePair.Value;

                            while (stepStack.Count > 0 && DFSData.GetContainsKey(k)[0] > preorderV)
                            {
                                stepStack.Pop();
                                StronglyConnectedComponets.Add(k);
                                DFSData.GetContainsKey(k)[0] = SCC_FOUND;

                                if (!BuchiFair && valuePair.Value.EndsWith(Constants.ACCEPT_STATE))
                                {
                                    BuchiFair = true;
                                }

                                if (stepStack.Count > 0)
                                {
                                    valuePair = stepStack.Peek();
                                    k         = valuePair.Key.GetIDWithEvent() + Constants.SEPARATOR + valuePair.Value;
                                }
                            }
                        }

                        if (BuchiFair && (config.IsDeadLock || StronglyConnectedComponets.Count > 1 || selfLoop))
                        {
                            VerificationOutput.VerificationResult = VerificationResultType.INVALID;
                            VerificationOutput.NoOfStates         = DFSData.Count;

                            while (TaskStack.Count > 0 && TaskStack.Peek().Key.Event != Constants.INITIAL_EVENT)
                            {
                                TaskStack.Pop();
                            }

                            string startID = null;
                            if (TaskStack.Count > 0)
                            {
                                startID = TaskStack.Peek().Key.GetIDWithEvent() + Constants.SEPARATOR +
                                          TaskStack.Peek().Value;
                            }

                            VerificationOutput.CounterExampleTrace = GetConcreteTrace(InitialStep, GetCounterExample(StronglyConnectedComponets, startID, OutgoingTransitionTable));
                            return;
                        }

                        foreach (string componet in StronglyConnectedComponets)
                        {
                            ExpendedNode.Remove(componet);
                        }

                        StronglyConnectedComponets.Clear();
                    }
                    else
                    {
                        stepStack.Push(pair);
                    }
                }
            } while (TaskStack.Count > 0);

            VerificationOutput.VerificationResult = VerificationResultType.VALID;
            VerificationOutput.NoOfStates         = DFSData.Count;
            return;
        }