Beispiel #1
0
        static internal EdgesAndExpectations GetStaticStrategy(Graph graph,
                                                               int[] sources,
                                                               HSet targets,
                                                               int nStates,
                                                               int resetCost,
                                                               int [] deadStates)
        {
            foreach (int t in targets)
            {
                foreach (Edge l in graph.EdgesAtVertex(t))
                {
                    l.weight = 0;
                }
            }

            //fix the edges weights
            foreach (Edge l in graph.MustEdges)
            {
                if (l.target >= nStates)
                {
                    l.weight = 0;
                }
            }
            foreach (Edge l in graph.OptionalEdges)
            {
                l.weight = resetCost;
            }



            if (graph.NumberOfVertices > 1000)//Value iteration becomes too slow
            {
                return(graph.GetStaticStrategyWithDistancesToAcceptingStates(sources, targets.ToArray(typeof(int)) as int[]));
            }


            HSet deadStatesSet = new HSet(deadStates);

            //create reachableGraph
            bool [] reachableVerts = new bool[graph.NumberOfVertices];

            //we have to walk backwards from the targets avoiding dead states
            graph.InitBackwardEdges();

            foreach (int i in targets)
            {
                reachableVerts[i] = true;
            }

            System.Collections.Queue queue = new System.Collections.Queue(targets);

            while (queue.Count > 0)
            {
                int i = (int)queue.Dequeue();
                foreach (int v in graph.Pred(i))
                {
                    if (!reachableVerts[v] && !deadStatesSet.Contains(v))
                    {
                        queue.Enqueue(v);
                        reachableVerts[v] = true;
                    }
                }
            }

            int numberOfReachableVerts = 0;

            foreach (bool b in reachableVerts)
            {
                if (b)
                {
                    numberOfReachableVerts++;
                }
            }

            Edge[]    strategyEdges;
            double [] expectations;

            if (numberOfReachableVerts == graph.NumberOfVertices)
            {
                expectations = GetExpectations(graph, /* sources,*/ targets, nStates);

                if (expectations == null)
                {
                    return(new EdgesAndExpectations());
                }

                strategyEdges = new Edge[nStates];

                for (int i = 0; i < nStates && i < graph.NumberOfVertices; i++)
                {
                    if (targets.Contains(i) || deadStatesSet.Contains(i))
                    {
                        continue;
                    }

                    double min = Single.MaxValue;

                    Edge stEdge = null;

                    foreach (Edge l in graph.EdgesAtVertex(i))
                    {
                        int j = l.target;
                        if (expectations[j] < min)
                        {
                            min = expectations[j];

                            stEdge = l;
                        }
                    }


                    strategyEdges[i] = stEdge;
                }
            }
            else
            { //numberOfReachableVerts<graph.NumberOfVertices)
                int [] graphToRG = new int[graph.NumberOfVertices];
                //reachable graph to graph
                int [] rGToGraph = new int[numberOfReachableVerts];

                int count    = 0;
                int rNStates = 0;
                for (int i = 0; i < reachableVerts.Length; i++)
                {
                    if (reachableVerts[i])
                    {
                        graphToRG[i]     = count;
                        rGToGraph[count] = i;
                        count++;
                        if (i < nStates)
                        {
                            rNStates++;
                        }
                    }
                }

                System.Collections.ArrayList mustEdges = new System.Collections.ArrayList();

                foreach (Edge l in graph.MustEdges)
                {
                    if (reachableVerts[l.source] && reachableVerts[l.target])
                    {
                        Edge ml = new Edge(graphToRG[l.source], graphToRG[l.target], l.label, l.weight);
                        mustEdges.Add(ml);
                    }
                }

                System.Collections.ArrayList nondVerts = new System.Collections.ArrayList();

                for (int i = nStates; i < graph.NumberOfVertices; i++)
                {
                    if (reachableVerts[i])
                    {
                        nondVerts.Add(graphToRG[i]);
                    }
                }

                Graph rGraph = new Graph(0, mustEdges.ToArray(typeof(Edge)) as Edge[], new Edge[0],
                                         nondVerts.ToArray(typeof(int)) as int[], true, WeakClosureEnum.DoNotClose);


                int [] rSources = new int[sources.Length];
                int    c        = 0;
                foreach (int s in sources)
                {
                    rSources[c++] = graphToRG[s];
                }

                HSet rTargets = new HSet();

                foreach (int s in targets)
                {
                    if (reachableVerts[s])
                    {
                        rTargets.Insert(graphToRG[s]);
                    }
                }

                double [] rExpectations = GetExpectations(rGraph, /*rSources,*/ rTargets, rNStates);

                if (rExpectations == null)
                {
                    return(new EdgesAndExpectations());
                }

                strategyEdges = new Edge[nStates];

                for (int i = 0; i < nStates; i++)
                {
                    if (!reachableVerts[i])
                    {
                        continue;
                    }

                    if (targets.Contains(i) || deadStatesSet.Contains(i))
                    {
                        continue;
                    }

                    double min = Single.MaxValue;

                    Edge stEdge = null;

                    foreach (Edge l in graph.EdgesAtVertex(i))
                    {
                        int j = l.target;

                        if (reachableVerts[j])
                        {
                            if (rExpectations[graphToRG[j]] < min)
                            {
                                min    = rExpectations[graphToRG[j]];
                                stEdge = l;
                            }
                        }
                    }


                    strategyEdges[i] = stEdge;
                }


                expectations = new double[graph.NumberOfVertices];
                if (expectations == null)
                {
                    return(new EdgesAndExpectations());
                }

                for (int i = 0; i < expectations.Length; i++)
                {
                    expectations[i] = Int32.MaxValue;
                }


                for (int i = 0; i < rExpectations.Length; i++)
                {
                    expectations[rGToGraph[i]] = rExpectations[i];
                }
            }

            graph.CleanTheStrategy(strategyEdges, sources);

            return(new EdgesAndExpectations(strategyEdges, expectations));
        }
