-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
EliteSelection.cs
62 lines (53 loc) · 2.37 KB
/
EliteSelection.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// Selects the chromosomes with the best fitness and a small portion of the best individuals
/// from the last generation is carried over (without any changes) to the next one.
/// </summary>
/// <remarks>
/// <see href="https://en.wikipedia.org/wiki/Selection_(genetic_algorithm)">Wikipedia</see>
/// </remarks>
[DisplayName("Elite")]
public sealed class EliteSelection : SelectionBase
{
readonly int _previousGenerationChromosomesNumber;
List<IChromosome> _previousGenerationChromosomes;
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.EliteSelection"/> class.
/// </summary>
public EliteSelection()
: this(1)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.EliteSelection"/> class.
/// </summary>
/// <param name="previousGenerationChromosomesNumber">The number of best chromosomes of the previous generation to carried over to the next one.</param>
public EliteSelection(int previousGenerationChromosomesNumber)
: base(2)
{
_previousGenerationChromosomesNumber = previousGenerationChromosomesNumber;
}
#region ISelection implementation
/// <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 select chromosomes.</returns>
protected override IList<IChromosome> PerformSelectChromosomes(int number, Generation generation)
{
if(generation.Number == 1)
_previousGenerationChromosomes = new List<IChromosome>();
_previousGenerationChromosomes.AddRange(generation.Chromosomes);
var ordered = _previousGenerationChromosomes.OrderByDescending(c => c.Fitness);
var result = ordered.Take(number).ToList();
_previousGenerationChromosomes = result.Take(_previousGenerationChromosomesNumber).ToList();
return result;
}
#endregion
}
}