コード例 #1
0
        public static void StartGA(GASettings settings)
        {
            tg_ga = new TerrainGeneratorGA(settings);
            tg_ga.EvaluateFitness();
            tg_ga.Draw();

            isRunningGA = true;

            GAThread = new Thread(new ThreadStart(Run));
            GAThread.Start();
        }
コード例 #2
0
        public TerrainGeneratorGA(GASettings settings)
        {
            this.settings  = settings;
            totalFitness   = 0;
            highestFitness = 0;

            population = new List <Terrain>(settings.populationSize);
            for (int i = 0; i < settings.populationSize; i++)
            {
                population.Add(new Terrain(settings.mapWidth, settings.mapHeight, true, settings.tile_selection));
            }
        }
コード例 #3
0
        private void Start_button_Click(object sender, EventArgs e)
        {
            if (!Program.isRunningGA)
            {
                GASettings settings = new GASettings();
                settings.populationSize         = (int)population_number.Value;
                settings.mutationRate           = (float)mutation_number.Value;
                settings.individualMutationRate = (float)individual_mutation_number.Value;
                settings.crossoverRate          = (float)crossover_number.Value;

                settings.mapWidth  = (int)mapwidth_number.Value;
                settings.mapHeight = (int)mapheight_number.Value;

                settings.tile_selection    = tile_selection.Checked;
                settings.expanded_mutation = expanded_mutation.Checked;
                settings.blend_crossover   = blend_check.Checked;
                settings.refined_clumping  = refined_clumping_check.Checked;

                settings.ratio_weight    = (float)ratio_number.Value;
                settings.tilePercentages = new float[Program.NumberOfTerrainTypes];
                settings.tilePercentages[(int)TerrainType.Grass]    = (float)grass_track.Value / 100f;
                settings.tilePercentages[(int)TerrainType.Water]    = (float)water_track.Value / 100f;
                settings.tilePercentages[(int)TerrainType.Forest]   = (float)forest_track.Value / 100f;
                settings.tilePercentages[(int)TerrainType.Mountain] = (float)mountain_track.Value / 100f;
                settings.tilePercentages[(int)TerrainType.Sand]     = (float)sand_track.Value / 100f;
                settings.tilePercentages[(int)TerrainType.Ice]      = (float)ice_track.Value / 100f;

                settings.clumping_weight = (float)clumping_number.Value;

                start_button.Text = "STOP";

                Program.StartGA(settings);
            }
            else
            {
                start_button.Text = "START";

                Program.StopGA();
            }
        }
コード例 #4
0
        public float CalcFitness()
        {
            GASettings settings = Program.tg_ga.settings;

            fitness = 0;

            tileAmounts = new float[Program.NumberOfTerrainTypes];

            for (int x = 0; x < tiles.GetLength(0); x++)
            {
                for (int y = 0; y < tiles.GetLength(1); y++)
                {
                    TerrainType thisTile = tiles[x, y];
                    tileAmounts[(int)thisTile] += 1;

                    if (settings.clumping_weight != 0)
                    {
                        for (int x2 = -1; x2 < 2; x2++)
                        {
                            for (int y2 = -1; y2 < 2; y2++)
                            {
                                if ((x2 == x && y2 == y) ||
                                    x + x2 < 0 ||
                                    y + y2 < 0 ||
                                    x + x2 >= tiles.GetLength(0) ||
                                    y + y2 >= tiles.GetLength(1))
                                {
                                    continue;
                                }

                                TerrainType otherTile = tiles[x + x2, y + y2];

                                if (!settings.refined_clumping)
                                {
                                    if (thisTile == otherTile)
                                    {
                                        fitness += settings.clumping_weight;
                                    }
                                }
                                else
                                {
                                    switch (thisTile)
                                    {
                                    case TerrainType.Grass:
                                        if (otherTile == TerrainType.Mountain ||
                                            otherTile == TerrainType.Forest)
                                        {
                                            fitness += settings.clumping_weight;
                                        }
                                        break;

                                    case TerrainType.Water:
                                        if (otherTile == TerrainType.Water)
                                        {
                                            fitness += settings.clumping_weight;
                                        }
                                        break;

                                    case TerrainType.Sand:
                                        if (otherTile == TerrainType.Sand ||
                                            otherTile == TerrainType.Water)
                                        {
                                            fitness += settings.clumping_weight;
                                        }
                                        break;

                                    case TerrainType.Forest:
                                        if (otherTile == TerrainType.Mountain ||
                                            otherTile == TerrainType.Grass)
                                        {
                                            fitness += settings.clumping_weight;
                                        }
                                        break;

                                    case TerrainType.Mountain:
                                        if (otherTile == TerrainType.Grass ||
                                            otherTile == TerrainType.Forest)
                                        {
                                            fitness += settings.clumping_weight;
                                        }
                                        break;

                                    case TerrainType.Ice:
                                        if (otherTile == TerrainType.Ice ||
                                            otherTile == TerrainType.Mountain)
                                        {
                                            fitness += settings.clumping_weight;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < Program.NumberOfTerrainTypes; i++)
            {
                tileAmounts[i] = tileAmounts[i] / tiles.Length;
                fitness       += (1 / (Math.Abs(tileAmounts[i] - settings.tilePercentages[i]) * 100 + 1)) * settings.ratio_weight;
            }

            return(fitness);
        }
コード例 #5
0
 public void Settings(GASettings settings)
 {
     this.settings = settings;
 }