public void Constructor_Throws()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();
            var chain = new NormalizedMarkovEdgeChain <int, Edge <int> >();

            // ReSharper disable ObjectCreationAsStatement
            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(
                () => new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null));

            Assert.Throws <ArgumentNullException>(
                () => new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, null));
            Assert.Throws <ArgumentNullException>(
                () => new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null, chain));
            Assert.Throws <ArgumentNullException>(
                () => new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null, null));

            Assert.Throws <ArgumentNullException>(
                () => new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null, graph, null));
            Assert.Throws <ArgumentNullException>(
                () => new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null, null, chain));
            Assert.Throws <ArgumentNullException>(
                () => new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null, null, null));

            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, chain);

            Assert.Throws <ArgumentNullException>(() => algorithm.Rand = null);
            // ReSharper restore AssignNullToNotNullAttribute
            // ReSharper restore ObjectCreationAsStatement
        }
Esempio n. 2
0
        public static TryFunc <TVertex, IEnumerable <TEdge> > TreeCyclePoppingRandom <TVertex, TEdge>(
#if !NET20
            this
#endif
            IVertexListGraph <TVertex, TEdge> visitedGraph,
            TVertex root,
            IMarkovEdgeChain <TVertex, TEdge> edgeChain)
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(visitedGraph != null);
            Contract.Requires(root != null);
            Contract.Requires(visitedGraph.ContainsVertex(root));
            Contract.Ensures(Contract.Result <TryFunc <TVertex, IEnumerable <TEdge> > >() != null);

            var algo = new CyclePoppingRandomTreeAlgorithm <TVertex, TEdge>(visitedGraph, edgeChain);
            var predecessorRecorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (predecessorRecorder.Attach(algo))
                algo.Compute(root);

            var predecessors = predecessorRecorder.VertexPredecessors;

            return(delegate(TVertex v, out IEnumerable <TEdge> edges)
            {
                return EdgeExtensions.TryGetPath(predecessors, v, out edges);
            });
        }
Esempio n. 3
0
		public void Test(IVertexAndEdgeListGraph g, IVertex root)
		{
			this.root = root; 

			RandomWalkAlgorithm walker = new RandomWalkAlgorithm(g);
			walker.TreeEdge +=new EdgeEventHandler(walker_TreeEdge);
			walker.Generate(root,50);

			BidirectionalAdaptorGraph bg = new BidirectionalAdaptorGraph(g);
			ReversedBidirectionalGraph rg = new ReversedBidirectionalGraph(bg);
			CyclePoppingRandomTreeAlgorithm pop = new CyclePoppingRandomTreeAlgorithm(rg);

			pop.InitializeVertex +=new VertexEventHandler(this.InitializeVertex);
			pop.FinishVertex +=new VertexEventHandler(this.FinishVertex);
			pop.TreeEdge += new EdgeEventHandler(this.TreeEdge);

			pop.InitializeVertex +=new VertexEventHandler(vis.InitializeVertex);
			pop.TreeEdge += new EdgeEventHandler(vis.TreeEdge);
			pop.ClearTreeVertex += new VertexEventHandler(vis.ClearTreeVertex);

			pop.RandomTreeWithRoot(root);

			// plot tree...
			GraphvizAlgorithm gv = new GraphvizAlgorithm(g,".",GraphvizImageType.Svg);
			gv.FormatEdge +=new FormatEdgeEventHandler(this.FormatEdge);
			gv.FormatVertex +=new FormatVertexEventHandler(this.FormatVertex);

			gv.Write("randomtree");
		}
        public void SetRootVertex_Throws()
        {
            var graph     = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var chain     = new NormalizedMarkovEdgeChain <TestVertex, Edge <TestVertex> >();
            var algorithm = new CyclePoppingRandomTreeAlgorithm <TestVertex, Edge <TestVertex> >(graph, chain);

            SetRootVertex_Throws_Test(algorithm);
        }
        public void ClearRootVertex()
        {
            var graph     = new AdjacencyGraph <int, Edge <int> >();
            var chain     = new NormalizedMarkovEdgeChain <int, Edge <int> >();
            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, chain);

            ClearRootVertex_Test(algorithm);
        }
        public void Constructor()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();
            IMarkovEdgeChain <int, Edge <int> > markovChain1 = new NormalizedMarkovEdgeChain <int, Edge <int> >();
            IMarkovEdgeChain <int, Edge <int> > markovChain2 = new WeightedMarkovEdgeChain <int, Edge <int> >(new Dictionary <Edge <int>, double>());

            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

            algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, markovChain1);
            AssertAlgorithmProperties(algorithm, graph, markovChain1);

            algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, markovChain2);
            AssertAlgorithmProperties(algorithm, graph, markovChain2);

            algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null, graph, markovChain1);
            AssertAlgorithmProperties(algorithm, graph, markovChain1);

            var random = new Random(123456);

            algorithm.Rand = random;
            AssertAlgorithmProperties(algorithm, graph, markovChain1, random);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                CyclePoppingRandomTreeAlgorithm <TVertex, TEdge> algo,
                IVertexListGraph <TVertex, TEdge> g,
                IMarkovEdgeChain <TVertex, TEdge> chain = null,
                Random rand = null
                )
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                if (chain is null)
                {
                    Assert.IsNotNull(algo.EdgeChain);
                }
                else
                {
                    Assert.AreSame(chain, algo.EdgeChain);
                }
                if (rand is null)
                {
                    Assert.IsNotNull(algo.Rand);
                }
                else
                {
                    Assert.AreSame(rand, algo.Rand);
                }
                CollectionAssert.IsEmpty(algo.Successors);
                CollectionAssert.IsEmpty(algo.VerticesColors);
            }

            #endregion
        }
