Exemplo n.º 1
0
        void GetReachableFromVertex(int i, HSet covered, int stepsLeft, HSet result, bool skipChoicePoints, int totalSteps)
        {
            if (stepsLeft == 0 || covered.Contains(i))
            {
                return;
            }
            covered.Insert(i);

            foreach (Edge l in this.EdgesAtVertex(i))
            {
                result.Insert(l);
                if (skipChoicePoints == false)
                {
                    GetReachableFromVertex(l.target, covered,
                                           stepsLeft - 1, result, skipChoicePoints, totalSteps);
                }
                else if (this.IsChoicePoint(l.target) == false)
                {
                    GetReachableFromVertex(l.target,
                                           covered, stepsLeft - 1, result, skipChoicePoints, totalSteps);
                }
                else //choice point
                {
                    GetReachableFromVertex(l.target, covered,
                                           totalSteps, result, skipChoicePoints, totalSteps);
                }
            }
        }
Exemplo n.º 2
0
        static void CheckTransience(Graph graph, HSet targets)
        {
            Queue q = new Queue();

            bool[] reached = new bool[graph.NumberOfVertices];
            foreach (int v in targets)
            {
                reached[v] = true;
                q.Enqueue(v);
            }
            while (q.Count > 0)
            {
                int v = (int)q.Dequeue();
                foreach (int u in new Pred(graph, v))
                {
                    if (reached[u] == false)
                    {
                        reached[u] = true;
                        q.Enqueue(u);
                    }
                }
            }

            foreach (bool b in reached)
            {
                if (!b)
                {
                    throw new InvalidOperationException("some vertex has not been reached");
                }
            }
        }
