예제 #1
0
        /// <summary>
        /// Generate an initial population of individuals
        /// </summary>
        /// <param name="individuals">An empty list to populate with the individuals</param>
        public void Populate(ArrayList individuals)
        {
            RoadNetwork original = new RoadNetwork(map);
            original.AddEdge(original.AddVertex(map.Start), original.AddVertex(map.End));
            individuals.Add(original);

            while (individuals.Count < populationSize)
            {
                individuals.Add(Mutator.Mutate((RoadNetwork)individuals[random.Next(individuals.Count)]));
            }
        }
예제 #2
0
        /// <summary>
        /// Generate two child RoadNetworks by recombining halves of two parent RoadNetworks.
        /// </summary>
        /// <param name="parent1">The first parent RoadNetwork</param>
        /// <param name="parent2">The second parent RoadNetwork</param>
        /// <param name="child1">The first child RoadNetwork</param>
        /// <param name="child2">The second child RoadNetwork</param>
        public static void Conjugate(RoadNetwork parent1, RoadNetwork parent2, out RoadNetwork child1, out RoadNetwork child2)
        {
            Cut(parent1);
            Cut(parent2);

            List<Vertex> startVertexPartition1 = new List<Vertex>();
            List<Vertex> endVertexPartition1 = new List<Vertex>();
            List<Vertex> startVertexPartition2 = new List<Vertex>();
            List<Vertex> endVertexPartition2 = new List<Vertex>();

            List<Edge> startEdgePartition1 = new List<Edge>();
            List<Edge> endEdgePartition1 = new List<Edge>();
            List<Edge> startEdgePartition2 = new List<Edge>();
            List<Edge> endEdgePartition2 = new List<Edge>();
            List<Edge> brokenEdgePartition1 = new List<Edge>();
            List<Edge> brokenEdgePartition2 = new List<Edge>();

            parent1.PartitionVertices(startVertexPartition1, endVertexPartition1);
            parent2.PartitionVertices(startVertexPartition2, endVertexPartition2);

            parent1.PartitionEdges(startEdgePartition1, endEdgePartition1, brokenEdgePartition1);
            parent2.PartitionEdges(startEdgePartition2, endEdgePartition2, brokenEdgePartition2);

            child1 = new RoadNetwork(parent1.Map);
            child2 = new RoadNetwork(parent2.Map);

            child1.CopyVertices(startVertexPartition1);
            child1.CopyEdges(startEdgePartition1);

            child1.CopyVertices(endVertexPartition2);
            child1.CopyEdges(endEdgePartition2);

            child2.CopyVertices(startVertexPartition2);
            child2.CopyEdges(startEdgePartition2);

            child2.CopyVertices(endVertexPartition1);
            child2.CopyEdges(endEdgePartition1);

            ShuffleEdges(brokenEdgePartition1);
            ShuffleEdges(brokenEdgePartition2);

            int nEdges = Math.Max(brokenEdgePartition1.Count, brokenEdgePartition2.Count);

            for (int i = 0; i < nEdges; i++)
            {
                Vertex start1;
                Vertex end1;
                Vertex start2;
                Vertex end2;

                GetStartAndEnd(brokenEdgePartition1, i, endVertexPartition1, startVertexPartition1, out start1, out end1);
                GetStartAndEnd(brokenEdgePartition2, i, endVertexPartition2, startVertexPartition2, out start2, out end2);

                if (start1 != null && start2 != null)
                {
                    child2.AddEdge(start1.Copy, end2.Copy);
                    child1.AddEdge(start2.Copy, end1.Copy);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Process a Network, breaking up all edges into small steps so that edges
        /// only join adjacent points.
        /// </summary>
        /// <param name="network">The RoadNetwork to process</param>
        /// <returns>The resulting RoadNetwork</returns>
        public static RoadNetwork MakeStepped(RoadNetwork network)
        {
            RoadNetwork result = new RoadNetwork(network, false, true);
            int endVertexIndex = result.VertexCount - 1;

            for (int i = 0; i < network.EdgeCount; i++)
            {
                Edge edge = network.GetEdge(i);
                Vertex start = edge.Start.Copy;
                Vertex end = edge.End.Copy;
                Coordinates startCoordinates = start.Coordinates;
                Coordinates endCoordinates = end.Coordinates;

                int dX = endCoordinates.X - startCoordinates.X;
                int dY = endCoordinates.Y - startCoordinates.Y;

                int xStep = 1;
                if (dX < 0)
                {
                    dX = -dX;
                    xStep = -1;
                }

                int yStep = 1;
                if (dY < 0)
                {
                    dY = -dY;
                    yStep = -1;
                }

                int x = startCoordinates.X;
                int y = startCoordinates.Y;

                Vertex startPoint = 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--;
                    }

                    Vertex endPoint = result.AddVertex(x, y);
                    result.AddEdge(startPoint, endPoint);
                    startPoint = endPoint;
                }

                result.AddEdge(startPoint, end);
            }

            result.SetEnd(endVertexIndex);

            return result;
        }
예제 #4
0
        /// <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);
        }
