本文整理汇总了C#中Population.Sort方法的典型用法代码示例。如果您正苦于以下问题:C# Population.Sort方法的具体用法?C# Population.Sort怎么用?C# Population.Sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Population
的用法示例。
在下文中一共展示了Population.Sort方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoSolve
/// <summary>
/// Returns a solution found using best-placement.
/// </summary>
/// <returns></returns>
protected override IRoute DoSolve(OsmSharp.Tools.Math.TSP.Problems.IProblem problem)
{
// create the settings.
SolverSettings settings = new SolverSettings(
-1,
-1,
1000000000,
-1,
-1,
-1);
Solver<List<int>, GeneticProblem, Fitness> solver =
new Solver<List<int>, GeneticProblem, Fitness>(
new GeneticProblem(problem),
settings,
null,
null,
null,
_generation_operation,
new FitnessCalculator(),
true, false);
Population<List<int>, GeneticProblem, Fitness> population =
new Population<List<int>, GeneticProblem, Fitness>(true);
while (population.Count < _population_size)
{
// generate new.
Individual<List<int>, GeneticProblem, Fitness> new_individual =
_generation_operation.Generate(solver);
// add to population.
population.Add(new_individual);
}
// select each individual once.
Population<List<int>, GeneticProblem, Fitness> new_population =
new Population<List<int>, GeneticProblem, Fitness>(true);
Individual<List<int>, GeneticProblem, Fitness> best = null;
int stagnation = 0;
while (stagnation < _stagnation)
{
while (new_population.Count < _population_size)
{
// select an individual and the next one.
int idx = OsmSharp.Tools.Math.Random.StaticRandomGenerator.Get().Generate(population.Count);
Individual<List<int>, GeneticProblem, Fitness> individual1 = population[idx];
Individual<List<int>, GeneticProblem, Fitness> individual2 = null;
if (idx == population.Count - 1)
{
individual2 = population[0];
}
else
{
individual2 = population[idx + 1];
}
population.RemoveAt(idx);
Individual<List<int>, GeneticProblem, Fitness> new_individual = _cross_over_operation.CrossOver(solver,
individual1, individual2);
new_individual.CalculateFitness(solver.Problem, solver.FitnessCalculator);
if (new_individual.Fitness.CompareTo(
individual1.Fitness) < 0)
{
new_population.Add(new_individual);
}
else
{
new_population.Add(individual1);
}
}
population = new_population;
population.Sort(solver, solver.FitnessCalculator);
new_population = new Population<List<int>, GeneticProblem, Fitness>(true);
if (best == null ||
best.Fitness.CompareTo(population[0].Fitness) > 0)
{
stagnation = 0;
best = population[0];
}
else
{
stagnation++;
}
//// report progress.
//OsmSharp.Tools.Output.OutputStreamHost.ReportProgress(stagnation,_stagnation,
// "OsmSharp.Tools.Math.TSP.EdgeAssemblyGenetic.EdgeAssemblyCrossOverSolver",
// "Solving using EAX...");
}
List<int> result = new List<int>(best.Genomes);
result.Insert(0, 0);
//.........这里部分代码省略.........
示例2: Run
// MAIN FUNCTION
public static bool Run( RandomGeneSig getRandomGeneFunc, FitnessSig getPopFitnessFunc )
{
// Runs the EA algorythm, returns true if goal is achieved, false if time is exceeded
// local variables
bool goalAchieved;
double bestFitness, avrFitness;
double[] popFitness = new double[G.popSize];
double[][] genes;
// Create a random population of creatures
myPop = new Population( G.popSize, G.geneLen, getRandomGeneFunc );
// MAIN LOOP
while( true ) {
// Evaluate and sort population
genes = myPop.GetGenes();
myPop.GetFitness( ref popFitness );
goalAchieved = getPopFitnessFunc( genes, ref popFitness, out bestFitness, out avrFitness );
myPop.SetFitness( popFitness, bestFitness, avrFitness );
myPop.Sort();
//myPop.Print( "EVALUATED", false );
//Console.ReadKey( true );
// Are we done?
if( goalAchieved ) break;
// Mate Selection
myPop.SelectToMate( showStepsFlag: true );
myPop.MatePairing();
// Create New Creatures
myPop.CreateOffspring();
myPop.Mutate();
// Death Selection
myPop.SelectToDie( showStepsFlag: false );
// Pause or continue
PausePrintContinue();
// Update population (genes)
myPop.InsertNewOffspring();
myPop.IncGeneration();
}
// success
myPop.Print( "(SUCCESS)", false );
FFNN answer = new FFNN( G.netDef, myPop.creatures[0].Gene);
Console.WriteLine( answer.ToString() );
return goalAchieved;
}
示例3: CalculateMedian
public void CalculateMedian(Population population)
{
population.Sort();
int middleIndex = population.Size/2;
population.MedianFitness = population[middleIndex].Fitness;
}