コード例 #1
0
ファイル: graph.cs プロジェクト: philscrace2/NModel
        /// <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
ファイル: graph.cs プロジェクト: philscrace2/NModel
        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
ファイル: graph.cs プロジェクト: philscrace2/NModel
        /// <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
ファイル: graph.cs プロジェクト: philscrace2/NModel
        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
ファイル: graph.cs プロジェクト: juhan/NModel
        /// <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
ファイル: graph.cs プロジェクト: juhan/NModel
        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
ファイル: graph.cs プロジェクト: juhan/NModel
        /// <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
ファイル: graph.cs プロジェクト: juhan/NModel
        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
ファイル: graph.cs プロジェクト: juhan/NModel
        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
ファイル: graph.cs プロジェクト: philscrace2/NModel
        /// <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);
        }