예제 #5
0
        /// <summary>
        /// Randomly alter a RoadNetwork to Produce a slightly different RoadNetwork
        /// </summary>
        /// <param name="source">The RoadNetwork to mutate</param>
        /// <returns>The mutated RoadNetwork</returns>
        public static RoadNetwork Mutate(RoadNetwork source)
        {
            Map map = source.Map;
            int mapWidth = map.Width;
            int mapHeight = map.Height;

            int maxXChange = 1 + mapWidth / 10;
            int maxYChange = 1 + mapHeight / 10;

            source.ClearCopies();
            source.SetVisited(false);
            source.SetBroken(true);

            RoadNetwork destination = new RoadNetwork(map);
            int endVertexIndex = -1;

            LinkedList<Vertex> vertexStack = new LinkedList<Vertex>();
            source.Start.Visited = true;
            vertexStack.AddFirst(source.Start);

            while (vertexStack.Count > 0)
            {
                Vertex vertex = vertexStack.First();
                vertexStack.RemoveFirst();

                List<Vertex> nextVertices = new List<Vertex>();
                for (int i = 0; i < vertex.EdgeCount; i++)
                {
                    Edge edge = vertex.GetEdge(i);
                    Vertex nextVertex = edge.End;

                    if (!nextVertex.Visited)
                    {
                        edge.Broken = false;
                        nextVertex.Visited = true;
                        nextVertices.Add(nextVertex);
                    }
                }

                //If this vertex is a leaf of the tree and not the end vertex it has a 25% chance of
                //begin removed.
                if (nextVertices.Count > 0 || vertex == source.End || random.Next(4) > 0)
                {
                    Coordinates coordinates = vertex.Coordinates;

                    if (vertex != source.Start && vertex != source.End && random.Next(4) == 0)
                    {
                        coordinates = RandomAdjacentPoint(vertex.Coordinates);
                    }

                    int index = destination.AddVertexUnique(vertex.Coordinates);
                    vertex.Copy = destination.GetVertex(index);

                    if (vertex == source.End)
                    {
                        endVertexIndex = index;
                    }
                }

                //Shuffle Vertices
                for (int n = nextVertices.Count - 1; n > 0; n--)
                {
                    int m = random.Next(n + 1);
                    Vertex temp = nextVertices[m];
                    nextVertices[m] = nextVertices[n];
                    nextVertices[n] = temp;
                }

                //Push vertices onto stack in shiffled order.
                foreach (Vertex nextVertex in nextVertices)
                {
                    vertexStack.AddFirst(nextVertex);
                }
            }

            for (int i = 0; i < source.EdgeCount; i++)
            {
                Edge edge = source.GetEdge(i);

                if ((!edge.Broken || random.Next(4) > 0) && edge.Start.Copy != null && edge.End.Copy != null)
                {
                    StepPath(destination, edge.Start.Copy, edge.End.Copy);
                }
            }

            int newVertices = random.Next(20);
            for (int i = 0; i < newVertices; i++)
            {
                Vertex startVertex = destination.GetVertex(random.Next(destination.VertexCount));
                int index = destination.AddVertexUnique(RandomAdjacentPoint(startVertex.Coordinates));
                Vertex endVertex = destination.GetVertex(index);
                destination.AddEdge(startVertex, endVertex);
            }

            if (endVertexIndex == -1)
            {
                throw new Exception("End vertex not found.");
            }
            else
            {
                destination.SetEnd(endVertexIndex);
            }

            return destination;
        }