Exemplo n.º 3
0
Arquivo: HSet.cs Projeto: juhan/NModel
        /// <summary>
        /// Set union.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static HSet operator +(HSet a, HSet b)
        {
            HSet ret=new HSet(a);
            foreach( object o in b)
                ret.Insert(o);

            return ret;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the set of vertices reachable from the given initial vertex for no more than n steps.
        /// </summary>
        /// <param name="vertex">an initial vertex to start the search</param>
        /// <param name="steps">limit on the depth</param>
        /// <param name="skipChoicePoints">This parameter influences the definition of a non-reachable state;
        /// if it is true then a  state is unreachable in n steps if there is a strategy on choice points ensuring that.</param>
        /// <returns>set of vertices</returns>
        internal HSet GetReachableEdges(int vertex, int steps, bool skipChoicePoints)
        {
            HSet result  = new HSet();
            HSet covered = new HSet();

            GetReachableFromVertex(vertex, covered, steps, result, skipChoicePoints, steps);

            return(result);
        }
Exemplo n.º 5
0
 internal static double[] GetExpectations(Graph graph,/*int[] sources,*/ HSet targets, int nStates)
 {
     double[] ret = ValueIteration(graph, targets, nStates); //ValueIteration(graph,sources,targets,nStates);
     if(ret==null)
     return graph.GetDistancesToAcceptingStates(targets.ToArray(typeof(int))as int[]);
         //return graph.GetDistancesToAcceptingStates(sources,targets.ToArray(typeof(int))as int[]);
       else
     return ret;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Clones the set.
        /// </summary>
        /// <returns></returns>
        public HSet Clone()
        {
            HSet ret = new HSet();

            foreach (object i in this)
            {
                ret.Insert(i);
            }
            return(ret);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Set union.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static HSet operator+(HSet a, HSet b)
        {
            HSet ret = new HSet(a);

            foreach (object o in b)
            {
                ret.Insert(o);
            }

            return(ret);
        }
Exemplo n.º 8
0
 static internal double [] GetExpectations(Graph graph, /*int[] sources,*/ HSet targets, int nStates)
 {
     double[] ret = ValueIteration(graph, targets, nStates); //ValueIteration(graph,sources,targets,nStates);
     if (ret == null)
     {
         return(graph.GetDistancesToAcceptingStates(targets.ToArray(typeof(int)) as int[]));
     }
     //return graph.GetDistancesToAcceptingStates(sources,targets.ToArray(typeof(int))as int[]);
     else
     {
         return(ret);
     }
 }
Exemplo n.º 9
0
/// <summary>
/// Set difference.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
        public static HSet operator-(HSet a, HSet b)
        {
            HSet ret = new HSet();

            foreach (object o in a)
            {
                if (!b.Contains(o))
                {
                    ret.Insert(o);
                }
            }

            return(ret);
        }
Exemplo n.º 10
0
        internal HSet DeadStates(int[] acceptingStates)
        {
            try {
                InitBackwardEdges();

                Queue q = new Queue();


                HSet aliveSet = new HSet(acceptingStates);

                foreach (int i in acceptingStates)
                {
                    if (i < NumberOfVertices)
                    {
                        q.Enqueue(i);
                    }
                }


                while (q.Count > 0)
                {
                    int u = (int)q.Dequeue();
                    foreach (int v in Pred(u))
                    {
                        if (!aliveSet.Contains(v))
                        {
                            aliveSet.Insert(v);
                            q.Enqueue(v);
                        }
                    }
                }


                HSet deadSet = new HSet();
                //adding the deads
                int n = NumberOfVertices;
                for (int i = 0; i < n; i++)
                {
                    if (!aliveSet.Contains(i))
                    {
                        deadSet.Insert(i);
                    }
                }

                return(deadSet);
            }
            catch {
                return(new HSet());
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Returns a strategy to reach targets from a vertex.
        /// </summary>
        /// <param name="vertex"></param>
        /// <returns></returns>
        internal Strategy[] GetStrategyReachableFromVertex(int vertex)
        {
            Strategy [] strategies = this.GetStrategies();



            if (VertIsNondet(vertex))
            {
                throw new ArgumentException("vertex is a choice point");
            }

            //ArrayList links=new ArrayList();


            HSet linkSet = new HSet();

            foreach (Strategy s in strategies)
            {
                if (s.edge != null)
                {
                    linkSet.Insert(s.edge);

                    if (VertIsNondet(s.edge.target))
                    {
                        foreach (Edge l in graph.EdgesAtVertex(s.edge.target))
                        {
                            linkSet.Insert(l);
                        }
                    }
                }
            }



            BasicGraph bg = new BasicGraph(0, linkSet.ToArray(typeof(Edge)));

            bool [] reachables = bg.GetReachableArray(vertex);

            for (int i = 0; i < reachables.Length; i++)
            {
                if (reachables[i] == false)
                {
                    strategies[i].edge = null;
                }
            }


            return(strategies);
        }
Exemplo n.º 12
0
        internal MultipleSourcesShortestPaths(IGraph graph,int[] source)
        {
            this.graph=graph;
            this.source=source;
            pred=new Edge[graph.NumberOfVertices];
            dist=new int[graph.NumberOfVertices];

            for(int i=0;i<dist.Length;i++)
                dist[i]=Graph.INF;

              foreach(int s in source)
            dist[s]=0;

              sourceSet=new HSet(source);
        }
Exemplo n.º 13
0
        /// <summary>
        /// </summary>
        /// <param name="initialVertex">the initial vertex - usually 0</param>
        /// <param name="mustEdges">the edge considered as must in Chinese Postman route</param>
        /// <param name="optionalEdges">the edges considered as optional in Chinese Postman route</param>
        /// <param name="nondetVertices">the vertices where the system behaves non-deterministically</param>
        /// <param name="closureInstr">this instruction will shuffle some optional edges to must ones. Chinese Rural Postman works only when the set of must links is weakly closed</param>
        internal Graph(int initialVertex, Edge[] mustEdges, Edge[] optionalEdges, int [] nondetVertices, WeakClosureEnum closureInstr)
            : this(initialVertex, mustEdges, optionalEdges, closureInstr)
        {
            this.nondNeighbours = new RBMap();

            foreach (int p in nondetVertices)
            {
                if (p < this.NumberOfVertices)
                {
                    HSet s = (this.nondNeighbours[p] = new HSet()) as HSet;
                    foreach (Edge l in this.graph.EdgesAtVertex(p))
                    {
                        s.Insert(l.target);
                    }
                }
            }
        }
Exemplo n.º 14
0
        void Step()
        {
            for (int j = 0; j < graph.NumberOfVertices; j++)
            {
                foreach (Edge edge in this.graph.EdgesAtVertex(j))
                {
                    if (front.Contains(edge.target))
                    {
                        Process(edge);
                    }
                }
            }


            front    = newfront;
            newfront = new HSet();
            PropagateChanges();
        }
Exemplo n.º 15
0
        internal MultipleSourcesShortestPaths(IGraph graph, int[] source)
        {
            this.graph  = graph;
            this.source = source;
            pred        = new Edge[graph.NumberOfVertices];
            dist        = new int[graph.NumberOfVertices];

            for (int i = 0; i < dist.Length; i++)
            {
                dist[i] = Graph.INF;
            }

            foreach (int s in source)
            {
                dist[s] = 0;
            }

            sourceSet = new HSet(source);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Get all states in the given finite automaton from which no accepting state is reachable.
        /// </summary>
        /// <param name="fa">given finite automaton</param>
        /// <returns>all dead states in the finite automaton</returns>
        public static Set <Term> GetDeadStates(FSM fa)
        {
            //build a graph from fa
            Dictionary <Term, int> stateToVertexMap = new Dictionary <Term, int>();

            stateToVertexMap[fa.InitialState] = 0;
            int i = 1;

            foreach (Term state in fa.States.Remove(fa.InitialState))
            {
                stateToVertexMap[state] = i++;
            }

            //create edges that correspond to the transitions
            GraphTraversals.Edge[] edges = new GraphTraversals.Edge[fa.Transitions.Count];
            Triple <Term, CompoundTerm, Term>[] transitions = new Triple <Term, CompoundTerm, Term> [fa.Transitions.Count];
            i = 0;
            foreach (Triple <Term, CompoundTerm, Term> trans in fa.Transitions)
            {
                edges[i] = new GraphTraversals.Edge(stateToVertexMap[trans.First],
                                                    stateToVertexMap[trans.Third], i);
                transitions[i++] = trans;
            }

            GraphTraversals.Graph g =
                new GraphTraversals.Graph(0, edges, new GraphTraversals.Edge[] { },
                                          GraphTraversals.WeakClosureEnum.DoNotClose);

            int[] acceptingVertices = new int[fa.AcceptingStates.Count];
            i = 0;
            foreach (Term accState in fa.AcceptingStates)
            {
                acceptingVertices[i++] = stateToVertexMap[accState];
            }

            GraphTraversals.HSet deadVertices = g.DeadStates(acceptingVertices);

            return(fa.States.Select(delegate(Term state)
                                    { return deadVertices.Contains(stateToVertexMap[state]); }));
        }
Exemplo n.º 17
0
        internal void InitBackwardEdges()
        {
            #region One time init
            //this block will be executed only once
            if (this.backwardEdges == null)
            {
                this.backwardEdges         = new ArrayList[NumberOfVertices];
                this.nondBackwardNeighbors = new HSet[NumberOfVertices];

                for (int i = 0; i < NumberOfVertices; i++)
                {
                    bool iIsNond = IsChoicePoint(i);

                    foreach (Edge l in graph.EdgesAtVertex(i))
                    {
                        ArrayList al = this.backwardEdges[l.target] as ArrayList;
                        if (al == null)
                        {
                            al = this.backwardEdges[l.target] = new ArrayList();
                        }

                        al.Add(l);

                        if (iIsNond)
                        {
                            HSet s = this.nondBackwardNeighbors[l.target];
                            if (s == null)
                            {
                                this.nondBackwardNeighbors[l.target] = s = new HSet();
                            }
                            s.Insert(i); //i is the edge 'l' start
                        }
                    }
                }
            }
            #endregion
        }
Exemplo n.º 18
0
        void GetReachableFromVertex(int i,HSet covered, int stepsLeft,HSet result,bool skipChoicePoints,int totalSteps)
        {
            if(stepsLeft==0 || covered.Contains(i))
            return;
              covered.Insert(i);

              foreach(Edge l in this.EdgesAtVertex(i)){
            result.Insert(l);
            if(skipChoicePoints==false)
              GetReachableFromVertex(l.target,covered,
                               stepsLeft-1,result,skipChoicePoints,totalSteps);
            else if (this.IsChoicePoint(l.target)==false)
              GetReachableFromVertex(l.target,
                               covered,stepsLeft-1,result,skipChoicePoints,totalSteps);
            else //choice point
              GetReachableFromVertex(l.target,covered,
                               totalSteps,result,skipChoicePoints,totalSteps);

              }
        }
Exemplo n.º 19
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));
        }
Exemplo n.º 20
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);
        }
Exemplo n.º 21
0
        /*
         * From "Play to test"
         * Value iteration is the most widely used algorithm for solving discounted Markov decision
         * problems (see e.g. [21]). Reachability games give rise to non-discounted Markov
         * decision problems. Nevertheless the value iteration algorithm applies; this is a practical
         * approach for computing strategies for transient test graphs. Test graphs, modified by inserting
         * a zero-cost edge (0; 0), correspond to a subclass of negative stationary Markov
         * decision processes (MDPs) with an infinite horizon, where rewards are negative and
         * thus regarded as costs, strategies are stationary, i.e. time independent, and there is no
         * finite upper bound on the number of steps in the process. The optimization criterion
         * for our strategies corresponds to the expected total reward criterion, rather than the
         * expected discounted reward criterion used in discounted Markov decision problems.
         * Let G = (V;E; V a; V p; g; p; c) be a test graph modified by inserting a zero-cost
         * edge (0; 0). The classical value iteration algorithm works as follows on G.
         *
         * Value iteration Let n = 0 and let M0 be the zero vector with coordinates V so that
         * every M0[u] = 0. Given n and Mn, we compute Mn+1 (and then increment n):
         * Mn+1[u] ={ min {c(u,v) +Mn[v]:(u,v) in E} if u is an active state}
         * or sum {p(u,v)*(c(u,v) +Mn[v]); if u is a choice point
         *
         * Value iteration for negative MDPs with the expected total reward criterion, or negative
         * Markov decision problems for short, does not in general converge to an optimal
         * solution, even if one exists. However, if there exists a strategy for which the the expected
         * cost is finite for all states [21, Assumption 7.3.1], then value iteration does converge for
         * negative Markov decision problems [21, Theorem 7.3.10]. In light of lemmas 2 and 3,
         * this implies that value iteration converges for transient test graphs. Let us make this
         * more precise, as a corollary of Theorem 7.3.10 in [21].
         */

        //nStates marks the end of active states, choice points start after that
        static double[] ValueIteration(Graph graph, HSet targets, int nStates) //ValueIteration(Graph graph,int[] sources,HSet targets,int nStates)
        {
            graph.InitEdgeProbabilities();

            double[] v0  = new double[graph.NumberOfVertices];
            double[] v1  = new double[graph.NumberOfVertices];
            double   eps = 1.0E-6;
            double   delta;

            double[] v = v0;
            //double[] vnew=v1;


            //      CheckTransience(graph,targets);


            int nOfIter = 0;

            do
            {
                delta = 0;


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

                    double min = Double.MaxValue;

                    foreach (Edge l in graph.EdgesAtVertex(i))
                    {
                        double r = ((double)l.weight) + v[l.target];
                        if (r < min)
                        {
                            min = r;
                        }
                    }
                    if (min != Double.MaxValue)
                    {
                        v1[i] = min;
                        if (delta < min - v[i])
                        {
                            delta = min - v[i];
                        }
                    }
                }

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

                    double r = 0;
                    foreach (Edge l in graph.EdgesAtVertex(i))
                    {
                        r += graph.EdgeProbability(l) * (((double)l.weight) + v[l.target]);
                    }

                    v1[i] = r;
                    if (delta < r - v[i])
                    {
                        delta = r - v[i];
                    }
                }


                nOfIter++;

                //swap v and v1
                double[] vtmp = v;
                v  = v1;
                v1 = vtmp;
            }while(delta > eps && nOfIter < 10000);

            if (delta > eps)
            {
                return(null); //the result is erroneous
            }

            return(v);
        }
