コード例 #1
0
ファイル: Form1.cs プロジェクト: perfettiful/evolisaclone
        void timer1_Tick(object sender, EventArgs e)
        {
            if (CommandChannel.GetCommand() == 2)
            {
                timer1.Stop();
                return;
            }
            Gene gene = GeneContainer.Instance.GetGene();

            if (gene != null)
            {
                Graphics graphics = drawingImagePanel.CreateGraphics();
                graphics.Clear(Color.Black);
                Bitmap   bmp             = new Bitmap(originalImage.Width, originalImage.Height);
                Graphics drawingGraphics = Graphics.FromImage(bmp);
                graphics.Clear(Color.Black);
                drawingGraphics.Clear(Color.Black);
                for (int ip = 0; ip < gene.Polygons.Count; ip++)
                {
                    if (gene.Polygons[ip] != null && gene.Brushes[ip] != null)
                    {
                        drawingGraphics.DrawPolygon(new Pen(gene.Brushes[ip]), gene.Polygons[ip]);
                        drawingGraphics.FillPolygon(gene.Brushes[ip], gene.Polygons[ip]);
                        graphics.DrawImage(bmp, new Point(0, 0));
                    }
                    Thread.Sleep(100);
                }

                if (oldNegativeFitness != gene.NegativeFitness)
                {
                    Console.WriteLine("[MainThread],NF: {0}", gene.NegativeFitness);
                    oldNegativeFitness = gene.NegativeFitness;
                    ((Bitmap)bmp.Clone()).Save("_" + gene.Polygons.Count.ToString() + ".bmp");
                    fileNameCounter++;
                    if (fileNameCounter % 10 == 0)
                    {
                        intFileNameCounter++;
                        ((Bitmap)bmp.Clone()).Save(intFileNameCounter.ToString() + ".bmp");
                    }
                }

                graphics.Clear(Color.Black);
                Bitmap demoBmp = (Bitmap)bmp.Clone();
                workingImagePanel.BackColor       = Color.Black;
                workingImagePanel.BackgroundImage = demoBmp;

                label1.Text = "Error: " + gene.NegativeFitness.ToString() + "   " + gene.Polygons.Count + " polygons used";
            }
        }
コード例 #2
0
        public void Run(object obj_iterations)
        {
            int nrOfIterations = (int)obj_iterations;

            double referenceNegativeIndex  = double.MaxValue;
            double referenceComeback       = NrOfPolygons;
            bool   nrOfMutationsTryChanged = false;

            int    limit = 0;
            int    shakeTheImageCounter   = 0;
            double currentNegativeFitness = workingGene.NegativeFitness;

            for (int i = 0; i < nrOfIterations; i++)
            {
                if (CommandChannel.GetCommand() == 1)
                {
                    break;
                }
                List <int> mutationsList = retrieveMutationsList();
                for (int mi = 0; mi < mutationsList.Count; mi++)
                {
                    int mutationStep = mutationsList[mi];
                    switch (mutationStep)
                    {
                    case 1: performAddPolygon(workingGene); break;

                    case 2: performMutateColor(workingGene); break;

                    case 3: performMutatePolygon(workingGene); break;

                    case 4: performMutatePolygonAndColor(workingGene); break;

                    case 5: performMutateColorParam(workingGene); break;

                    case 6: performMutatePolygonPoint(workingGene); break;
                    }
                }


                #region check if the gene had evolved in the last 250 iterations; comment this region if you don't one to have momentum in your app
                if (currentNegativeFitness != workingGene.NegativeFitness)
                {
                    currentNegativeFitness = workingGene.NegativeFitness;
                    shakeTheImageCounter   = 0;
                }
                else
                {
                    //that means the in the last 250 iterations nothing changed in the gene fitness.
                    if (shakeTheImageCounter > 250)
                    {
                        int nrOfPolysToBeRemoved = workingGene.NrOfPolygons / 10;
                        for (int ix = 0; ix < nrOfPolysToBeRemoved; ix++)
                        {
                            randomlyRemovePolygons(workingGene);
                        }
                        referenceComeback = workingGene.Polygons.Count + 1;
                    }
                    shakeTheImageCounter++;
                }
                #endregion

                //if gene is more fit, we added to the container;
                Console.WriteLine("Negative fitness: {0} ,     {1}", workingGene.NegativeFitness, workingGene.Polygons.Count);
                if (workingGene.NegativeFitness < referenceNegativeIndex)
                {
                    GeneContainer.Instance.AddGene(workingGene.Clone());
                    referenceNegativeIndex = workingGene.NegativeFitness;
                }

                #region every 100 iterations we increase the number of polygons, till we reach 50
                if (workingGene.Polygons.Count < 50 && this.NrOfPolygons < 50)
                {
                    if (i % 100 == 0 && i != 0)
                    {
                        this.NrOfPolygons++;
                        workingGene.NrOfPolygons++;
                    }
                }
                #endregion

                //if every time the number of polygons in the gene reaches the max number of polygons, then we increase the nr of polygons and
                //we remove the less fit individuals
                if (workingGene.Polygons.Count < 50)
                {
                    if (workingGene.Polygons.Count == referenceComeback)
                    {
                        limit = workingGene.Polygons.Count / 10;
                        for (int ix = 0; ix < limit; ix++)
                        {
                            removeLessFitPolys(workingGene);
                        }
                        Console.WriteLine("-------- [ReferenceComeback] :" + referenceComeback);
                        referenceComeback++;
                    }
                }
                // when 50 poly are reached, we double the nr of mutations tries;
                // and we remove a larger number of less fit individuals;
                else if (workingGene.Polygons.Count >= 50)
                {
                    if (nrOfMutationsTryChanged == false)
                    {
                        NrOfMutationsTries      = NrOfMutationsTries * 2;
                        nrOfMutationsTryChanged = true;
                    }

                    limit = workingGene.Polygons.Count / 8;
                    for (int ix = 0; ix < limit; ix++)
                    {
                        removeLessFitPolys(workingGene);
                    }
                }
            }

            CommandChannel.SetCommand(2); //tell timer to stop
        }