/// <summary> /// Perform one generation. /// </summary> public virtual void Iteration() { int countToMate = (int)(Population.Genomes.Count * PercentToMate); int offspringCount = countToMate * 2; int offspringIndex = Population.Genomes.Count - offspringCount; int matingPopulationSize = (int)(Population.Genomes.Count * MatingPopulation); TaskGroup group = EngineConcurrency.Instance .CreateTaskGroup(); // mate and form the next generation for (int i = 0; i < countToMate; i++) { IGenome mother = Population.Genomes[i]; int fatherInt = (int)(ThreadSafeRandom.NextDouble() * matingPopulationSize); IGenome father = Population.Genomes[fatherInt]; IGenome child1 = Population.Genomes[offspringIndex]; IGenome child2 = Population.Genomes[ offspringIndex + 1]; MateWorker worker = new MateWorker(mother, father, child1, child2); EngineConcurrency.Instance.ProcessTask(worker, group); offspringIndex += 2; } group.WaitForComplete(); // sort the next generation Population.Sort(); }
/// <summary> /// Perform one generation. /// </summary> public virtual void Iteration(GATracker gaTracker) { int countToMate = (int)(Population.Genomes.Count * PercentToMate); int matingPopulationSize = (int)(Population.Genomes.Count * MatingPopulation); //The only individuals we want to preserve is the mating population. //i.e. everything but the mating population gets replaced. int newOffspringCount = Population.Genomes.Count - matingPopulationSize; int newOffspringIndex = Population.Genomes.Count - newOffspringCount; int offspringCount = countToMate * 2; //int offspringIndex = Population.Genomes.Count - offspringCount; //TaskGroup group = EngineConcurrency.Instance.CreateTaskGroup(); // mate and form the next generation //20 < 80 while (newOffspringIndex < newOffspringCount + matingPopulationSize) { for (int i = 0; i < countToMate; i++) { IGenome mother = Population.Genomes[i]; int fatherInt = (int)(ThreadSafeRandom.NextDouble() * matingPopulationSize); IGenome father = Population.Genomes[fatherInt]; IGenome child1 = Population.Genomes[newOffspringIndex]; IGenome child2 = Population.Genomes[newOffspringIndex + 1]; GATracker.MatedElement matedElement = new GATracker.MatedElement(i, fatherInt, newOffspringIndex, newOffspringIndex + 1); MateWorker worker = new MateWorker(mother, father, child1, child2, matedElement); //EngineConcurrency.Instance.ProcessTask(worker, group); worker.Run(); //Add the matedElement to the GATracker gaTracker.AddMatedElement(matedElement); newOffspringIndex += 2; } } //group.WaitForComplete(); // sort the next generation Population.Sort(); }