-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
ReinsertionBase.cs
72 lines (64 loc) · 3.05 KB
/
ReinsertionBase.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
using System.Collections.Generic;
namespace GeneticSharp
{
/// <summary>
/// Base class for IReinsertion's implementations.
/// </summary>
public abstract class ReinsertionBase : IReinsertion
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.ReinsertionBase"/> class.
/// </summary>
/// <param name="canCollapse">If set to <c>true</c> can collapse the number of selected chromosomes for reinsertion.</param>
/// <param name="canExpand">If set to <c>true</c> can expand the number of selected chromosomes for reinsertion.</param>
protected ReinsertionBase(bool canCollapse, bool canExpand)
{
CanCollapse = canCollapse;
CanExpand = canExpand;
}
#endregion
#region Properties
/// <summary>
/// Gets a value indicating whether can collapse the number of selected chromosomes for reinsertion.
/// </summary>
public bool CanCollapse { get; private set; }
/// <summary>
/// Gets a value indicating whether can expand the number of selected chromosomes for reinsertion.
/// </summary>
public bool CanExpand { get; private set; }
#endregion
#region Methods
/// <summary>
/// Selects the chromosomes which will be reinserted.
/// </summary>
/// <returns>The chromosomes to be reinserted in next generation..</returns>
/// <param name="population">The population.</param>
/// <param name="offspring">The offspring.</param>
/// <param name="parents">The parents.</param>
public IList<IChromosome> SelectChromosomes(IPopulation population, IList<IChromosome> offspring, IList<IChromosome> parents)
{
ExceptionHelper.ThrowIfNull("population", population);
ExceptionHelper.ThrowIfNull("offspring", offspring);
ExceptionHelper.ThrowIfNull("parents", parents);
if (!CanExpand && offspring.Count < population.MinSize)
{
throw new ReinsertionException(this, "Cannot expand the number of chromosome in population. Try another reinsertion!");
}
if (!CanCollapse && offspring.Count > population.MaxSize)
{
throw new ReinsertionException(this, "Cannot collapse the number of chromosome in population. Try another reinsertion!");
}
return PerformSelectChromosomes(population, offspring, parents);
}
/// <summary>
/// Selects the chromosomes which will be reinserted.
/// </summary>
/// <returns>The chromosomes to be reinserted in next generation..</returns>
/// <param name="population">The population.</param>
/// <param name="offspring">The offspring.</param>
/// <param name="parents">The parents.</param>
protected abstract IList<IChromosome> PerformSelectChromosomes(IPopulation population, IList<IChromosome> offspring, IList<IChromosome> parents);
#endregion
}
}