예제 #6
0
        static RoadNetwork RandomNetwork(Map map)
        {
            Random random = new Random();

            RoadNetwork network = new RoadNetwork(map);

            network.AddVertex(map.Start);
            network.AddVertex(map.End);

            Vertex start = network.Start;
            Vertex end = network.End;

            for (int i = 0; i < 2; i++)
            {
                Vertex startPoint = start;

                for (int j = 0; j < 10; j++)
                {
                    Vertex endPoint = network.AddVertex(random.Next(map.Width), random.Next(map.Height));
                    network.AddEdge(startPoint, endPoint);
                    startPoint = endPoint;
                }

                network.AddEdge(startPoint, end);
            }

            for (int k = 0; k < 80; k++)
            {
                Vertex startPoint = network.GetVertex(random.Next(network.VertexCount));
                Vertex endPoint = network.AddVertex(random.Next(map.Width), random.Next(map.Height));
                network.AddEdge(startPoint, endPoint);
            }

            for (int l = 0; l < 20; l++)
            {
                Vertex startPoint = network.GetVertex(random.Next(network.VertexCount));
                Vertex endPoint = network.GetVertex(random.Next(network.VertexCount));
                network.AddEdge(startPoint, endPoint);
            }

            network.SetEnd(1);
            return network;
        }
예제 #7
0
        static void Save()
        {
            Random random = new Random();

            Map map = Map.FromFile("map.xml");

            IOutputter outputter = new RoadNetworkXmlOutputter("c:\\cits3200test\\roadnetworks.xml");
            outputter.OpenOutput();

            for (int g = 0; g < 10; g++)
            {
                AATreeGeneration generation = new AATreeGeneration();

                for (int i = 0; i < 20; i++)
                {
                    RoadNetwork network = new RoadNetwork(map);

                    for (int v = 0; v < 100; v++)
                    {
                        network.AddVertex(random.Next(100), random.Next(100));
                    }

                    for (int e = 0; e < 1000; e++)
                    {
                        network.AddEdge(network.GetVertex(random.Next(100)), network.GetVertex(random.Next(100)));
                    }

                    generation.Insert(network, (uint)i);
                }

                outputter.OutputGeneration(generation, g);
            }

            outputter.CloseOutput();
        }
예제 #8
0
        /// <summary>
        /// Perform mutations on the network
        /// </summary>
        /// <param name="ent">Network to mutate</param>
        public RoadNetwork Mutate(RoadNetwork ent)
        {
            RoadNetwork	ret = new RoadNetwork(ent.Map);

            ent.Start.Copy = ret.AddVertex(ent.Start.Coordinates);

            // Copy over verticies
            for (int ii = 1; ii < ent.VertexCount - 1; ii++)
            {
                Vertex v = ent.GetVertex(ii);
                v.Copy = null;
                // Only move the vertex if a random number succeeds
                if (random.NextDouble() < chance_delete_vertex)
                    continue;
                else if (random.NextDouble() < chance_move_vertex)
                    v.Copy = MoveVertex(ret, v);
                else
                    v.Copy = ret.AddVertex(v.Coordinates);
            }

            // Create some new verticies
            int num_new_verts = ent.EdgeCount;
            if (num_new_verts < min_vertex_base) num_new_verts = min_vertex_base;
            num_new_verts = (int)(num_new_verts * max_new_verticies * random.NextDouble());
            for( int ii = 0; ii < num_new_verts; ii ++ )
            {
                AddVertex(ret);
            }

            ent.End.Copy = ret.AddVertex(ent.End.Coordinates);

            // Copy over edges
            for (int ii = 0; ii < ent.EdgeCount; ii++)
            {
                Edge e = ent.GetEdge(ii);
                if (random.NextDouble() < chance_delete_edge)
                    continue;
                else if (e.Start.Copy == null || e.End.Copy == null)
                    continue;
                else
                    ret.AddEdge(e.Start.Copy, e.End.Copy);
            }

            // Create some new edges
            int num_new_edges = ent.EdgeCount;
            if(num_new_edges < min_edge_base) num_new_edges = min_edge_base;
            num_new_edges = (int)(num_new_edges * max_new_edges * random.NextDouble());
            for( int ii = 0; ii < num_new_edges; ii ++ )
            {
                AddEdge(ret);
            }

            RepairMesh(ret);

            return ret;
        }