Esempio n. 7
0
        public void IsolatedVertex()
        {
            AdjacencyGraph g = new AdjacencyGraph();

            g.AddVertex();

            target = new CyclePoppingRandomTreeAlgorithm(g);
            target.RandomTree();
        }
Esempio n. 8
0
        public void Repro13160()
        {
            // Create a new graph
            var graph = new BidirectionalGraph <int, SEquatableEdge <int> >(false);

            // Adding vertices
            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    graph.AddVertex(i * 3 + j);
                }
            }

            // Adding Width edges
            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 2; ++j)
                {
                    graph.AddEdge(
                        new SEquatableEdge <int>(i * 3 + j, i * 3 + j + 1));
                }
            }

            // Adding Length edges
            for (int i = 0; i < 2; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    graph.AddEdge(
                        new SEquatableEdge <int>(i * 3 + j, (i + 1) * 3 + j));
                }
            }

            // Create cross edges
            foreach (SEquatableEdge <int> edge in graph.Edges)
            {
                graph.AddEdge(new SEquatableEdge <int>(edge.Target, edge.Source));
            }

            // Breaking graph apart
            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    if (i == 1)
                    {
                        graph.RemoveVertex(i * 3 + j);
                    }
                }
            }

            var target = new CyclePoppingRandomTreeAlgorithm <int, SEquatableEdge <int> >(graph);

            target.Compute(2);
        }
        public void ComputeWithRoot()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();
            var chain = new NormalizedMarkovEdgeChain <int, Edge <int> >();

            graph.AddVertex(0);
            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, chain);

            ComputeWithRoot_Test(algorithm);
        }
Esempio n. 10
0
        public void RootIsNotAccessible()
        {
            AdjacencyGraph g    = new AdjacencyGraph();
            IVertex        root = g.AddVertex();
            IVertex        v    = g.AddVertex();

            g.AddEdge(root, v);

            target = new CyclePoppingRandomTreeAlgorithm(g);
            target.RandomTreeWithRoot(root);
        }
Esempio n. 11
0
 public void CyclePoppingRandomTreeAll()
 {
     foreach (AdjacencyGraph <string, Edge <string> > graph in TestGraphFactory.GetAdjacencyGraphs())
     {
         foreach (string root in graph.Vertices)
         {
             var target = new CyclePoppingRandomTreeAlgorithm <string, Edge <string> >(graph);
             target.Compute(root);
         }
     }
 }
