Exemple #1
0
        /// <summary>
        /// mutation of colors for brush
        /// </summary>
        /// <param name="drawing"></param>
        public void mutatation(MyDnaDrawing drawing)
        {
            if (mutate(RedMutationRate))
            {
                red = getRandomNumber(RedRangeMin, RedRangeMax);
                drawing.setDefect();
            }

            if (mutate(GreenMutationRate))
            {
                green = getRandomNumber(GreenRangeMin, GreenRangeMax);
                drawing.setDefect();
            }

            if (mutate(BlueMutationRate))
            {
                blue = getRandomNumber(BlueRangeMin, BlueRangeMax);
                drawing.setDefect();
            }

            if (mutate(AlphaMutationRate))
            {
                alpha = getRandomNumber(AlphaRangeMin, AlphaRangeMax);
                drawing.setDefect();
            }
        }
Exemple #2
0
        private static MyDnaDrawing GetNewInitializedDrawing()
        {
            var drawing = new MyDnaDrawing();

            drawing.init();
            return(drawing);
        }
Exemple #3
0
        //clonning the whole drawing polygon
        public MyDnaDrawing reproduction()
        {
            var drawing = new MyDnaDrawing();

            drawing.Polygons = new List <MyDnaPolygon>();
            foreach (MyDnaPolygon polygon in Polygons)
            {
                drawing.Polygons.Add(polygon.reproduction());
            }

            return(drawing);
        }
Exemple #4
0
 //if generated chromosome of our population is not fit to us we set dirty
 //working with polygon we can expand it make another shape
 private void removePoint(MyDnaDrawing drawing)
 {
     if (Points.Count > PointsPerPolygonMin)
     {
         if (drawing.pointCount > PointsMin)
         {
             int index = getRandomNumber(0, Points.Count);
             Points.RemoveAt(index);
             drawing.setDefect();
         }
     }
 }
Exemple #5
0
        /*
         * private void CreateSaveBitmap(Canvas canvas, string filename)
         * {
         *  RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
         *   (int)canvas.Width, (int)canvas.Height,
         *   96d, 96d, PixelFormats.Pbgra32);
         *  // needed otherwise the image output is black
         *  canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
         *  canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
         *
         *  renderBitmap.Render(canvas);
         *
         *  JpegBitmapEncoder encoder = new JpegBitmapEncoder();
         *
         *  encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
         *
         *  using (FileStream file = File.Create(filename))
         *  {
         *      encoder.Save(file);
         *  }
         * }*/
        private void tmrRedraw_Tick(object sender, EventArgs e)
        {
            if (currentDrawing == null)
            {
                return;
            }

            int    polygons = currentDrawing.Polygons.Count;
            int    points   = currentDrawing.pointCount;
            double avg      = 0;

            if (polygons != 0)
            {
                avg = points / polygons;
            }

            toolStripStatusLabelFitness.Text    = errorLevel.ToString();
            toolStripStatusLabelGeneration.Text = generation.ToString();
            toolStripStatusLabelSelected.Text   = selected.ToString();
            toolStripStatusLabelPolygons.Text   = polygons.ToString();

            bool shouldRepaint = false;

            if (repaintIntervall.Ticks > 0)
            {
                if (lastRepaint < DateTime.Now - repaintIntervall)
                {
                    shouldRepaint = true;
                }
            }

            if (repaintOnSelectedSteps > 0)
            {
                if (lastSelected + repaintOnSelectedSteps < selected)
                {
                    shouldRepaint = true;
                }
            }

            if (shouldRepaint)
            {
                lock (currentDrawing)
                {
                    guiDrawing = currentDrawing.reproduction();
                }
                genFrame.Invalidate();
                lastRepaint  = DateTime.Now;
                lastSelected = selected;
            }
        }
Exemple #6
0
        //mutation function for polygons
        public void mutation(MyDnaDrawing drawing)
        {
            if (mutate(AddPointMutationRate))
            {
                addPoint(drawing);
            }

            if (mutate(RemovePointMutationRate))
            {
                removePoint(drawing);
            }

            Brush.mutatation(drawing);
            Points.ForEach(p => p.mutation(drawing));
        }
Exemple #7
0
        //main muation function for evolving our chromosomes for points
        public void mutation(MyDnaDrawing drawing)
        {
            if (mutate(MovePointMaxMutationRate))
            {
                x = getRandomNumber(0, MaxWidth);
                y = getRandomNumber(0, MaxHeight);
                drawing.setDefect();
            }

            if (mutate(MovePointMidMutationRate))
            {
                x =
                    Math.Min(
                        Math.Max(0,
                                 x +
                                 getRandomNumber(-MovePointRangeMid,
                                                 MovePointRangeMid)), MaxWidth);
                y =
                    Math.Min(
                        Math.Max(0,
                                 y +
                                 getRandomNumber(-MovePointRangeMid,
                                                 MovePointRangeMid)), MaxHeight);
                drawing.setDefect();
            }

            if (mutate(MovePointMinMutationRate))
            {
                x =
                    Math.Min(
                        Math.Max(0,
                                 x +
                                 getRandomNumber(-MovePointRangeMin,
                                                 MovePointRangeMin)), MaxWidth);
                y =
                    Math.Min(
                        Math.Max(0,
                                 y +
                                 getRandomNumber(-MovePointRangeMin,
                                                 MovePointRangeMin)), MaxHeight);
                drawing.setDefect();
            }
        }
Exemple #8
0
        //adding points in polygon
        //modify polygon with adding points (make from 3th(min) -> 5th edges )
        private void addPoint(MyDnaDrawing drawing)
        {
            if (Points.Count < PointsPerPolygonMax)
            {
                if (drawing.pointCount < PointsMax)
                {
                    var newPoint = new MyDnaPoint();

                    int index = getRandomNumber(1, Points.Count - 1);

                    MyDnaPoint prev = Points[index - 1];
                    MyDnaPoint next = Points[index];

                    newPoint.x = (prev.x + next.x) / 2;
                    newPoint.y = (prev.y + next.y) / 2;

                    Points.Insert(index, newPoint);
                    drawing.setDefect();
                }
            }
        }
Exemple #9
0
        private void StartEvolution()
        {
            splitSourceImage();
            if (currentDrawing == null)
            {
                currentDrawing = GetNewInitializedDrawing();
            }
            lastSelected = 0;

            int i = 0;

            while (isRunning)
            {
                MyDnaDrawing newDrawing;
                lock (currentDrawing)
                {
                    newDrawing = currentDrawing.reproduction();
                }
                newDrawing.mutation();

                if (newDrawing.isDefected)
                {
                    generation++;

                    double newErrorLevel = FitnessFunction.getDrawingFitness(newDrawing, sourceColors);

                    if (newErrorLevel <= errorLevel)
                    {
                        selected++;
                        lock (currentDrawing)
                        {
                            currentDrawing = newDrawing;
                        }
                        errorLevel = newErrorLevel;
                    }
                }
            }
        }