forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TournamentSelection.cs
113 lines (102 loc) · 4.74 KB
/
TournamentSelection.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Populations;
using GeneticSharp.Domain.Randomizations;
using GeneticSharp.Infrastructure.Framework.Texts;
namespace GeneticSharp.Domain.Selections
{
/// <summary>
/// Tournament selection involves running several "tournaments" among a few individuals chosen at random from the population.
/// The winner of each tournament (the one with the best fitness) is selected for crossover.
/// <remarks>
/// Selection pressure is easily adjusted by changing the tournament size.
/// If the tournament size is larger, weak individuals have a smaller chance to be selected.
/// </remarks>
/// </summary>
[DisplayName("Tournament")]
public class TournamentSelection : SelectionBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="TournamentSelection"/> class.
/// <remarks>
/// The default Size is 2.
/// The default AllowWinnerCompeteNextTournament is true.
/// </remarks>
/// </summary>
public TournamentSelection() : this(2)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TournamentSelection"/> class.
/// <remarks>
/// The default AllowWinnerCompeteNextTournament is true.
/// </remarks>
/// </summary>
/// <param name="size">The size of the tournament, in other words, the number of chromosomes that will participate of each tournament until all need chromosomes be selected.</param>
public TournamentSelection(int size) : this(size, true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TournamentSelection"/> class.
/// </summary>
/// <param name="size">The size of the tournament, in other words, the number of chromosomes that will participate of each tournament until all need chromosomes be selected.</param>
/// <param name="allowWinnerCompeteNextTournament">If allow any winner in a tournament participate in the next tournament, in other words, if you want to allow a chromosome be selected more the one time.</param>
public TournamentSelection(int size, bool allowWinnerCompeteNextTournament) : base(2)
{
Size = size;
AllowWinnerCompeteNextTournament = allowWinnerCompeteNextTournament;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the size of the tournament.
/// <remarks>
/// In other words, the number of chromosomes that will participate of each tournament until all need chromosomes be selected.
/// </remarks>
/// </summary>
public int Size { get; set; }
/// <summary>
/// Gets or sets a value indicating whether allow any winner in a tournament to participate in the next tournament.
/// <remarks>
/// In other words, if you want to allow a chromosome be selected more the one time.
/// </remarks>
/// </summary>
public bool AllowWinnerCompeteNextTournament { get; set; }
#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)
{
if (Size > generation.Chromosomes.Count)
{
throw new SelectionException(
this,
"The tournament size is greater than available chromosomes. Tournament size is {0} and generation {1} available chromosomes are {2}.".With(Size, generation.Number, generation.Chromosomes.Count));
}
var candidates = generation.Chromosomes.ToList();
var selected = new List<IChromosome>();
while (selected.Count < number)
{
var randomIndexes = RandomizationProvider.Current.GetUniqueInts(Size, 0, candidates.Count);
var tournamentWinner = candidates.Where((c, i) => randomIndexes.Contains(i)).OrderByDescending(c => c.Fitness).First();
selected.Add(tournamentWinner);
if (!AllowWinnerCompeteNextTournament)
{
candidates.Remove(tournamentWinner);
}
}
return selected;
}
#endregion
}
}