/// <summary> /// Get the start and end vertices of the broken edges /// (those which formerly joined the start and end partitions) /// If there aren't enough broken edges then randomly choose vertices /// from the corresponding partitions. /// </summary> /// <param name="brokenEdges">The edges formerly joining the two partitions</param> /// <param name="index">The index of the broken edge to use</param> /// <param name="visitedVertices">The vertices in the visited (end) partition</param> /// <param name="unvisitedVertices">The vertices in the unvisited (start) partition</param> /// <param name="start">The start vertex</param> /// <param name="end">The end vertex</param> private static void GetStartAndEnd(List<Edge> brokenEdges, int index, List<Vertex> visitedVertices, List<Vertex> unvisitedVertices, out Vertex start, out Vertex end) { if (index < brokenEdges.Count) { Edge edge = brokenEdges[index]; if (edge.End.Visited) { edge = edge.Reversed; } start = edge.Start; end = edge.End; } else { if (random.Next(2) == 0) { start = visitedVertices[random.Next(visitedVertices.Count)]; end = unvisitedVertices[random.Next(unvisitedVertices.Count)]; } else { start = null; end = null; } } }
/// <summary> /// Initialise a new Edge /// </summary> /// <param name="start">The start Vertex</param> /// <param name="end">The end Vertex</param> public Edge(Vertex start, Vertex end) { this.end = end; this.otherSide = new Edge(start, this); end.AddEdge(otherSide); }
/// <summary> /// Initialise a new Edge when the 'other side' already exists. /// </summary> /// <param name="end">The end Vertex of this edge.</param> /// <param name="otherSide">The 'other side' of this edge.</param> private Edge(Vertex end, Edge otherSide) { this.end = end; this.otherSide = otherSide; end.AddEdge(otherSide); }
public void TestD5() { AllInOneOperator AIOO = new AllInOneOperator(null); AATreeGeneration generation = new AATreeGeneration(); ArrayList individuals = new ArrayList(); //Populate with identical entries. RoadNetwork theRoad; Vertex[] theVertices = new Vertex[5]; Map theMap = Map.FromFile("map.xml"); ArrayList RN = new ArrayList(); for (int i = 0; i < 10; i++) { theRoad = new RoadNetwork(theMap); RN.Add(theRoad); //RN[i] = new RoadNetwork(theMap); theVertices[0] = ((RoadNetwork)RN[i]).AddVertex(0, 0); theVertices[1] = ((RoadNetwork)RN[i]).AddVertex(10, 0); theVertices[2] = ((RoadNetwork)RN[i]).AddVertex(5, 5); theVertices[3] = ((RoadNetwork)RN[i]).AddVertex(0, 10); theVertices[4] = ((RoadNetwork)RN[i]).AddVertex(10, 10); ((RoadNetwork)RN[i]).AddEdge(theVertices[0], theVertices[1]); ((RoadNetwork)RN[i]).AddEdge(theVertices[0], theVertices[3]); ((RoadNetwork)RN[i]).AddEdge(theVertices[3], theVertices[4]); ((RoadNetwork)RN[i]).AddEdge(theVertices[1], theVertices[4]); ((RoadNetwork)RN[i]).AddEdge(theVertices[0], theVertices[2]); ((RoadNetwork)RN[i]).AddEdge(theVertices[1], theVertices[2]); ((RoadNetwork)RN[i]).AddEdge(theVertices[3], theVertices[2]); ((RoadNetwork)RN[i]).AddEdge(theVertices[4], theVertices[2]); //Add vertex and add edge generation.Insert(RN[i], 1); } AIOO.Operate(generation, individuals); //Ensure population is made up of valid roadnetworks. //Ensure all are not the same. Boolean passed = false; for (int i = 0; i < individuals.Count; i++) { for (int j = 0; j < individuals.Count; j++) { //Cross check each of the 5 vertices for (int k = 0; k < ((RoadNetwork)individuals[i]).VertexCount; k++) { for (int l = 0; l < ((RoadNetwork)individuals[j]).VertexCount; l++) { if (((RoadNetwork)individuals[i]).GetVertex(k).Coordinates.X != ((RoadNetwork)individuals[j]).GetVertex(l).Coordinates.X) { passed = true; } } } } } Assert.IsTrue(passed); }
public void TestF1() { RoadNetworkXmlOutputter RNO = new RoadNetworkXmlOutputter("map.xml"); Map theMap; theMap = Map.FromFile("map.xml"); Vertex[] theVertices = new Vertex[4]; RoadNetwork RN1 = new RoadNetwork(theMap); theVertices[0] = ((RoadNetwork)RN1).AddVertex(0, 0); theVertices[1] = ((RoadNetwork)RN1).AddVertex(10, 0); ((RoadNetwork)RN1).AddEdge(theVertices[0], theVertices[1]); RoadNetwork RN2 = new RoadNetwork(theMap); theVertices[2] = ((RoadNetwork)RN2).AddVertex(5, 5); theVertices[3] = ((RoadNetwork)RN2).AddVertex(10, 10); ((RoadNetwork)RN2).AddEdge(theVertices[2], theVertices[3]); AATreeGeneration generation = new AATreeGeneration(); generation.Insert(RN1, 1); generation.Insert(RN2, 2); XmlWriter writer1 = XmlWriter.Create("test1xml.xml"); XmlWriter writer2 = XmlWriter.Create("test2xml.xml"); //Output individuals/generation somehow. RN1.WriteXml(writer1); writer1.Close(); RN2.WriteXml(writer2); writer2.Close(); XmlTextReader reader1 = new XmlTextReader("test1xml.xml"); XmlTextReader reader2= new XmlTextReader("test2xml.xml"); RoadNetworkReader rnr = new RoadNetworkReader(); RoadNetwork RNL1 = (RoadNetwork)rnr.ReadIndividual(reader1); RoadNetwork RNL2 = (RoadNetwork)rnr.ReadIndividual(reader2); Assert.AreEqual(RNL1.GetVertex(0).Coordinates.X, 0); Assert.AreEqual(RNL1.GetVertex(0).Coordinates.Y, 0); Assert.AreEqual(RNL1.GetVertex(1).Coordinates.X, 10); Assert.AreEqual(RNL1.GetVertex(1).Coordinates.Y, 0); Assert.AreEqual(RNL2.GetVertex(0).Coordinates.X, 5); Assert.AreEqual(RNL2.GetVertex(0).Coordinates.Y, 5); Assert.AreEqual(RNL2.GetVertex(1).Coordinates.X, 10); Assert.AreEqual(RNL2.GetVertex(1).Coordinates.Y, 10); Assert.AreEqual(RNL1.GetEdge(0).Start.Coordinates.X, 0); Assert.AreEqual(RNL1.GetEdge(0).Start.Coordinates.Y, 0); Assert.AreEqual(RNL1.GetEdge(0).End.Coordinates.X, 10); Assert.AreEqual(RNL1.GetEdge(0).End.Coordinates.Y, 0); Assert.AreEqual(RNL2.GetEdge(0).Start.Coordinates.X, 5); Assert.AreEqual(RNL2.GetEdge(0).Start.Coordinates.Y, 5); Assert.AreEqual(RNL2.GetEdge(0).End.Coordinates.X, 10); Assert.AreEqual(RNL2.GetEdge(0).End.Coordinates.Y, 10); }
/// <summary> /// Explore the edges leading from a vertex /// </summary> /// <param name="vertex"> /// The <see cref="Vertex"/> to work from /// </param> /// <param name="path"> /// The <see cref="Path"/> segment which led to this <see cref="Vertex"/> /// </param> /// <param name="pathQueue"> /// Queue to add the new <see cref="Path"/> objects to, to be explored later. /// </param> private void VisitVertex(Vertex vertex, Path path, LinkedList<Path> pathQueue) { vertex.Visited = true; for (int i = 0; i < vertex.EdgeCount; i++) { Edge edge = vertex.GetEdge(i); if (!edge.Broken) { pathQueue.AddLast(new Path(edge, path)); } } }
/// <summary> /// Make a path between two vertices in a RoadNetwork out of a number of edges connecting adjacent points. /// </summary> /// <param name="network">The RoadNetwork to make the path in</param> /// <param name="start">The start Vertex of the path</param> /// <param name="end">The end Vertex of the path</param> public static void StepPath(RoadNetwork network, Vertex start, Vertex end) { int dX = end.Coordinates.X - start.Coordinates.X; int dY = end.Coordinates.Y - start.Coordinates.Y; int xStep = 1; if (dX < 0) { dX = -dX; xStep = -1; } int yStep = 1; if (dY < 0) { dY = -dY; yStep = -1; } int x = start.Coordinates.X; int y = start.Coordinates.Y; Vertex startVertex = start; while (dX > 1 || dY > 1) { if (dX > dY) { x += xStep; dX--; } else if (dY > dX) { y += yStep; dY--; } else { x += xStep; dX--; y += yStep; dY--; } int index = network.AddVertexUnique(x, y); Vertex endVertex = network.GetVertex(index); network.AddEdge(startVertex, endVertex); startVertex = endVertex; } network.AddEdge(startVertex, end); }
/// <summary> /// Add a copy of a vertex to the network through the AddVertexUnique method. /// </summary> /// <param name="vertex">The vertex to copy</param> /// <returns>The index of the new vertex.</returns> public int CopyVertexUnique(Vertex vertex) { int index = AddVertexUnique(vertex.Coordinates); vertex.Copy = vertices[index]; return index; }
/// <summary> /// Add a copy of a vertex to the network. /// </summary> /// <param name="vertex"> /// The vertex to copy /// </param> public void CopyVertex(Vertex vertex) { vertex.Copy = AddVertex(vertex.Coordinates); }
/// <summary> /// Add a vertex to the network /// </summary> /// <param name="coordinates"> /// A <see cref="Coordinates"/> object denoting the position /// </param> /// <returns> /// The added <see cref="Vertex"/> /// </returns> public Vertex AddVertex(Coordinates coordinates) { Vertex vertex = new Vertex(this, coordinates); vertices.Add(vertex); return vertex; }
/// <summary> /// Add an edge joining two <see cref="Vertex"/> objects /// </summary> /// <param name="start"> /// Start <see cref="Vertex"/> /// </param> /// <param name="end"> /// End <see cref="Vertex"/> /// </param> /// <returns> /// The added <see cref="Edge"/> /// </returns> public Edge AddEdge(Vertex start, Vertex end) { if (!start.BelongsToNetwork(this)) { throw new Exception("Start Vertex does not belong to this RoadNetwork."); } if (!end.BelongsToNetwork(this)) { throw new Exception("End Vertex does not belong to this RoadNetwork."); } Edge edge = new Edge(start, end); edges.Add(edge); return edge; }
private Vertex MoveVertex(RoadNetwork network, Vertex template) { double x = (double)template.Coordinates.X * (0.5 + random.NextDouble()); double y = (double)template.Coordinates.Y * (0.5 + random.NextDouble()); // Clip values if (x >= network.Map.Width) x = network.Map.Width; if (y >= network.Map.Height) y = network.Map.Height; return network.AddVertex( new Coordinates((int)x, (int)y) ); }