Exemplo n.º 22
0
Arquivo: HSet.cs Projeto: juhan/NModel
 /// <summary>
 /// Clones the set.
 /// </summary>
 /// <returns></returns>
 public HSet Clone()
 {
     HSet ret=new HSet();
     foreach(object i in this)
     {
         ret.Insert(i);
     }
     return ret;
 }
Exemplo n.º 23
0
Arquivo: HSet.cs Projeto: juhan/NModel
        /// <summary>
        /// Set difference.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static HSet operator -(HSet a, HSet b)
        {
            HSet ret=new HSet();
            foreach( object o in a)
                if(!b.Contains(o))
                    ret.Insert(o);

            return ret;
        }
Exemplo n.º 24
0
        internal HSet DeadStatesWithoutChangingChoicePoints(int[] acceptingStates)
        {
            try {
                InitBackwardEdges();

                Queue q = new Queue();

                HSet deadSet = new HSet();
                bool done    = false;

                HSet targetSet = new HSet();

                foreach (int i in acceptingStates)
                {
                    if (i < NumberOfVertices)
                    {
                        targetSet.Insert(i);
                    }
                }


                while (!done)
                {
                    done = true;
                    //alives can reach acceptingStates by not passing through deads
                    HSet aliveSet = new HSet(targetSet);
                    foreach (int i in targetSet)
                    {
                        q.Enqueue(i);
                    }

                    while (q.Count > 0)
                    {
                        int u = (int)q.Dequeue();
                        foreach (int v in Pred(u))
                        {
                            if (!aliveSet.Contains(v) && !deadSet.Contains(v))
                            {
                                aliveSet.Insert(v);
                                q.Enqueue(v);
                            }
                        }
                    }


                    //adding new deads
                    int n = NumberOfVertices;
                    for (int i = 0; i < n; i++)
                    {
                        if (!aliveSet.Contains(i) && !deadSet.Contains(i))
                        {
                            done = false; //we are not done since we've found a new dead
                            q.Enqueue(i);
                            deadSet.Insert(i);
                        }
                    }



                    while (q.Count > 0)
                    {
                        int u = (int)q.Dequeue();
                        foreach (int v in Pred(u))
                        {
                            if (deadSet.Contains(v) == false && targetSet.Contains(v) == false)
                            {
                                if (this.IsChoicePoint(v))
                                {
                                    deadSet.Insert(v);
                                    q.Enqueue(v);
                                }
                                else
                                {
                                    bool isDead = true;
                                    foreach (int w in Succ(v))
                                    {
                                        if (deadSet.Contains(w) == false)
                                        {
                                            isDead = false;
                                            break;
                                        }
                                    }
                                    if (isDead)
                                    {
                                        deadSet.Insert(v);
                                        q.Enqueue(v);
                                    }
                                }
                            }
                        }
                    }
                }

                //add to deadSet everything that cannot be reached from the initial vertex

                HSet alSet = new HSet();
                if (NumberOfVertices > 0)
                {
                    if (!deadSet.Contains(initVertex))
                    {
                        q.Enqueue(initVertex);
                        alSet.Insert(initVertex);
                        while (q.Count > 0)
                        {
                            int u = (int)q.Dequeue();
                            foreach (int v in Succ(u))
                            {
                                if (!deadSet.Contains(v))
                                {
                                    if (!alSet.Contains(v))
                                    {
                                        alSet.Insert(v);
                                        q.Enqueue(v);
                                    }
                                }
                            }
                        }
                    }
                }

                for (int i = 0; i < NumberOfVertices; i++)
                {
                    if (!alSet.Contains(i))
                    {
                        deadSet.Insert(i);
                    }
                }

                return(deadSet);
            }
            catch {
                return(new HSet());
            }
        }
