Beispiel #1
0
        //TODO add mutation rates and min/max move values
        public void Mutate(EvoDrawing parent)
        {
            if (Numbers.ProbabilityFulfilled(Numbers.PointMoveMutationProbability))
            {
                X = Math.Min(
                    Math.Max(
                        0, X + Numbers.GetRandom(-Numbers.FirstRange, Numbers.FirstRange)), Numbers.MaxWidth);
                Y = Math.Min(
                    Math.Max
                        (0, Y + Numbers.GetRandom(-Numbers.FirstRange, Numbers.FirstRange)), Numbers.MaxHeight);
                parent.NeedRepaint = true;
            }


            if (Numbers.ProbabilityFulfilled(Numbers.PointMoveMutationProbability))
            {
                X = Math.Min(
                    Math.Max(
                        0, X + Numbers.GetRandom(-Numbers.SecondRange, Numbers.SecondRange)), Numbers.MaxWidth);
                Y = Math.Min(
                    Math.Max
                        (0, Y + Numbers.GetRandom(-Numbers.SecondRange, Numbers.SecondRange)), Numbers.MaxHeight);
                parent.NeedRepaint = true;
            }

            if (Numbers.ProbabilityFulfilled(Numbers.PointMoveMutationProbability))
            {
                X = Numbers.GetRandom(1, Numbers.MaxWidth);
                Y = Numbers.GetRandom(1, Numbers.MaxHeight);
                parent.NeedRepaint = true;
            }
        }
Beispiel #2
0
        //Mutating shape by changing number of points or changing their position
        //TODO change mutation rates to variables
        public void Mutate(EvoDrawing parent)
        {
            foreach (var point in shapePoints)
            {
                point.Mutate(parent);
            }

            //Add point to shape
            if (shapePoints.Count < MaxPoints && Numbers.ProbabilityFulfilled(Numbers.AddPointMutationProbability))
            {
                parent.NeedRepaint = true;

                EvoPoint additionalPoint = new EvoPoint();
                int      i = Numbers.GetRandom(1, shapePoints.Count - 1);
                additionalPoint.X = Numbers.GetAverage(shapePoints[i - 1].X, shapePoints[i].X);
                additionalPoint.Y = Numbers.GetAverage(shapePoints[i - 1].Y, shapePoints[i].Y);
                shapePoints.Insert(i, additionalPoint);
            }
            //Delete point
            if (shapePoints.Count > MinPoints && Numbers.ProbabilityFulfilled(Numbers.RemovePointmutationProbability))
            {
                parent.NeedRepaint = true;
                shapePoints.Remove(
                    shapePoints.ElementAt <EvoPoint>(
                        Numbers.GetRandom(0, shapePoints.Count)));
            }

            color.Mutate(parent);
        }
Beispiel #3
0
 //Mutation of a color
 //TODO add variables of mutation probability
 public void Mutate(EvoDrawing parent)
 {
     //Red mutation
     if (Numbers.ProbabilityFulfilled(Numbers.ColorMutationProbability))
     {
         red = Numbers.GetRandom(0, 255);
         parent.NeedRepaint = true;
     }
     //Green mutation
     if (Numbers.ProbabilityFulfilled(Numbers.ColorMutationProbability))
     {
         green = Numbers.GetRandom(0, 255);
         parent.NeedRepaint = true;
     }
     //Blue mutation
     if (Numbers.ProbabilityFulfilled(Numbers.ColorMutationProbability))
     {
         blue = Numbers.GetRandom(0, 255);
         parent.NeedRepaint = true;
     }
     //Alpha mutation
     if (Numbers.ProbabilityFulfilled(Numbers.ColorMutationProbability))
     {
         alpha = Numbers.GetRandom(Numbers.MinAlfa, Numbers.MaxAlfa);
         parent.NeedRepaint = true;
     }
 }
Beispiel #4
0
        public static float Fitness(EvoDrawing drawing, float[] image)
        {
            float fitness      = 0;
            var   bytesDrawing = BitmapConverter.getFloats(drawing.ToColors());

            for (int i = 0; i < bytesDrawing.Count(); i++)
            {
                fitness += (bytesDrawing[i] - image[i]) * (bytesDrawing[i] - image[i]);
            }
            return(fitness);
        }
Beispiel #5
0
        public static List <EvoDrawing> Crossover(int numberOfChildren, EvoDrawing firstParent, EvoDrawing secondParent)
        {
            var drawings = new List <EvoDrawing>();

            for (int i = 0; i < numberOfChildren; i++)
            {
                if (Numbers.ProbabilityFulfilled(Numbers.CrossoverProbability))
                {
                    drawings.Add(firstParent.Crossover(secondParent));
                }
                else
                {
                    drawings.Add(firstParent.Clone());
                }
            }
            return(drawings);
        }
Beispiel #6
0
        public EvoDrawing Crossover(EvoDrawing mate)
        {
            List <EvoShape> shapeList      = new List <EvoShape>();
            var             mainEnumerator = shapes.GetEnumerator();
            var             mateEnumerator = mate.shapes.GetEnumerator();

            while (mainEnumerator.MoveNext() != false && mateEnumerator.MoveNext() != false)
            {
                if (Numbers.ProbabilityFulfilled(Numbers.UniformCrossoverRatio))
                {
                    shapeList.Add(mainEnumerator.Current.Clone());
                }
                else
                {
                    shapeList.Add(mateEnumerator.Current.Clone());
                }
            }

            return(new EvoDrawing(shapeList, shape, MinShapePoints, MaxShapePoints, float.MaxValue));
        }