-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSelectionStrategyUtils.cs
57 lines (51 loc) · 2.04 KB
/
SelectionStrategyUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Linq;
using System.Threading.Tasks;
using GeneticAlgorithm.Exceptions;
using GeneticAlgorithm.Interfaces;
namespace GeneticAlgorithm.SelectionStrategies
{
public static class SelectionStrategyUtils
{
public static double[] GetNormilizeEvaluations(this Population population)
{
population = population.Clone();
var total = population.GetEvaluations().Sum();
Parallel.ForEach(population, chromosome =>
{
chromosome.Evaluation = chromosome.Evaluation / total;
});
return population.GetEvaluations();
}
/// <summary>
/// Returns a population object with only the best n chromosomes
/// </summary>
public static Population GetBestChromosomes(this Population population, int n)
{
if (n == population.Count())
return population;
if (n <= 0 || n > population.Count())
throw new InternalSearchException($"Code 1006 (requested {n} best chromosomes; population size is {population.Count()})");
var min = population.GetEvaluations().OrderByDescending(x => x).Take(n).Last();
var bestChromosomes = new IChromosome[n];
int index = 0;
double[] evaluations = new double[n];
foreach (var chromosome in population)
{
if (chromosome.Evaluation >= min)
{
bestChromosomes[index] = chromosome.Chromosome;
evaluations[index] = chromosome.Evaluation;
index++;
}
if (index >= n)
{
var newPopulation = new Population(bestChromosomes);
for (int i = 0; i < n; i++)
newPopulation[i].Evaluation = evaluations[i];
return newPopulation;
}
}
throw new InternalSearchException("Code 1007 (not enough best chromosomes found)");
}
}
}