forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StochasticUniversalSamplingSelection.cs
78 lines (71 loc) · 3.06 KB
/
StochasticUniversalSamplingSelection.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections.Generic;
using System.ComponentModel;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Populations;
using GeneticSharp.Domain.Randomizations;
namespace GeneticSharp.Domain.Selections
{
/// <summary>
/// Stochastic Universal Sampling.
/// <remarks>
/// Also know as: Roulette wheel selection.
/// Is a kind of Fitness Proportionate Selection.
/// <see href=" http://watchmaker.uncommons.org/manual/ch03s02.html">Fitness-Proportionate Selection</see>
/// <para>
/// Stochastic Universal Sampling is an elaborately-named variation of roulette wheel selection.
/// Stochastic Universal Sampling ensures that the observed selection frequencies of each individual
/// are in line with the expected frequencies. So if we have an individual that occupies 4.5% of the
/// wheel and we select 100 individuals, we would expect on average for that individual to be selected
/// between four and five times. Stochastic Universal Sampling guarantees this. The individual will be
/// selected either four times or five times, not three times, not zero times and not 100 times.
/// Standard roulette wheel selection does not make this guarantee.
/// </para>
/// <see href="http://en.wikipedia.org/wiki/Stochastic_universal_sampling">Wikipedia</see>
/// </remarks>
/// </summary>
[DisplayName("Stochastic Universal Sampling")]
public class StochasticUniversalSamplingSelection : RouletteWheelSelection
{
#region Constructors
/// <summary>
/// Initializes a new instance of the
/// <see cref="GeneticSharp.Domain.Selections.StochasticUniversalSamplingSelection"/> class.
/// </summary>
public StochasticUniversalSamplingSelection()
{
}
#endregion
#region Methods
/// <summary>
/// Performs the selection of chromosomes from the generation specified.
/// </summary>
/// <param name="number">The number of chromosomes to select.</param>
/// <param name="generation">The generation where the selection will be made.</param>
/// <returns>
/// The selected chromosomes.
/// </returns>
protected override IList<IChromosome> PerformSelectChromosomes(int number, Generation generation)
{
var chromosomes = generation.Chromosomes;
var rouleteWheel = new List<double>();
double stepSize = 1.0 / number;
CalculateCumulativePercentFitness(chromosomes, rouleteWheel);
var pointer = RandomizationProvider.Current.GetDouble();
return SelectFromWheel(
number,
chromosomes,
rouleteWheel,
() =>
{
if (pointer > 1.0)
{
pointer -= 1.0;
}
var currentPointer = pointer;
pointer += stepSize;
return currentPointer;
});
}
#endregion
}
}