/// <summary> /// Linear crossover executor. /// </summary> /// <param name="population">Input population.</param> /// <param name="xover">Crossover operator.</param> /// <param name="xoverProbability">Crossver probability</param> /// <param name="parents">Parents list</param> /// <returns>Childten(individuals)</returns> public virtual IList <IIndividual> Cross(IPopulation population, IXover xover, float xoverProbability, IList <IIndividual> parents) { var size = population.Size; var offspring = new List <IIndividual>(size); for (int i = 0; i < size; i += xover.ChildrenNumber) { // selected parents from population var selectedParents = parents.Skip(i).Take(xover.ParentsNumber).ToList(); // If match the probability cross is made, otherwise the offspring is an exact copy of the parents. // Checks if the number of selected parents is equal which the crossover expect, because the in the end of the list we can // have some rest individual if (selectedParents.Count == xover.ParentsNumber && RandomizationRnd.GetDouble() <= xoverProbability) { offspring.AddRange(xover.Cross(selectedParents)); } else { offspring.AddRange(selectedParents); } } return(offspring); }
/// <summary> /// Mutate the specified individual in population. /// </summary> /// <param name="individual">The individual to be mutated.</param> /// <param name="mut_probability">The mutation probability to mutate each individual.</param> public override void Mutate(IIndividual individual, float mutation_probabilty) { if (RandomizationRnd.GetDouble() <= mutation_probabilty) { var indexes = RandomizationRnd.GetUniqueInts(2, 0, individual.Length); SwapGenes(indexes[0], indexes[1], individual); } }
/// <summary> /// Mutate the specified individual. /// </summary> /// <param name="individual">The individual to be mutated.</param> /// <param name="mutation_probabilty">The mutation probability to mutate each individual.</param> public override void Mutate(IIndividual individual, float mutation_probabilty) { for (int index = 0; index < individual.Length; index++) { if (RandomizationRnd.GetDouble() <= mutation_probabilty) { individual.ReplaceGene(index, individual.GenerateGene()); } } }
/// <summary> /// Cross the specified parents generating the children (Uniform cross). /// </summary> /// <param name="firstParent"> First parent</param> /// <param name="secondParent">Second parent</param> /// <returns>The offspring (children) of the parents.</returns> private IList <IIndividual> PerfomCross(IIndividual firstParent, IIndividual secondParent) { var firstChild = firstParent.CreateNew(); var secondChild = firstParent.CreateNew(); // uniform cross by pmixProb for (int i = 0; i < firstParent.Length; i++) { if (RandomizationRnd.GetDouble() < mixProbability) { firstChild.ReplaceGene(i, firstParent.GetGene(i)); secondChild.ReplaceGene(i, secondParent.GetGene(i)); } else { firstChild.ReplaceGene(i, secondParent.GetGene(i)); secondChild.ReplaceGene(i, firstParent.GetGene(i)); } } return(new List <IIndividual> { firstChild, secondChild }); }