예제 #9
0
        private void RepairMesh(RoadNetwork network)
        {
            network.SetVisited(false);
            int num_unknown = network.VertexCount;
            LinkedList<Vertex> to_check = new LinkedList<Vertex>();
            to_check.AddLast( network.Start );

            while( num_unknown > 0 )
            {
                Vertex v = null;

                // While there's still unchecked nodes in this group
                while(to_check.Count > 0)
                {
                    v = to_check.First.Value;
                    to_check.RemoveFirst();
                    v.Visited = true;
                    num_unknown --;

                    for( int i = 0; i < v.EdgeCount; i ++ )
                    {
                        Vertex next = v.GetEdge(i).End;
                        if (next.Visited) continue;
                        to_check.AddLast(next);
                    }
                }

                // If there are still unreached verticies, pick the first and connect
                if( num_unknown > 0 )
                {
                    Vertex end = null;

                    // Find the first non-visited vertex
                    for (int i = 0; i < network.VertexCount; i++)
                    {
                        if (!network.GetVertex(i).Visited)
                        {
                            end = network.GetVertex(i);
                            break;
                        }
                    }

                    // Make an edge from this vertex to the last checked
                    network.AddEdge(v, end);
                    to_check.AddLast(end);
                }
            }
        }
예제 #10
0
        private void AddVertex(RoadNetwork network)
        {
            Vertex prev = null;
            Vertex next = null;
            int x = (int)( random.NextDouble() * network.Map.Width );
            int y = (int)( random.NextDouble() * network.Map.Height );

            if (network.VertexCount >= 2)
            {
                // Create new links
                int prev_idx = (int)(random.NextDouble() * (network.VertexCount));
                int next_idx = (int)(random.NextDouble() * (network.VertexCount - 1));
                prev = network.GetVertex(prev_idx);
                // Get the next vertex, and make sure it's not the previous
                if (next_idx == prev_idx)
                    next_idx = (next_idx + 1) % network.VertexCount;
                next = network.GetVertex(next_idx);
            }

            Vertex v = network.AddVertex(new Coordinates(x, y));

            if(network.VertexCount >= 2)
            {
                // Create two edges for it
                network.AddEdge(prev, v);
                network.AddEdge(v, next);
            }
        }
예제 #11
0
        /// <summary>
        /// Add a random edge to the network
        /// </summary>
        /// <param name="network"></param>
        /// <returns>
        /// False if the edge was not created (due to a collision), true if it was
        /// </returns>
        private bool AddEdge(RoadNetwork network)
        {
            // Get two verticies (-1 creates a smaller pool)
            int start_idx = (int)(random.NextDouble() * network.VertexCount);
            int end_idx = (int)(random.NextDouble() * (network.VertexCount - 1));
            // Handle collision
            if(end_idx >= start_idx) end_idx ++;
            Vertex start = network.GetVertex(start_idx);
            Vertex end = network.GetVertex(end_idx);

            // Make sure this isn't making a duplicate (or reverse duplicate)
            for( int i = 0; i < network.EdgeCount; i ++ )
            {
                Edge edge = network.GetEdge(i);
                if( edge.Start == start && edge.End == end )
                    return false;
                if( edge.End == start && edge.Start == end )
                    return false;
            }
            network.AddEdge(start, end);
            return true;
        }
