/// <summary> /// Resize population to the new specified size. /// </summary> /// /// <param name="newPopulationSize">New size of population.</param> /// <param name="membersSelector">Selection algorithm to use in the case /// if population should get smaller.</param> /// /// <remarks><para>The method does resizing of population. In the case if population /// should grow, it just adds missing number of random members. In the case if /// population should get smaller, the specified selection method is used to /// reduce the population.</para></remarks> /// /// <exception cref="ArgumentException">Too small population's size was specified. The /// exception is thrown in the case if <paramref name="newPopulationSize"/> is smaller than 2.</exception> /// public void Resize(int newPopulationSize, ISelectionMethod membersSelector) { if (newPopulationSize < 2) { throw new ArgumentException("Too small new population's size was specified."); } if (newPopulationSize > size) { // population is growing, so add new rundom members // Note: we use population.Count here instead of "size" because // population may be bigger already after crossover/mutation. So // we just keep those members instead of adding random member. var toAdd = newPopulationSize - population.Count; for (var i = 0; i < toAdd; i++) { // create new chromosome var c = population[0].CreateNew(); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } } else { // do selection membersSelector.ApplySelection(population, newPopulationSize); } size = newPopulationSize; }
/// <summary> /// Do selection. /// </summary> /// /// <remarks>The method applies selection operator to the current population. Using /// specified selection algorithm it selects members to the new generation from current /// generates and adds certain amount of random members, if is required /// (see <see cref="RandomSelectionPortion"/>).</remarks> /// public virtual void Selection() { // amount of random chromosomes in the new population var randomAmount = (int)(randomSelectionPortion * size); // do selection selectionMethod.ApplySelection(population, size - randomAmount); // add random chromosomes if (randomAmount > 0) { var ancestor = population[0]; for (var i = 0; i < randomAmount; i++) { // create new chromosome var c = ancestor.CreateNew(); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } } FindBestChromosome(); }
#pragma warning disable CS0436 // Type conflicts with imported type /// <summary> /// Perform migration between two populations. /// </summary> /// /// <param name="anotherPopulation">Population to do migration with.</param> /// <param name="numberOfMigrants">Number of chromosomes from each population to migrate.</param> /// <param name="migrantsSelector">Selection algorithm used to select chromosomes to migrate.</param> /// /// <remarks><para>The method performs migration between two populations - current and the /// <paramref name="anotherPopulation">specified one</paramref>. During migration /// <paramref name="numberOfMigrants">specified number</paramref> of chromosomes is choosen from /// each population using <paramref name="migrantsSelector">specified selection algorithms</paramref> /// and put into another population replacing worst members there.</para></remarks> /// public void Migrate(Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector) #pragma warning restore CS0436 // Type conflicts with imported type { int currentSize = this.size; int anotherSize = anotherPopulation.Size; // create copy of current population List <IChromosome> currentCopy = new List <IChromosome>( ); for (int i = 0; i < currentSize; i++) { currentCopy.Add(population[i].Clone( )); } // create copy of another population List <IChromosome> anotherCopy = new List <IChromosome>( ); for (int i = 0; i < anotherSize; i++) { anotherCopy.Add(anotherPopulation.population[i].Clone( )); } // apply selection to both populations' copies - select members to migrate migrantsSelector.ApplySelection(currentCopy, numberOfMigrants); migrantsSelector.ApplySelection(anotherCopy, numberOfMigrants); // sort original populations, so the best chromosomes are in the beginning population.Sort( ); anotherPopulation.population.Sort( ); // remove worst chromosomes from both populations to free space for new members population.RemoveRange(currentSize - numberOfMigrants, numberOfMigrants); anotherPopulation.population.RemoveRange(anotherSize - numberOfMigrants, numberOfMigrants); // put migrants to corresponding populations population.AddRange(anotherCopy); anotherPopulation.population.AddRange(currentCopy); // find best chromosomes in each population FindBestChromosome( ); anotherPopulation.FindBestChromosome( ); }
public void pruebaSeleccion() { for (int i = 0; i < 100; i++) { var cromosoma = new CocheCromosoma(); cromosoma.Generate(); cromosoma.Evaluate(fitness); cromosomas.Add(cromosoma); } seleccion = new CocheSeleccion(); Console.WriteLine(this.cromosomas.Count); seleccion.ApplySelection(this.cromosomas, 20); Console.WriteLine(this.cromosomas.Count); }
/// <summary> /// Resize population to the new specified size. /// </summary> /// /// <param name="newPopulationSize">New size of population.</param> /// <param name="membersSelector">Selection algorithm to use in the case /// if population should get smaller.</param> /// /// <remarks><para>The method does resizing of population. In the case if population /// should grow, it just adds missing number of random members. In the case if /// population should get smaller, the specified selection method is used to /// reduce the population.</para></remarks> /// /// <exception cref="ArgumentException">Too small population's size was specified. The /// exception is thrown in the case if <paramref name="newPopulationSize"/> is smaller than 2.</exception> /// public void Resize(int newPopulationSize, ISelectionMethod membersSelector) { if (newPopulationSize < 2) throw new ArgumentException("Too small new population's size was specified."); if (newPopulationSize > size) { // population is growing, so add new rundom members // Note: we use population.Count here instead of "size" because // population may be bigger already after crossover/mutation. So // we just keep those members instead of adding random member. int toAdd = newPopulationSize - population.Count; for (int i = 0; i < toAdd; i++) { // create new chromosome IChromosome c = population[0].CreateNew(); // calculate it's fitness c.Evaluate(fitnessFunction); // add it to population population.Add(c); } } else { // do selection membersSelector.ApplySelection(population, newPopulationSize); } size = newPopulationSize; }
/// <summary> /// Perform migration between two populations. /// </summary> /// /// <param name="anotherPopulation">Population to do migration with.</param> /// <param name="numberOfMigrants">Number of chromosomes from each population to migrate.</param> /// <param name="migrantsSelector">Selection algorithm used to select chromosomes to migrate.</param> /// /// <remarks><para>The method performs migration between two populations - current and the /// <paramref name="anotherPopulation">specified one</paramref>. During migration /// <paramref name="numberOfMigrants">specified number</paramref> of chromosomes is choosen from /// each population using <paramref name="migrantsSelector">specified selection algorithms</paramref> /// and put into another population replacing worst members there.</para></remarks> /// public void Migrate(Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector) { int currentSize = this.size; int anotherSize = anotherPopulation.Size; // create copy of current population List<IChromosome> currentCopy = new List<IChromosome>(); for (int i = 0; i < currentSize; i++) { currentCopy.Add(population[i].Clone()); } // create copy of another population List<IChromosome> anotherCopy = new List<IChromosome>(); for (int i = 0; i < anotherSize; i++) { anotherCopy.Add(anotherPopulation.population[i].Clone()); } // apply selection to both populations' copies - select members to migrate migrantsSelector.ApplySelection(currentCopy, numberOfMigrants); migrantsSelector.ApplySelection(anotherCopy, numberOfMigrants); // sort original populations, so the best chromosomes are in the beginning population.Sort(); anotherPopulation.population.Sort(); // remove worst chromosomes from both populations to free space for new members population.RemoveRange(currentSize - numberOfMigrants, numberOfMigrants); anotherPopulation.population.RemoveRange(anotherSize - numberOfMigrants, numberOfMigrants); // put migrants to corresponding populations population.AddRange(anotherCopy); anotherPopulation.population.AddRange(currentCopy); // find best chromosomes in each population FindBestChromosome(); anotherPopulation.FindBestChromosome(); }