Exemplo n.º 25
0
        internal void InitBackwardEdges()
        {
            #region One time init
              //this block will be executed only once
              if(this.backwardEdges==null)
            {
              this.backwardEdges=new ArrayList[NumberOfVertices];
              this.nondBackwardNeighbors=new HSet[NumberOfVertices];

              for(int i=0;i<NumberOfVertices;i++)
            {
              bool iIsNond=IsChoicePoint(i);

              foreach(Edge l in graph.EdgesAtVertex(i))
                {
                  ArrayList al=this.backwardEdges[l.target] as ArrayList;
                  if(al==null)
                    al=this.backwardEdges[l.target]=new ArrayList();

                  al.Add(l);

                  if(iIsNond)
                    {
                      HSet s=this.nondBackwardNeighbors[l.target];
                      if(s==null)
                        this.nondBackwardNeighbors[l.target]=s=new HSet();
                      s.Insert(i); //i is the edge 'l' start
                    }
                }
            }

            }
            #endregion
        }
Exemplo n.º 26
0
        /// <summary>
        /// Returns the set of vertices reachable from the given initial vertex for no more than n steps.
        /// </summary>
        /// <param name="vertex">an initial vertex to start the search</param>
        /// <param name="steps">limit on the depth</param>
        /// <param name="skipChoicePoints">This parameter influences the definition of a non-reachable state;
        /// if it is true then a  state is unreachable in n steps if there is a strategy on choice points ensuring that.</param>
        /// <returns>set of vertices</returns>
        internal HSet GetReachableEdges(int vertex,int steps,bool skipChoicePoints)
        {
            HSet result=new HSet();
              HSet covered=new HSet();
              GetReachableFromVertex(vertex,covered,steps,result,skipChoicePoints,steps);

              return result;
        }
