public void CrossPopulation(GeneticPopulation population)
        {
            var count = population.Samples.Count;
            var restCount = Convert.ToInt32(0.01 * count * _settings.Elitism);
            if (restCount < 1)
                restCount = 1;
            var removeCount = count - restCount;

            var minDeviation = Convert.ToInt32(0.01*byte.MaxValue*_settings.MutabilityMin);
            var maxDeviation = Convert.ToInt32(0.01*byte.MaxValue*_settings.MutabilityMax)+1;

            population.Samples.RemoveRange(restCount, removeCount);
            for (var i = restCount; i < count; i++)
            {
                var parent1Index = _random.Next(0, restCount);
                var parent2Index = _random.Next(0, restCount);
                var parent1Percentage = Convert.ToByte(_random.Next(0, 101));

                var childColor = population.Samples[parent1Index].Color.Blend(
                    population.Samples[parent2Index].Color, parent1Percentage);

                if (_random.Next(0, 101) < _settings.Mutation)
                {
                    childColor.Red += Convert.ToByte(_random.Next(minDeviation, maxDeviation));
                    childColor.Green += Convert.ToByte(_random.Next(minDeviation, maxDeviation));
                    childColor.Blue += Convert.ToByte(_random.Next(minDeviation, maxDeviation));
                }

                var child = new GeneticSample
                {
                    Color = childColor,
                    DifferenceUnits = 0,
                    DifferencePercents = 0,
                    SimilarityPercents = 0,
                    SimilarityUnits = 0,
                    Age = 0
                };
                population.Samples.Add(child);
            }

            for(var i=0; i<restCount;i++)
            {
                population.Samples[i].Age++;
                if (population.Samples[i].Age>=_settings.Age)
                {
                    population.Samples[i] = GeneticSample.GetRandomGeneticSample(_random);
                }
            }
        }
Ejemplo n.º 2
0
 public static GeneticSample GetRandomGeneticSample(Random random)
 {
     var sample = new GeneticSample
     {
         DifferencePercents = 0,
         DifferenceUnits = 0,
         SimilarityPercents = 0,
         SimilarityUnits = 0,
         Color = new GeneticColor
         {
             Red = (byte)random.Next(byte.MinValue, byte.MaxValue + 1),
             Green = (byte)random.Next(byte.MinValue, byte.MaxValue + 1),
             Blue = (byte)random.Next(byte.MinValue, byte.MaxValue + 1)
         },
         Age = 0
     };
     return sample;
 }