本文整理汇总了C#中IChromosome.CreateNew方法的典型用法代码示例。如果您正苦于以下问题:C# IChromosome.CreateNew方法的具体用法?C# IChromosome.CreateNew怎么用?C# IChromosome.CreateNew使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IChromosome
的用法示例。
在下文中一共展示了IChromosome.CreateNew方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: QueuePopulation
/// <summary>
/// Initializes a new instance of the <see cref="Population"/> class.
/// </summary>
///
/// <param name="size">Initial size of population.</param>
/// <param name="ancestor">Ancestor chromosome to use for population creatioin.</param>
/// <param name="fitnessFunction">Fitness function to use for calculating
/// chromosome's fitness values.</param>
/// <param name="selectionMethod">Selection algorithm to use for selection
/// chromosome's to new generation.</param>
///
/// <remarks>Creates new population of specified size. The specified ancestor
/// becomes first member of the population and is used to create other members
/// with same parameters, which were used for ancestor's creation.</remarks>
///
/// <exception cref="ArgumentException">Too small population's size was specified. The
/// exception is thrown in the case if <paramref name="size"/> is smaller than 2.</exception>
///
public QueuePopulation(int size,
IChromosome ancestor,
IFitnessFunction fitnessFunction,
ISelectionMethod selectionMethod)
{
if (size < 2)
throw new ArgumentException("Too small population's size was specified.");
this.fitnessFunction = fitnessFunction;
this.selectionMethod = selectionMethod;
this.size = size;
// add ancestor to the population
ancestor.Evaluate(fitnessFunction);
population.Add(ancestor.Clone());
// add more chromosomes to the population
for (int i = 1; i < size; i++)
{
// create new chromosome
IChromosome c = ancestor.CreateNew();
// calculate it's fitness
c.Evaluate(fitnessFunction);
// add it to population
population.Add(c);
}
}
示例2: CreateOffspring
private static IChromosome CreateOffspring(IChromosome leftParent, IChromosome rightParent, int leftParentPoint, int rightParentPoint)
{
var offspring = leftParent.CreateNew();
offspring.Resize(leftParentPoint + (rightParent.Length - rightParentPoint));
offspring.ReplaceGenes(0, leftParent.GetGenes().Take(leftParentPoint).ToArray());
offspring.ReplaceGenes(leftParentPoint, rightParent.GetGenes().Skip(rightParentPoint).ToArray());
return offspring;
}
示例3: CreateChild
/// <summary>
/// Creates the child.
/// </summary>
/// <returns>The child.</returns>
/// <param name="leftParent">Left parent.</param>
/// <param name="rightParent">Right parent.</param>
private IChromosome CreateChild(IChromosome leftParent, IChromosome rightParent)
{
var cutGenesCount = SwapPointIndex + 1;
var child = leftParent.CreateNew();
child.ReplaceGenes(0, leftParent.GetGenes().Take(cutGenesCount).ToArray());
child.ReplaceGenes(cutGenesCount, rightParent.GetGenes().Skip(cutGenesCount).ToArray());
return child;
}
示例4: CreateChild
/// <summary>
/// Creates the child.
/// </summary>
/// <returns>The child.</returns>
/// <param name="leftParent">Left parent.</param>
/// <param name="rightParent">Right parent.</param>
protected override IChromosome CreateChild(IChromosome leftParent, IChromosome rightParent)
{
var firstCutGenesCount = SwapPointOneGeneIndex + 1;
var secondCutGenesCount = SwapPointTwoGeneIndex + 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;
}
示例5: CreateChild
/// <summary>
/// Creates the child.
/// </summary>
/// <returns>The child.</returns>
/// <param name="firstParent">First parent.</param>
/// <param name="secondParent">Second parent.</param>
/// <param name="middleSectionBeginIndex">Middle section begin index.</param>
/// <param name="middleSectionEndIndex">Middle section end index.</param>
private static IChromosome CreateChild(IChromosome firstParent, IChromosome secondParent, int middleSectionBeginIndex, int middleSectionEndIndex)
{
var middleSectionGenes = firstParent.GetGenes().Skip(middleSectionBeginIndex).Take((middleSectionEndIndex - middleSectionBeginIndex) + 1);
var secondParentRemainingGenes = secondParent.GetGenes().Except(middleSectionGenes).GetEnumerator();
var child = firstParent.CreateNew();
for (int i = 0; i < firstParent.Length; i++)
{
var firstParentGene = firstParent.GetGene(i);
if (i >= middleSectionBeginIndex && i <= middleSectionEndIndex)
{
child.ReplaceGene(i, firstParentGene);
}
else
{
secondParentRemainingGenes.MoveNext();
child.ReplaceGene(i, secondParentRemainingGenes.Current);
}
}
return child;
}