Exemplo n.º 27
0
        //ValueIteration(Graph graph,int[] sources,HSet targets,int nStates)
        /*
          From "Play to test"
          Value iteration is the most widely used algorithm for solving discounted Markov decision
          problems (see e.g. [21]). Reachability games give rise to non-discounted Markov
          decision problems. Nevertheless the value iteration algorithm applies; this is a practical
          approach for computing strategies for transient test graphs. Test graphs, modified by inserting
          a zero-cost edge (0; 0), correspond to a subclass of negative stationary Markov
          decision processes (MDPs) with an infinite horizon, where rewards are negative and
          thus regarded as costs, strategies are stationary, i.e. time independent, and there is no
          finite upper bound on the number of steps in the process. The optimization criterion
          for our strategies corresponds to the expected total reward criterion, rather than the
          expected discounted reward criterion used in discounted Markov decision problems.
          Let G = (V;E; V a; V p; g; p; c) be a test graph modified by inserting a zero-cost
          edge (0; 0). The classical value iteration algorithm works as follows on G.

          Value iteration Let n = 0 and let M0 be the zero vector with coordinates V so that
          every M0[u] = 0. Given n and Mn, we compute Mn+1 (and then increment n):
          Mn+1[u] ={ min {c(u,v) +Mn[v]:(u,v) in E} if u is an active state}
          or sum {p(u,v)*(c(u,v) +Mn[v]); if u is a choice point

          Value iteration for negative MDPs with the expected total reward criterion, or negative
          Markov decision problems for short, does not in general converge to an optimal
          solution, even if one exists. However, if there exists a strategy for which the the expected
          cost is finite for all states [21, Assumption 7.3.1], then value iteration does converge for
          negative Markov decision problems [21, Theorem 7.3.10]. In light of lemmas 2 and 3,
          this implies that value iteration converges for transient test graphs. Let us make this
          more precise, as a corollary of Theorem 7.3.10 in [21].
        */
        //nStates marks the end of active states, choice points start after that
        static double[] ValueIteration(Graph graph, HSet targets, int nStates)
        {
            graph.InitEdgeProbabilities();

              double[]v0=new double[graph.NumberOfVertices];
              double[]v1=new double[graph.NumberOfVertices];
              double eps=1.0E-6;
              double delta;
              double[] v=v0;
              //double[] vnew=v1;

              //      CheckTransience(graph,targets);

              int nOfIter=0;
              do{

            delta=0;

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

              if(targets.Contains(i))
            continue;

              double min=Double.MaxValue;

              foreach(Edge l in graph.EdgesAtVertex(i)){
            double r=((double)l.weight)+v[l.target];
            if(r<min)
              min=r;
              }
              if(min!=Double.MaxValue){
            v1[i]=min;
            if(delta<min-v[i])
              delta=min-v[i];
              }

            }

            for(int i=nStates;i<graph.NumberOfVertices;i++){
              if(targets.Contains(i))
            continue;

              double r=0;
              foreach(Edge l in graph.EdgesAtVertex(i))
            r+=graph.EdgeProbability(l)*(((double)l.weight)+v[l.target]);

              v1[i]=r;
              if(delta<r-v[i])
            delta=r-v[i];
            }

            nOfIter++;

            //swap v and v1
            double[] vtmp=v;
            v=v1;
            v1=vtmp;
              }
              while(delta>eps && nOfIter<10000);

              if(delta>eps){
            return null; //the result is erroneous
              }

              return v;
        }