예제 #12
0
        /// <summary>
        /// Randomly alter a RoadNetwork to Produce a slightly different RoadNetwork
        /// </summary>
        /// <param name="source">The RoadNetwork to mutate</param>
        /// <returns>The mutated RoadNetwork</returns>
        public static RoadNetwork Mutate(RoadNetwork source)
        {
            Map map = source.Map;
            int mapWidth = map.Width;
            int mapHeight = map.Height;

            int maxXChange = 1 + mapWidth / 10;
            int maxYChange = 1 + mapHeight / 10;

            source.ClearCopies();
            source.SetVisited(false);
            source.SetBroken(true);

            RoadNetwork destination = new RoadNetwork(map);
            int endVertexIndex = -1;

            LinkedList<Vertex> vertexStack = new LinkedList<Vertex>();
            source.Start.Visited = true;
            vertexStack.AddFirst(source.Start);

            while (vertexStack.Count > 0)
            {
                Vertex vertex = vertexStack.First();

                vertexStack.RemoveFirst();

                List<Vertex> nextVertices = new List<Vertex>();
                for (int i = 0; i < vertex.EdgeCount; i++)
                {
                    Edge edge = vertex.GetEdge(i);
                    Vertex nextVertex = edge.End;

                    if (!nextVertex.Visited)
                    {
                        edge.Broken = false;
                        nextVertex.Visited = true;
                        nextVertices.Add(nextVertex);
                    }
                }

                //If this vertex is a leaf of the tree and not the end vertex it has a 25% chance of
                //begin removed.
                if (nextVertices.Count > 0 || vertex == source.End || random.Next(4) > 0)
                {
                    if (vertex == source.End)
                    {
                        endVertexIndex = destination.VertexCount;
                    }

                    if (vertex == source.Start || vertex == source.End || random.Next(10) > 0)
                    {
                        destination.CopyVertex(vertex);
                    }
                    else
                    {
                        int x = vertex.Coordinates.X + random.Next(2 * maxXChange+1) - maxXChange;
                        int y = vertex.Coordinates.Y + random.Next(2 * maxYChange + 1) - maxYChange;

                        x = Math.Max(0, Math.Min(x, map.Width - 1));
                        y = Math.Max(0, Math.Min(y, map.Height - 1));

                        vertex.Copy = destination.AddVertex(x, y);
                    }
                }

                //Shuffle Vertices
                for (int n = nextVertices.Count - 1; n > 0; n--)
                {
                    int m = random.Next(n + 1);
                    Vertex temp = nextVertices[m];
                    nextVertices[m] = nextVertices[n];
                    nextVertices[n] = temp;
                }

                //Push vertices onto stack in shiffled order.
                foreach (Vertex nextVertex in nextVertices)
                {
                    vertexStack.AddFirst(nextVertex);
                }
            }

            for (int i = 0; i < source.EdgeCount; i++)
            {
                Edge edge = source.GetEdge(i);

                if ((!edge.Broken || random.Next(4) > 0) && edge.Start.Copy != null && edge.End.Copy != null)
                {
                    destination.CopyEdge(edge);
                }
            }

            int verticesToAdd = random.Next(10);

            for (int i = 0; i < verticesToAdd; i++)
            {
                Vertex startVertex = destination.GetVertex(random.Next(destination.VertexCount));
                Vertex endVertex = destination.AddVertex(random.Next(mapWidth), random.Next(mapHeight));
                destination.AddEdge(startVertex, endVertex);
            }

            int edgesToAdd = random.Next(destination.VertexCount/2);

            for (int i = 0; i < edgesToAdd; i++)
            {
                Vertex startVertex = destination.GetVertex(random.Next(destination.VertexCount));
                Vertex endVertex = destination.GetVertex(random.Next(destination.VertexCount));
                if (startVertex != endVertex)
                {
                    destination.AddEdge(startVertex, endVertex);
                }
            }

            if (endVertexIndex == -1)
            {
                throw new Exception("End vertex not found.");
            }
            else
            {
                destination.SetEnd(endVertexIndex);
            }

            return destination;
        }