Esempio n. 12
0
        public void IsolatedVerticesWithRoot()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >(true);

            graph.AddVertex(0);
            graph.AddVertex(1);

            var target = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph);

            target.RandomTreeWithRoot(0);
        }
        public void IsolatedVerticesWithRoot()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >(true);

            graph.AddVertex(0);
            graph.AddVertex(1);

            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph);

            algorithm.RandomTreeWithRoot(0);
            AssertIsTree(0, algorithm.Successors);
        }
Esempio n. 14
0
        public void RootIsNotAccessible()
        {
            AdjacencyGraph <int, Edge <int> > graph = new AdjacencyGraph <int, Edge <int> >(true);

            graph.AddVertex(0);
            graph.AddVertex(1);
            graph.AddEdge(new Edge <int>(0, 1));

            var target = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph);

            target.RandomTreeWithRoot(0);
        }
        public void GetVertexColor_Throws()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertex(0);
            var chain     = new NormalizedMarkovEdgeChain <int, Edge <int> >();
            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, chain);

            algorithm.Compute(0);

            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => algorithm.GetVertexColor(1));
        }
        public void RootIsNotAccessible()
        {
            AdjacencyGraph <int, Edge <int> > graph = new AdjacencyGraph <int, Edge <int> >(true);

            graph.AddVertex(0);
            graph.AddVertex(1);
            graph.AddEdge(new Edge <int>(0, 1));

            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph);

            algorithm.RandomTreeWithRoot(0);
            AssertIsTree(0, algorithm.Successors);
        }
        public void GetVertexColor()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVerticesAndEdge(new Edge <int>(1, 2));
            var chain = new NormalizedMarkovEdgeChain <int, Edge <int> >();

            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, chain);

            algorithm.Compute(1);

            Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(1));
            Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(2));
        }
Esempio n. 18
0
        public static CyclePoppingRandomTreeAlgorithm <T, Edge <T> > CreateAlgorithmAndMaybeDoComputation <T>(
            [NotNull] ContractScenario <T> scenario)
        {
            var graph = new AdjacencyGraph <T, Edge <T> >();

            graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <T>(e.Source, e.Target)));
            graph.AddVertexRange(scenario.SingleVerticesInGraph);
            var chain = new NormalizedMarkovEdgeChain <T, Edge <T> >();

            var algorithm = new CyclePoppingRandomTreeAlgorithm <T, Edge <T> >(graph, chain);

            if (scenario.DoComputation)
            {
                algorithm.Compute(scenario.Root);
            }
            return(algorithm);
        }
Esempio n. 19
0
        public void GenerateWalls(int outi, int outj, int ini, int inj)
        {
            // here we use a uniform probability distribution among the out-edges
            CyclePoppingRandomTreeAlgorithm pop =
                new CyclePoppingRandomTreeAlgorithm(this.graph);

            // we can also weight the out-edges

/*
 *                      EdgeDoubleDictionary weights = new EdgeDoubleDictionary();
 *                      foreach(IEdge e in this.graph.Edges)
 *                              weights[e]=1;
 *                      pop.EdgeChain = new WeightedMarkovEdgeChain(weights);
 */
            IVertex root = this.latice[outi, outj];

            if (root == null)
            {
                throw new ArgumentException("outi,outj vertex not found");
            }

            pop.RandomTreeWithRoot(this.latice[outi, outj]);
            this.successors = pop.Successors;

            // build the path to ini, inj
            IVertex v = this.latice[ini, inj];

            if (v == null)
            {
                throw new ArgumentException("ini,inj vertex not found");
            }
            this.outPath = new EdgeCollection();
            while (v != root)
            {
                IEdge e = this.successors[v];
                if (e == null)
                {
                    throw new Exception();
                }
                this.outPath.Add(e);
                v = e.Target;
            }
        }
        private static void RunCyclePoppingRandomTreeAndCheck <TVertex, TEdge>(
            IVertexListGraph <TVertex, TEdge> graph,
            TVertex root)
            where TEdge : IEdge <TVertex>
        {
            var randomChain = new Random(123456);
            var chain       = new NormalizedMarkovEdgeChain <TVertex, TEdge>
            {
                Rand = randomChain
            };
            var randomAlgorithm = new Random(123456);
            var algorithm       = new CyclePoppingRandomTreeAlgorithm <TVertex, TEdge>(graph, chain)
            {
                Rand = randomAlgorithm
            };

            algorithm.InitializeVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.White, algorithm.VerticesColors[vertex]);
            };

            algorithm.FinishVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.Black, algorithm.VerticesColors[vertex]);
            };

            algorithm.Compute(root);

            Assert.AreEqual(graph.VertexCount, algorithm.VerticesColors.Count);
            foreach (TVertex vertex in graph.Vertices)
            {
                Assert.AreEqual(GraphColor.Black, algorithm.VerticesColors[vertex]);
            }

            AssertIsTree(root, algorithm.Successors);
        }