Beispiel #2
0
        internal static EdgesAndExpectations GetStaticStrategy(Graph graph,
            int[] sources,
            HSet targets,
            int nStates,
            int resetCost,
            int []deadStates)
        {
            foreach(int t in targets){
            foreach(Edge l in graph.EdgesAtVertex(t))
              l.weight=0;
              }

              //fix the edges weights
              foreach(Edge l in graph.MustEdges){
            if(l.target>=nStates)
              l.weight=0;
              }
              foreach(Edge l in graph.OptionalEdges)
            l.weight=resetCost;

              if(graph.NumberOfVertices>1000){//Value iteration becomes too slow
            return graph.GetStaticStrategyWithDistancesToAcceptingStates(sources, targets.ToArray(typeof(int)) as int[]);
              }

              HSet deadStatesSet=new HSet(deadStates);

              //create reachableGraph
              bool []reachableVerts=new bool[graph.NumberOfVertices];

              //we have to walk backwards from the targets avoiding dead states
              graph.InitBackwardEdges();

              foreach(int i in targets)
            reachableVerts[i]=true;

              System.Collections.Queue queue=new System.Collections.Queue(targets);

              while(queue.Count>0)
            {
              int i=(int)queue.Dequeue();
              foreach(int v in graph.Pred(i))
            {
              if(!reachableVerts[v] && !deadStatesSet.Contains(v))
                {
                  queue.Enqueue(v);
                  reachableVerts[v]=true;
                }

            }
            }

              int numberOfReachableVerts=0;
              foreach(bool b in reachableVerts)
            if(b)
              numberOfReachableVerts++;

              Edge[] strategyEdges;
              double [] expectations;

              if(numberOfReachableVerts==graph.NumberOfVertices)
            {
              expectations=GetExpectations(graph,/* sources,*/targets,nStates);

              if(expectations==null)
            return new EdgesAndExpectations();

              strategyEdges=new Edge[nStates];

              for(int i=0;i<nStates&&i<graph.NumberOfVertices;i++){

            if(targets.Contains(i)||deadStatesSet.Contains(i))
              continue;

            double min=Single.MaxValue;

            Edge stEdge=null;

            foreach(Edge l in graph.EdgesAtVertex(i)){
              int j=l.target;
              if(expectations[j]<min){
                min=expectations[j];

                stEdge=l;
              }
            }

            strategyEdges[i]=stEdge;
              }

            }
              else
            { //numberOfReachableVerts<graph.NumberOfVertices)

              int [] graphToRG=new int[graph.NumberOfVertices];
              //reachable graph to graph
              int [] rGToGraph=new int[numberOfReachableVerts];

              int count=0;
              int rNStates=0;
              for(int i=0;i<reachableVerts.Length;i++)
            if(reachableVerts[i])
              {
                graphToRG[i]=count;
                rGToGraph[count]=i;
                count++;
                if(i<nStates)
                  rNStates++;
              }

              System.Collections.ArrayList mustEdges=new System.Collections.ArrayList();

              foreach(Edge l in graph.MustEdges)
            {
              if( reachableVerts[l.source]&& reachableVerts[l.target])
                {
                  Edge ml=new Edge(graphToRG[l.source],graphToRG[l.target], l.label,l.weight);
                  mustEdges.Add(ml);
                }
            }

              System.Collections.ArrayList nondVerts=new System.Collections.ArrayList();

              for(int i=nStates;i<graph.NumberOfVertices;i++)
            {
              if(reachableVerts[i])
                nondVerts.Add(graphToRG[i]);
            }

              Graph rGraph=new Graph(0,mustEdges.ToArray(typeof(Edge)) as Edge[],new Edge[0],
                                 nondVerts.ToArray(typeof(int)) as int[],true,WeakClosureEnum.DoNotClose);

              int []rSources=new int[sources.Length];
              int c=0;
              foreach(int s in sources)
            {
              rSources[c++]=graphToRG[s];
            }

              HSet rTargets=new HSet();

              foreach(int s in targets)
            {
              if( reachableVerts[s])
                {
                  rTargets.Insert(graphToRG[s]);
                }
            }

              double []rExpectations=GetExpectations(rGraph,/*rSources,*/ rTargets,rNStates);

              if(rExpectations==null)
            return new EdgesAndExpectations();

              strategyEdges=new Edge[nStates];

              for(int i=0;i<nStates;i++){

            if(!reachableVerts[i])
              continue;

            if(targets.Contains(i)||deadStatesSet.Contains(i))
              continue;

            double min=Single.MaxValue;

            Edge stEdge=null;

            foreach(Edge l in graph.EdgesAtVertex(i)){
              int j=l.target;

              if(reachableVerts[j])
                if(rExpectations[graphToRG[j]]<min){
                  min=rExpectations[graphToRG[j]];
                  stEdge=l;
                }
            }

            strategyEdges[i]=stEdge;
              }

              expectations=new double[graph.NumberOfVertices];
              if(expectations==null)
            return new EdgesAndExpectations();

              for(int i=0;i<expectations.Length;i++)
            expectations[i]=Int32.MaxValue;

              for(int i=0;i<rExpectations.Length;i++)
            expectations[rGToGraph[i]]=rExpectations[i];

            }

              graph.CleanTheStrategy(strategyEdges,sources);

              return new EdgesAndExpectations(strategyEdges, expectations);
        }