/// <summary> /// Creates the child. /// </summary> /// <param name="leftParent">Left parent.</param> /// <param name="rightParent">Right parent</param> /// <param name="swapPoint">The index of the swap point.</param> /// <returns>The child.</returns> protected virtual IIndividual CreateChild(IIndividual leftParent, IIndividual rightParent, int swapPoint) { var cutGenesCount = swapPoint + 1; var child = leftParent.CreateNew(); child.ReplaceGenes(0, leftParent.GetGenes().Take(cutGenesCount).ToArray()); child.ReplaceGenes(cutGenesCount, rightParent.GetGenes().Skip(cutGenesCount).ToArray()); return(child); }
/// <summary> /// Creates the child. /// </summary> /// <param name="leftParent">Left parent.</param> /// <param name="rightParent">Right parent</param> /// <param name="firstSwapPoint">he index of the swap point one gene.</param> /// <param name="secondSwapPoint">he index of the swap point two gene.</param> /// <returns>The child.</returns> protected IIndividual CreateChild(IIndividual leftParent, IIndividual rightParent, int firstSwapPoint, int secondSwapPoint) { var firstCutGenesCount = firstSwapPoint + 1; var secondCutGenesCount = secondSwapPoint + 1; var child = leftParent.CreateNew(); child.ReplaceGenes(0, leftParent.GetGenes().Take(firstCutGenesCount).ToArray()); child.ReplaceGenes(firstCutGenesCount, rightParent.GetGenes().Skip(firstCutGenesCount).Take(secondCutGenesCount - firstCutGenesCount).ToArray()); child.ReplaceGenes(secondCutGenesCount, leftParent.GetGenes().Skip(secondCutGenesCount).ToArray()); return(child); }
/// <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 (FastRandom.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 }; }