示例#1
0
        private void SetBestChromosome(DrawingChromosome c)
        {
            var bitmap = GenBitmap(c);

            using (var memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption  = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                BestChromosome = bitmapImage;
            }
        }
示例#2
0
        private void StartGa()
        {
            var selection  = new EliteSelection();
            var crossover  = new OnePointCrossover();
            var mutation   = new DrawingMutation();
            var fitness    = new LineDrawingTarget(InputFilename);
            var chromosome = new DrawingChromosome(NumberOfLines);
            var population = new Population(50, 60, chromosome);

            _ga                     = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            _ga.Termination         = new GenerationNumberTermination(NumberOfGenerations);
            _ga.MutationProbability = MutationProbability;

            _ga.GenerationRan      += OnGenerationComplete;
            _ga.TerminationReached += OnTerminationReached;

            _gaThread = new Thread(() => _ga.Start());
            _gaThread.Start();
        }
示例#3
0
        private static Bitmap GenBitmap(DrawingChromosome c)
        {
            // REVISIT: This is a crappy way of doing this.
            var bitmap   = new Bitmap(256, 256);
            var graphics = Graphics.FromImage(bitmap);

            graphics.FillRectangle(Brushes.White, 0, 0, 255, 255);
            var ph = c.GetPhenotype();

            for (var y = 0; y < 256; ++y)
            {
                for (var x = 0; x < 256; ++x)
                {
                    if (ph[x, y] < 128)
                    {
                        graphics.DrawLine(Pens.Black, x, y, x + 1, y + 1);
                    }
                }
            }
            return(bitmap);
        }