本文整理汇总了C#中Population.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Population.Add方法的具体用法?C# Population.Add怎么用?C# Population.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Population
的用法示例。
在下文中一共展示了Population.Add方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: testCrossoverPopulation
public bool testCrossoverPopulation()
{
Population parents = new Population();
Chromosome[] chr_tab = { new Chromosome(new bool[] {false, false, false}), new Chromosome(new bool[] { true, true, true })};
Individual[] ind_tab = new Individual[4];
for (int i = 0; i < 4; i++)
{
ind_tab[i] = new Individual(chr_tab[i % 2]);
parents.Add(ind_tab[i]);
}
int[] permutation = new int[] { 0, 2, 1, 3};
int random1 = 2;
int random2 = 0;
Population orphans = this.crossoverHelper(parents, random1, random2, permutation);
for (int i = 0; i < 4; i++)
{
if (i < 2)
{
for (int j = 0; j < orphans.getIndividual(0).chromosome.Count; j++)
{
if (orphans.getIndividual(i).chromosome[j] != false) return false;
}
}
else
{
for (int j = 0; j < orphans.getIndividual(0).chromosome.Count; j++)
{
if (orphans.getIndividual(i).chromosome[j] != true) return false;
}
}
}
return true;
}
示例2: 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);
//.........这里部分代码省略.........
示例3: SolveStep
//.........这里部分代码省略.........
{
var first = this.TournamentSelect();
var second = this.TournamentSelect();
Assert.That(first.DepartureTime != default(DateTime) && second.DepartureTime != default(DateTime));
bool doCrossover = this.random.NextDouble() <= this.Properties.CrossoverRate;
bool doMutation = this.random.NextDouble() <= this.Properties.MutationRate;
Critter[] children = doCrossover
? this.Properties.Breeder.Crossover(first, second)
: new[] { first, second };
if (doMutation)
{
children[0] = this.Properties.Mutator.Mutate(children[0]);
children[1] = this.Properties.Mutator.Mutate(children[1]);
}
Assert.That(
children[0].DepartureTime != default(DateTime) && children[1].DepartureTime != default(DateTime));
if (doCrossover || doMutation)
{
children[0].Fitness = this.Properties.FitnessFunction.GetFitness(
children[0].Route, children[0].DepartureTime);
children[1].Fitness = this.Properties.FitnessFunction.GetFitness(
children[1].Route, children[1].DepartureTime);
}
// var ff = (AlFitnessFunction)this.properties.FitnessFunction;
Assert.That(
children[0].DepartureTime != default(DateTime) && children[1].DepartureTime != default(DateTime));
this.population[1].AddRange(children);
this.progress++;
}
/*
var q = new List<Critter>(this.Properties.PopulationSize);
foreach (var c1 in f)
{
c1.Clear();
}
f.Clear();
for (int i = 0; i < this.Properties.PopulationSize/2; i ++)
{
var first = TournamentSelect();
var second = TournamentSelect();
Assert.That(first.departureTime != default(DateTime) && second.departureTime != default(DateTime));
bool doCrossover = this.random.NextDouble() <= this.Properties.CrossoverRate;
bool doMutation = this.random.NextDouble() <= this.Properties.MutationRate;
Critter[] children = doCrossover
? this.Properties.Breeder.Crossover(first, second)
: new[] { first, second };
if (doMutation)
{
children[0] = this.Properties.Mutator.Mutate(children[0]);
children[1] = this.Properties.Mutator.Mutate(children[1]);
}