コード例 #1
0
        private List <Gene> ReproduceGenes(List <Gene> genePool)
        {
            // The list of new genes
            List <Gene> newGenes = new List <Gene>();

            // List<Task> tasks = new List<Task>();

            // int numberOfChildren = 0;
            // Reproduce the genes
            foreach (Gene gene1 in genePool)
            {
                foreach (Gene gene2 in genePool)
                {
                    if (gene1 != gene2)
                    {
                        // tasks.Add(Task.Run(() => {
                        //     Gene gene = gene1.Crossover(gene2);

                        //     lock(_lock)
                        //     {
                        //         newGenes.Add(gene);
                        //     }
                        // }));
                        Gene gene = gene1.Crossover(gene2);
                        newGenes.Add(gene);
                    }

                    // numberOfChildren++;

                    if (newGenes.Count >= this.numberOfGenes)
                    {
                        // Task.WaitAll(tasks.ToArray());

                        return(newGenes);
                    }
                }
            }

            //throw new Exception("You shouldn't be here!!!");

            return(newGenes);
        }
コード例 #2
0
        public override Gene Crossover(Gene mate)
        {
            LineGene matePaintGene = (LineGene)mate;
            LineGene childGene     = new LineGene(this.Bitmap.Width, this.Bitmap.Height, this.MutationRangeMax, this.numberOfSeps);

            for (int i = 0; i < this.numberOfSeps; i++)
            {
                Stroke stroke = new Stroke();
                stroke.Paint =
                    Utilities.Random.Next(0, this.MutationRangeMax) == this.MutationNumber ?
                    Stroke.RandomPaint() :
                    (Utilities.Random.Next(0, 100) > 50 ? this.Strokes[i].Paint : matePaintGene.Strokes[i].Paint);
                stroke.Points =
                    Utilities.Random.Next(0, this.MutationRangeMax) == this.MutationNumber ?
                    Stroke.RandomPoints(this.Bitmap.Width, this.Bitmap.Height) :
                    (Utilities.Random.Next(0, 100) > 50 ? this.Strokes[i].Points : matePaintGene.Strokes[i].Points);

                childGene.Strokes.Add(stroke);
            }

            return(childGene);
        }
コード例 #3
0
        public void Run(RunConfig runConfig)
        {
            // Create a new run instance
            this.runInstance = Task.Run(() => {
                // Update engine status
                this.UpdateStatus(true, false, "Engine is running", runConfig);

                // Remove the current engine output directory if it exists
                if (Directory.Exists(Utilities.EngineOutputDirectory))
                {
                    Directory.Delete(Utilities.EngineOutputDirectory, true);
                }

                this.population = new Population(runConfig.TargetBitmap, runConfig);

                // Generate population
                switch (runConfig.GeneType)
                {
                case 1:
                    population.GeneratePixelGenePopulation(runConfig);
                    break;

                case 2:
                    population.GenerateLineGenePopulation(runConfig);
                    break;
                }

                var watch = System.Diagnostics.Stopwatch.StartNew();

                for (int i = 1; i <= runConfig.Generations; i++)
                {
                    // Check to see if this run has been cancelled
                    if (this.cancel)
                    {
                        // Update engine status
                        this.UpdateStatus(false, true, "Engine was cancelled", runConfig);
                        this.cancel = false;
                        break;
                    }

                    this.population.EvaluateFitness();
                    Gene topGene = this.population.NaturalSelection();

                    // Save the image of the top gene
                    Utilities.SaveFittestGeneForGeneration(topGene.Bitmap, i);

                    // If this is the last generation, then save the step images for this top gene
                    if (i == runConfig.Generations)
                    {
                        topGene.SaveSteps();
                    }

                    // Update engine status
                    this.status.CurrentGeneration = i;
                }

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                System.Console.WriteLine($"Milliseconds: {elapsedMs}");

                this.population.EvaluateFitness();
                Utilities.SaveBitmap(this.population.BestGene.Bitmap, $"{Utilities.EngineOutputDirectory}/result.png");

                // Update engine status
                this.UpdateStatus(false, true, "Results are available", runConfig);
                this.cancel = false;
            });
        }
コード例 #4
0
ファイル: Gene.cs プロジェクト: TypicalFooBar/genetic-images
 public abstract Gene Crossover(Gene mate);