Exemplo n.º 1
0
        protected virtual void Crossover()
        {
            var nextGenerationSize = Random.Next(MinPopulationSize, MaxPopulationSize);

            var noOfRequiredChildren = nextGenerationSize - Parents.Count;

            while (noOfRequiredChildren > 0 && Parents.Count > 1)
            {
                var parent = Parents.ElementAt(Random.Next(Parents.Count));

                Parents.Remove(parent);
                Population.AddLast(parent);

                var mates = Parents.Where(chromosome => chromosome.CanBreed(parent)).ToList();

                if (mates.Count <= 0)
                {
                    continue;
                }

                var partner = mates.ElementAt(Random.Next(mates.Count));

                Parents.Remove(partner);
                Population.AddLast(partner);

                var children = parent.Breed(partner).ToList();

                foreach (var child in children)
                {
                    Population.AddLast(child);

                    noOfRequiredChildren--;
                }
            }

            while (noOfRequiredChildren-- > 0)
            {
                var chromosome = CreateChromosome();

                Population.AddLast(chromosome);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Gets the nth parent of this memento. Indices start at 0,
 ///     e.g. <code>m.GetNthParent(0) == m.Parent</code>.
 /// </summary>
 /// <param name="n"></param>
 /// <returns></returns>
 public GameMemento GetNthParent(int n)
 {
     return(Parents.ElementAt(n));
 }