Beispiel #1
0
    public BaseCell(BaseCell previousCell, Direction direction, MazeGenerator mazeGenerator)
    {
        m_center = previousCell.Center + MazeUtils.DirectionToVector2(direction);

        m_root      = GameObject.Instantiate(Resources.Load(PREFAB_NAME) as GameObject);
        m_root.name = string.Format("Cell {0},{1}", m_center.x, m_center.y);
        m_root.transform.position = new Vector3(m_center.x, m_center.y, -0.1f);

        m_maze = mazeGenerator.maze;
        m_maze.SetCell((int)m_center.x, (int)m_center.y, this);

        SetupWalls(direction);
        SetupNeighbours();
    }
Beispiel #2
0
    private void SetupWalls(Direction initialDirection)
    {
        m_walls = new Dictionary <Direction, GameObject> ();
        for (int cnt = 1; cnt < Enum.GetNames(typeof(Direction)).Length; cnt++)
        {
            GameObject wall = m_root.FindChild("Wall" + ((Direction)cnt).ToString());
            m_walls.Add((Direction)cnt, wall);

            if (initialDirection != Direction.None)
            {
                if ((Direction)cnt == MazeUtils.GetOppositeDirection(initialDirection))
                {
                    DisableWalls((Direction)cnt);
                }
            }
        }
    }
Beispiel #3
0
    private void UpdateNeighbours()
    {
        for (int cnt = 1; cnt < Enum.GetNames(typeof(Direction)).Length; cnt++)
        {
            Vector2 direction = m_center + MazeUtils.DirectionToVector2((Direction)cnt);
            if (direction.x < 0 || direction.x == m_maze.Width ||
                direction.y < 0 || direction.y == m_maze.Height)
            {
                continue;
            }

            if (m_maze.HasCell((int)direction.x, (int)direction.y))
            {
                m_neighbours[(Direction)cnt] = NeighbourType.Visited;
            }
        }
    }
