-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
SelectionBase.cs
56 lines (49 loc) · 2.16 KB
/
SelectionBase.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// A base class for selection.
/// </summary>
public abstract class SelectionBase : ISelection
{
readonly int m_minNumberChromosomes;
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.SelectionBase"/> class.
/// </summary>
/// <param name="minNumberChromosomes">Minimum number chromosomes support to be selected.</param>
protected SelectionBase(int minNumberChromosomes)
{
m_minNumberChromosomes = minNumberChromosomes;
}
/// <summary>
/// Selects the number of chromosomes from the generation specified.
/// </summary>
/// <returns>The selected chromosomes.</returns>
/// <param name="number">The number of chromosomes to select.</param>
/// <param name="generation">The generation where the selection will be made.</param>
public IList<IChromosome> SelectChromosomes(int number, Generation generation)
{
if (number < m_minNumberChromosomes)
{
throw new ArgumentOutOfRangeException(nameof(number), "The number of selected chromosomes should be at least {0}.".With(m_minNumberChromosomes));
}
ExceptionHelper.ThrowIfNull("generation", generation);
if (generation.Chromosomes.Any(c => !c.Fitness.HasValue))
{
throw new SelectionException(
this,
"There are chromosomes with null fitness.");
}
return PerformSelectChromosomes(number, generation);
}
/// <summary>
/// Performs the selection of chromosomes from the generation specified.
/// </summary>
/// <returns>The selected chromosomes.</returns>
/// <param name="number">The number of chromosomes to select.</param>
/// <param name="generation">The generation where the selection will be made.</param>
protected abstract IList<IChromosome> PerformSelectChromosomes(int number, Generation generation);
}
}