本文整理汇总了C#中Chromosome.Count方法的典型用法代码示例。如果您正苦于以下问题:C# Chromosome.Count方法的具体用法?C# Chromosome.Count怎么用?C# Chromosome.Count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Chromosome
的用法示例。
在下文中一共展示了Chromosome.Count方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BinaryCrossover
public static Population BinaryCrossover(Chromosome x1, Chromosome x2, Random r)
{
var genelist1 = new List<Gene>();
var genelist2 = new List<Gene>();
//for each gene in the chromosomes, we crossover
for (int i = 0; i < x1.Count(); i++)
{
Gene c1, c2;
x1[i].Crossover(x2[i], out c1, out c2);
genelist1.Add(c1);
genelist2.Add(c2);
}
var x1_t = new Chromosome(genelist1);
var x2_t = new Chromosome(genelist2);
var l1 = new List<Chromosome>();
l1.Add(x1_t);
l1.Add(x2_t);
return new Population(l1);
}
示例2: Run
public void Run()
{
InitializePopulation();
double maxFitness = 0;
while (_currentTrial++ < Trials)
{
Console.WriteLine("Starting trial " + _currentTrial + "...");
string filename = QSConstants.DEFAULT_DATA_FILEPATH + "result" + _currentTrial + ".txt";
using (var file = new StreamWriter(@filename))
{
for (int generation = 0; generation < Generations; generation++)
{
Console.WriteLine(generation);
var newPop = new Population();
//elitism
Population elite = _currentPopulation.ElitismSelection(2);
newPop.JoinPopulation(elite);
//populate rest of population with children
while (newPop.PopulationSize < PopulationSize)
{
//selection
Population parents = _currentPopulation.SimpleRouletteSelection(2, _randomSeed);
//crossover
Population children = Crossover.BinaryCrossover(parents[0], parents[1], _randomSeed);
newPop.JoinPopulation(children);
}
newPop.MutatePopulation(MutationRate, _randomSeed);
double average = 0;
//calculate fitness for new population
foreach (Chromosome chromosome in newPop.Chromosomes)
{
if (chromosome.Fitness.Equals(double.NaN))
chromosome.CalculateFitness(_fitnessFunction);
if (chromosome.Fitness > maxFitness)
{
maxFitness = chromosome.Fitness;
maxC = chromosome;
file.WriteLine("Generation: {0}", generation);
file.WriteLine("Fitness: {0}", maxFitness);
for (int i = 0; i < maxC.Count(); i++)
{
file.WriteLine("Gene " + i + ":" + maxC[i]);
}
file.WriteLine();
file.Flush();
}
average += chromosome.Fitness;
//Console.WriteLine(chromosome.Fitness + ": x:" + (double)chromosome.GeneList[0].geneValue + "; y:" + (double)chromosome.GeneList[1].geneValue);
}
//Console.WriteLine(average/newPop.Chromosomes.Count);
//Console.WriteLine("-----------------------------------");
_currentPopulation = newPop;
}
}
maxC.CalculateFitness(_fitnessFunction);
}
Console.WriteLine("Execution complete.");
}