Beispiel #4
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Maze_Recursive_Backtracker m = new Maze_Recursive_Backtracker(25, 25);
            MazeUtils.ExportToImage(@"D:\testy.png", m, 20, 2, Color.FromName(cmbColours.SelectedItem.ToString()));
        }

        else
        {
            foreach (PropertyInfo property in typeof(System.Drawing.Color).GetProperties(BindingFlags.Static | BindingFlags.Public))
            {
                if (property.PropertyType == typeof(System.Drawing.Color))
                {
                    cmbColours.Items.Add(property.Name);
                }
            }
        }
    }
        /// <summary>
        ///     Handles the process of generating maze genomes and rendering bitmap files of their structure.
        /// </summary>
        private static void HandleMazeGeneration()
        {
            IRandomSource rand = RandomDefaults.CreateRandomSource();

            // Get the evolved maze height and width
            int mazeHeight         = Int32.Parse(_executionConfiguration[ExecutionParameter.MazeHeight]);
            int mazeWidth          = Int32.Parse(_executionConfiguration[ExecutionParameter.MazeWidth]);
            int mazeQuadrantHeight = Int32.Parse(_executionConfiguration[ExecutionParameter.MazeQuadrantHeight]);
            int mazeQuadrantWidth  = Int32.Parse(_executionConfiguration[ExecutionParameter.MazeQuadrantWidth]);

            // Get the number of interior walls and maze genome output directory
            int    numWaypoints              = Int32.Parse(_executionConfiguration[ExecutionParameter.NumWaypoints]);
            int    numInteriorWalls          = Int32.Parse(_executionConfiguration[ExecutionParameter.NumWalls]);
            string mazeGenomeOutputDirectory = _executionConfiguration[ExecutionParameter.MazeGenomeOutputBaseDirectory];

            // Get the number of sample mazes to generate (or just 1 maze if not specified)
            int numMazes = _executionConfiguration.ContainsKey(ExecutionParameter.NumMazes)
                ? Int32.Parse(_executionConfiguration[ExecutionParameter.NumMazes])
                : 1;

            // Get whether images are being generated for the sample mazes
            bool generateMazeImages = _executionConfiguration.ContainsKey(ExecutionParameter.OutputMazeBitmap) &&
                                      Boolean.Parse(_executionConfiguration[ExecutionParameter.OutputMazeBitmap]);

            // Get whether maze genomes are being serialized to the same file (defaults to false)
            bool isSingleOutputFile = _executionConfiguration.ContainsKey(ExecutionParameter.SingleGenomeOutputFile) &&
                                      Boolean.Parse(_executionConfiguration[ExecutionParameter.SingleGenomeOutputFile]);

            // Create a new maze genome factory
            MazeGenomeFactory mazeGenomeFactory =
                new MazeGenomeFactory(mazeHeight, mazeWidth, mazeQuadrantHeight, mazeQuadrantWidth);

            // Instantiate list to hold generated maze genomes
            // (only really used when we're writing everything out to one file)
            List <MazeGenome> mazeGenomeList = new List <MazeGenome>(numMazes);

            for (int curMazeCnt = 0; curMazeCnt < numMazes; curMazeCnt++)
            {
                MazeGenome mazeGenome;

                // Lay out the base file name
                string fileBaseName =
                    string.Format("GeneratedMazeGenome_{0}_Height_{1}_Width_{2}_Waypoints_{3}_Walls_{4}", mazeHeight,
                                  mazeWidth, numWaypoints, numInteriorWalls, curMazeCnt);

                // With a single output file, the genomes are likely being used for separate experiments, so we
                // reset the innovation IDs and assign the maze a constant identifier
                if (isSingleOutputFile == false)
                {
                    // Reset innovation IDs
                    mazeGenomeFactory.InnovationIdGenerator.Reset();

                    // Create a new genome and pass in the requisite factory
                    mazeGenome = new MazeGenome(mazeGenomeFactory, 0, 0);
                }
                // Otherwise, we leave the innovation ID generator alone and create a new maze genome with
                // an identifier that's incremented by one
                else
                {
                    mazeGenome = new MazeGenome(mazeGenomeFactory, (uint)curMazeCnt, 0);
                }

                // Add the specified number of waypoints (less one because center waypoint is created on maze initialization)
                for (int cnt = 0; cnt < numWaypoints - 1; cnt++)
                {
                    Point2DInt waypoint;

                    // Randomly select an orientation
                    IntersectionOrientation newPointOrientation = mazeGenomeFactory.Rng.NextBool()
                        ? IntersectionOrientation.Horizontal
                        : IntersectionOrientation.Vertical;

                    // Iterate until we get a waypoint that's valid
                    do
                    {
                        waypoint = new Point2DInt(mazeGenomeFactory.Rng.Next(mazeGenome.MazeBoundaryWidth - 1),
                                                  mazeGenomeFactory.Rng.Next(mazeGenome.MazeBoundaryHeight - 1));
                    } while (
                        MazeUtils.IsValidWaypointLocation(mazeGenome, waypoint, UInt32.MaxValue, newPointOrientation) ==
                        false);

                    mazeGenome.PathGeneList.Add(new PathGene(mazeGenomeFactory.InnovationIdGenerator.NextId, waypoint,
                                                             newPointOrientation));
                }

                // Create the specified number of interior walls (i.e. maze genes)
                for (int cnt = 0; cnt < numInteriorWalls; cnt++)
                {
                    // Create new maze gene and add to genome
                    mazeGenome.WallGeneList.Add(new WallGene(mazeGenomeFactory.InnovationIdGenerator.NextId,
                                                             rand.NextDouble(), rand.NextDouble(), rand.NextDouble() < 0.5));
                }

                // Only serialize genomes to separate files if single output file option is turned off
                if (isSingleOutputFile == false)
                {
                    // Serialize the genome to XML
                    using (
                        XmlWriter xmlWriter =
                            XmlWriter.Create(
                                Path.Combine(mazeGenomeOutputDirectory, string.Format("{0}.xml", fileBaseName)),
                                new XmlWriterSettings {
                        Indent = true
                    }))
                    {
                        // Get genome XML
                        MazeGenomeXmlIO.WriteComplete(xmlWriter, mazeGenome);
                    }
                }
                // Otherwise, just add genome to list to be serialized to single file in bulk
                else
                {
                    mazeGenomeList.Add(mazeGenome);
                }

                // Print the maze to a bitmap file if that option has been specified
                if (generateMazeImages)
                {
                    PrintMazeToFile(mazeGenome, string.Format("{0}_Structure.bmp", fileBaseName));
                }
            }

            // If serialize to single output file is turned off, go ahead and write everything out to the file
            if (isSingleOutputFile)
            {
                // Serialize all genomes to XML
                using (
                    XmlWriter xmlWriter =
                        XmlWriter.Create(Path.Combine(mazeGenomeOutputDirectory,
                                                      string.Format("GeneratedMaze_{0}_Genomes_{1}_Height_{2}_Width_{3}_Waypoints_{4}_Walls.xml",
                                                                    numMazes,
                                                                    mazeHeight, mazeWidth, numWaypoints, numInteriorWalls))))
                {
                    MazeGenomeXmlIO.WriteComplete(xmlWriter, mazeGenomeList);
                }
            }
        }