Esempio n. 21
0
 public void Loop()
 {
     target = new CyclePoppingRandomTreeAlgorithm(GraphFactory.Loop());
     target.RandomTree();
 }
Esempio n. 22
0
        /// <summary>
        /// Intelligent Path Generator
        /// </summary>
        /// <param name="rootX">Root x: Must be less than tileAmount</param>
        /// <param name="rootZ">Root z: Must be less than tileAmount</param>
        public bool GeneratePath(string type, bool automated, int rootX, int rootZ)
        {
            assertMessage = string.Format("Point: {0} : {1} is the entrance for this module", rootX, rootZ);
            Debug.Assert(DebugStatements == true, assertMessage);
            //Console.ReadLine();

            // fill in the entrance vertex for searcher
            pointFirst.Width  = rootX;
            pointFirst.Length = rootZ;

            this.vedSuccessors = new Dictionary <Point <int>, Edge <Point <int> > >();

            // create cross edges to avoid missing edges
            foreach (Edge <Point <int> > e in this.graph.Edges)
            {
                this.edge = new Edge <Point <int> >(e.Target, e.Source);
                this.graph.AddEdge(edge);
            }

            if (automated)    // TODO
            // find a branch(entrence) and root(exit) that exists on the borders
            {
                int tick = 0;
                foreach (Point <int> v in this.graph.Vertices)
                {
                    if (this.graph.ContainsVertex(v) & this.graph.Degree(v) > 0)   // if it has edges
                    {
                        if (tick == 0)
                        {
                            //branchX = v.Width;
                            //branchZ = v.Length;
                            tick++;
                        }
                        rootX = v.Width;
                        rootZ = v.Length;
                        // Debug.Assert(DebugStatements == true,"{0}", v.ToString());
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            // weight the out-edges - POSSIBILITY
            Dictionary <Edge <Point <int> >, double> weights = new Dictionary <Edge <Point <int> >, double>();

            foreach (Edge <Point <int> > e in this.graph.Edges)
            {
                weights[e] = 1;
            }

            // creates mazed edges from the uniform graphs vertices
            this.pop = new CyclePoppingRandomTreeAlgorithm <Point <int>, Edge <Point <int> > >(this.graph /*, new WeightedMarkovEdgeChain<Point<int>, Edge<Point<int>>>(weights)*/);

            this.pop.StateChanged += new EventHandler(this.StateStatus);

            try { // create a Random Tree Maze with entrence root. Creates new edges for you.
                pop.RandomTreeWithRoot(new Point <int>(rootX, rootZ));
            }
            catch (Exception ex) {
                Debug.Assert(DebugStatements == true, "{0} Module could not be pathed... SKIPPING", ex.ToString());
                //Console.ReadLine();
                return(false);
            }

            this.vedSuccessors = pop.Successors;

            // delete all edges before edge re-population -- helps remove edgeless vertices
            foreach (Point <int> v in this.graph.Vertices)
            {
                this.graph.ClearEdges(v);
            }

            if (type == "MODULE")
            {
                this.md.Clear();
                Debug.Assert(DebugStatements == true, "--------Adding mazed RandomTreeWithRootBranch graph to dictionary--------");
                assertMessage = string.Format("VEDSuccesors Count is: {0}", this.vedSuccessors.Count);
                Debug.Assert(DebugStatements == true, assertMessage);
                // put vedSuccessors in MultiDictionary for mazed map creation (populate edges)
                foreach (Edge <Point <int> > e in this.vedSuccessors.Values)
                {
                    if (e == null)   // if empty, skip and continue the edge search
                    {
                        Debug.Assert(DebugStatements == true, "--------EDGE IS EMPTY--------");
                        continue;
                    }

                    // grab these mazed vertex/edges for vertex/edge print-out in CreateTileset()
                    this.md.Add(e.Source, e); // out-edge
                    this.md.Add(e.Target, e); // in-edge
                    assertMessage = string.Format("Edge {0}", e.Target, e.Source);
                    Debug.Assert(DebugStatements == true, assertMessage);
                    // redo the cleared graph with new random edges
                    this.graph.AddEdge(e);
                }
                assertMessage = string.Format("FINAL Graph Edge Count is: {0}", graph.EdgeCount);
                Debug.Assert(DebugStatements == true, assertMessage);
                assertMessage = string.Format("FINAL MD VERTEX Count is: {0}", this.md.Keys.Count);
                Debug.Assert(DebugStatements == true, assertMessage);
                assertMessage = string.Format("FINAL MD TOTAL Edge COUNT is: {0}", this.md.Values.Count);
                Debug.Assert(DebugStatements == true, assertMessage);
                assertMessage = string.Format("FINAL MD Edges details are: {0}", this.md.KeyValuePairs);
                Debug.Assert(DebugStatements == true, assertMessage);
            }

            if (type == "QUADRANT")
            {
                this.md2.Clear();
                // put graph in MultiDictionary for map creation (populate edges)
                foreach (Edge <Point <int> > e in this.vedSuccessors.Values)
                {
                    if (e == null)   // if empty, skip and continue the edge search
                    {
                        continue;
                    }
                    //Debug.Assert(DebugStatements == true,"{0} was successfull.", e.ID);
                    // grab these mazed vertex/edges for vertex/edge print-out in CreateTileset()
                    this.md2.Add(e.Source, e); // out-edge
                    this.md2.Add(e.Target, e); // in-edge
                    //Debug.Assert(DebugStatements == true,"Edge {0}", e.Target, e.Source);
                    // redo the cleared graph with new random edges
                    this.graph.AddEdge(e);
                }
            }

            // remove edgeless vertices
            for (int x = 0; x < this.Width; x++)
            {
                for (int z = 0; z < this.Length; z++)
                {
                    if (this.graph.ContainsVertex(new Point <int>(x, z)))
                    {
                        if (this.graph.Degree(new Point <int>(x, z)) == 0)
                        {
                            this.graph.RemoveVertex(new Point <int>(x, z));
                        }
                    }
                }
            }
            Debug.Assert(DebugStatements == true, "--------Cleaned up edgeless vertices--------");
            //Console.ReadLine();
            // GC.Collect(); // could probably be used. Not too slow.

            return(true);
        }
        public void Repro13160()
        {
            // Create a new graph
            var graph = new BidirectionalGraph <int, Edge <int> >(false);

            // Adding vertices
            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    graph.AddVertex(i * 3 + j);
                }
            }

            // Adding Width edges
            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 2; ++j)
                {
                    graph.AddEdge(
                        new Edge <int>(i * 3 + j, i * 3 + j + 1));
                }
            }

            // Adding Length edges
            for (int i = 0; i < 2; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    graph.AddEdge(
                        new Edge <int>(i * 3 + j, (i + 1) * 3 + j));
                }
            }

            // Create cross edges
            foreach (Edge <int> edge in graph.Edges)
            {
                graph.AddEdge(new Edge <int>(edge.Target, edge.Source));
            }

            // Breaking graph apart
            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    if (i == 1)
                    {
                        graph.RemoveVertex(i * 3 + j);
                    }
                }
            }

            var randomChain = new Random(123456);
            var chain       = new NormalizedMarkovEdgeChain <int, Edge <int> >
            {
                Rand = randomChain
            };
            var randomAlgorithm = new Random(123456);
            var algorithm       = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, chain)
            {
                Rand = randomAlgorithm
            };

            Assert.DoesNotThrow(() => algorithm.Compute(2));
            // Successors is not a spanning tree...
        }