Exemplo n.º 28
0
        void Step()
        {
            for(int j=0;j<graph.NumberOfVertices;j++)
            foreach(Edge edge in this.graph.EdgesAtVertex(j))
              if( front.Contains(edge.target))
            Process(edge);

              front=newfront;
              newfront=new HSet();
              PropagateChanges();
        }
Exemplo n.º 29
0
        /// <summary>
        /// Returns a strategy to reach targets from a vertex.
        /// </summary>
        /// <param name="vertex"></param>
        /// <returns></returns>
        internal Strategy[] GetStrategyReachableFromVertex(int vertex)
        {
            Strategy [] strategies=this.GetStrategies();

              if(VertIsNondet(vertex))
            throw new ArgumentException("vertex is a choice point");

              //ArrayList links=new ArrayList();

              HSet linkSet=new HSet();

              foreach(Strategy s in strategies)
            if(s.edge!=null){

              linkSet.Insert(s.edge);

              if(VertIsNondet( s.edge.target))
            foreach(Edge l in graph.EdgesAtVertex(s.edge.target))
              linkSet.Insert(l);

             }

              BasicGraph bg=new BasicGraph(0, linkSet.ToArray(typeof(Edge)) );

              bool []reachables=bg.GetReachableArray(vertex);

              for(int i=0;i<reachables.Length;i++){
            if(reachables[i]==false)
              strategies[i].edge=null;
              }

              return strategies;
        }
