Пример #1
0
        /// <summary>
        /// Sets edge probability.
        /// </summary>
        /// <param name="edge">the edge</param>
        /// <param name="prob">the probability: a number between 0 and 1</param>
        internal void SetEdgeProbability(Edge edge, double prob)
        {
            if (this.edgeProbabilities == null)
            {
                edgeProbabilities = new RBMap();
            }

            edgeProbabilities[edge] = prob;
        }
Пример #2
0
        static Edge ReadEdge(string s, RBMap probabilities)
        {
            string [] split = s.Split(' ');
            Edge      edge  = new Edge(Int32.Parse(split[0]), Int32.Parse(split[2]), Int32.Parse(split[1]), Int32.Parse(split[3]));

            if (split.Length == 5)
            {
                probabilities[edge] = Double.Parse(split[4]);
            }

            return(edge);
        }
Пример #3
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);
                    }
                }
            }
        }
Пример #4
0
        internal void InitEdgeProbabilities()
        {
            if (this.nondNeighbours.Count == 0 || (edgeProbabilities != null && edgeProbabilities.Count > 0))
            {
                return;
            }

            edgeProbabilities = new RBMap();

            foreach (DictionaryEntry p in this.nondNeighbours)
            {
                int       i     = (int)p.Key;
                ArrayList edges = EdgesAtVertex(i) as ArrayList;

                double pr = 1.0 / edges.Count;
                foreach (Edge l in edges)
                {
                    this.edgeProbabilities[l] = pr;
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Sets edge probability.
        /// </summary>
        /// <param name="edge">the edge</param>
        /// <param name="prob">the probability: a number between 0 and 1</param>
        internal void SetEdgeProbability(Edge edge,double prob)
        {
            if(this.edgeProbabilities==null)
            {
                edgeProbabilities=new RBMap();
            }

            edgeProbabilities[edge]=prob;
        }
Пример #6
0
        internal void InitEdgeProbabilities()
        {
            if(this.nondNeighbours.Count==0 || (edgeProbabilities!=null&&edgeProbabilities.Count>0))
            return;

              edgeProbabilities=new RBMap();

              foreach( DictionaryEntry p in this.nondNeighbours)
            {
              int i=(int)p.Key;
              ArrayList edges=EdgesAtVertex(i) as ArrayList;

              double pr=1.0/edges.Count;
              foreach(Edge l in edges)
            this.edgeProbabilities[l]=pr;
            }
        }
Пример #7
0
        /// <summary>
        /// Creates a graph from a file.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        internal static Graph CreateFromFile(string fileName)
        {
            StreamReader sr=new StreamReader(fileName);

              if(sr.BaseStream.CanRead==false)
            {
              sr.Close();
              return null;
            }

              sr.ReadLine(); //swallow start vertex

              int initVertex= Int32.Parse(sr.ReadLine());

              ArrayList must=new ArrayList();
              sr.ReadLine();//swallow "must"

              RBMap edgeProbs=new RBMap();

              string s=sr.ReadLine();
              while(s!="optional"){

            //string [] split=s.Split(' ');

            must.Add(ReadEdge(s,edgeProbs));

            s=sr.ReadLine();

              }

              //"optional" has been swallowed already

              s=sr.ReadLine();
              ArrayList opt=new ArrayList();

              while(s.StartsWith("nond vertices")==false){

            opt.Add(ReadEdge(s,edgeProbs));

            s=sr.ReadLine();

              }

              string [] spl=s.Split(' ');
              ArrayList nondVertices=new ArrayList();

              for(int i=2;i<spl.Length;i++){
                if(!String.IsNullOrEmpty(spl[i]))
                    nondVertices.Add(Int32.Parse(spl[i]));
              }

              Graph g=new Graph(initVertex,must.ToArray(typeof(Edge)) as Edge[],
                        opt.ToArray(typeof(Edge)) as Edge[],
                        nondVertices.ToArray(typeof(int)) as int[],WeakClosureEnum.DoNotClose);

              g.edgeProbabilities=edgeProbs;
              return g;
        }
Пример #8
0
        internal Graph(int initialVertex, Edge[] mustEdges,Edge[] optionalEdges, int [] nondetVertices,bool builtNondNeigh,WeakClosureEnum closerInstuction)
            : this(initialVertex,mustEdges,optionalEdges,closerInstuction)
        {
            if(builtNondNeigh==true)
            {
              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);
                }
            }
            }
              else
            this.nondNeighbours=null;
        }
Пример #9
0
        static Edge ReadEdge(string s,RBMap probabilities)
        {
            string [] split=s.Split(' ');
              Edge edge=new Edge(Int32.Parse(split[0]), Int32.Parse(split[2]),Int32.Parse(split[1]),Int32.Parse(split[3]));

              if(split.Length==5){

            probabilities[edge]=Double.Parse(split[4]);

              }

              return edge;
        }
Пример #10
0
        /// <summary>
        /// Creates a graph from a file.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        internal static Graph CreateFromFile(string fileName)
        {
            StreamReader sr = new StreamReader(fileName);

            if (sr.BaseStream.CanRead == false)
            {
                sr.Close();
                return(null);
            }


            sr.ReadLine(); //swallow start vertex

            int initVertex = Int32.Parse(sr.ReadLine());

            ArrayList must = new ArrayList();

            sr.ReadLine();//swallow "must"


            RBMap edgeProbs = new RBMap();

            string s = sr.ReadLine();

            while (s != "optional")
            {
                //string [] split=s.Split(' ');

                must.Add(ReadEdge(s, edgeProbs));

                s = sr.ReadLine();
            }


            //"optional" has been swallowed already

            s = sr.ReadLine();
            ArrayList opt = new ArrayList();

            while (s.StartsWith("nond vertices") == false)
            {
                opt.Add(ReadEdge(s, edgeProbs));

                s = sr.ReadLine();
            }

            string [] spl          = s.Split(' ');
            ArrayList nondVertices = new ArrayList();

            for (int i = 2; i < spl.Length; i++)
            {
                if (!String.IsNullOrEmpty(spl[i]))
                {
                    nondVertices.Add(Int32.Parse(spl[i]));
                }
            }


            Graph g = new Graph(initVertex, must.ToArray(typeof(Edge)) as Edge[],
                                opt.ToArray(typeof(Edge)) as Edge[],
                                nondVertices.ToArray(typeof(int)) as int[], WeakClosureEnum.DoNotClose);

            g.edgeProbabilities = edgeProbs;
            return(g);
        }