Exemplo n.º 30
0
        internal HSet DeadStates(int[] acceptingStates)
        {
            try {

            InitBackwardEdges();

            Queue q=new Queue();

            HSet aliveSet=new HSet(acceptingStates);

            foreach(int i in acceptingStates)
              if(i<NumberOfVertices)
            q.Enqueue(i);

            while(q.Count>0){

              int u=(int)q.Dequeue();
              foreach( int v in Pred(u))
            {
              if(!aliveSet.Contains(v))
                {
                  aliveSet.Insert(v);
                  q.Enqueue(v);
                }
            }
            }

            HSet deadSet=new HSet();
            //adding the deads
            int n=NumberOfVertices;
            for(int i=0;i<n;i++)
              if(! aliveSet.Contains(i))
            {
              deadSet.Insert(i);
            }

            return deadSet;
              }
              catch{
            return new HSet();
              }
        }
Exemplo n.º 31
0
        internal HSet DeadStatesWithoutChangingChoicePoints(int[] acceptingStates)
        {
            try {
            InitBackwardEdges();

            Queue q=new Queue();

            HSet deadSet=new HSet();
            bool done=false;

            HSet targetSet=new HSet();

            foreach(int i in acceptingStates)
              if(i<NumberOfVertices)
            targetSet.Insert(i);

            while(!done){

              done=true;
              //alives can reach acceptingStates by not passing through deads
              HSet aliveSet=new HSet(targetSet);
              foreach(int i in targetSet)
            q.Enqueue(i);

              while(q.Count>0)
            {

              int u=(int)q.Dequeue();
              foreach( int v in Pred(u))
                {
                  if(!aliveSet.Contains(v)&&!deadSet.Contains(v))
                    {
                      aliveSet.Insert(v);
                      q.Enqueue(v);
                    }
                }
            }

              //adding new deads
              int n=NumberOfVertices;
              for(int i=0;i<n;i++)
            if(! aliveSet.Contains(i) && !deadSet.Contains(i))
              {
                done=false; //we are not done since we've found a new dead
                q.Enqueue(i);
                deadSet.Insert(i);
              }

              while(q.Count>0)
            {
              int u=(int)q.Dequeue();
              foreach(int v in Pred(u))
                {
                  if(deadSet.Contains(v)==false&& targetSet.Contains(v)==false)
                    {
                      if(this.IsChoicePoint(v))
                        {
                          deadSet.Insert(v);
                          q.Enqueue(v);
                        }
                      else {
                        bool isDead=true;
                        foreach(int w in Succ(v))
                          {
                            if(deadSet.Contains(w)==false)
                              {
                                isDead=false;
                                break;
                              }
                          }
                        if(isDead)
                          {
                            deadSet.Insert(v);
                            q.Enqueue(v);
                          }
                      }
                    }

                }
            }
            }

            //add to deadSet everything that cannot be reached from the initial vertex

            HSet alSet=new HSet();
                if(NumberOfVertices>0)
                    if(!deadSet.Contains(initVertex))
            {
              q.Enqueue(initVertex);
              alSet.Insert(initVertex);
              while(q.Count>0)
                {
                  int u=(int)q.Dequeue();
                  foreach(int v in Succ(u))
                    if(!deadSet.Contains(v))
                      if(!alSet.Contains(v))
                        {
                          alSet.Insert(v);
                          q.Enqueue(v);
                        }
                }
            }

                for(int i=0;i<NumberOfVertices;i++){
              if(!alSet.Contains(i))
            deadSet.Insert(i);
            }

            return deadSet;
              }
              catch{
            return new HSet();
              }
        }
Exemplo n.º 32
0
        static void CheckTransience(Graph graph,HSet targets)
        {
            Queue q=new Queue();
              bool[] reached=new bool[graph.NumberOfVertices];
              foreach(int v in targets){
            reached[v]=true;
            q.Enqueue(v);
              }
              while(q.Count>0){
            int v=(int)q.Dequeue();
            foreach(int u in new Pred(graph,v)){
              if(reached[u]==false){
            reached[u]=true;
            q.Enqueue(u);
              }
            }
              }

              foreach(bool b in reached)
            if(!b)
              throw new InvalidOperationException("some vertex has not been reached");
        }