-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
UniformReinsertion.cs
51 lines (46 loc) · 1.82 KB
/
UniformReinsertion.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
using System.Collections.Generic;
using System.ComponentModel;
namespace GeneticSharp
{
/// <summary>
/// Uniform Reinsertion.
/// <remarks>
/// When there are less offspring than parents, select the offspring uniformly at random to be reinserted, the parents are discarded.
/// <see href="http://usb-bg.org/Bg/Annual_Informatics/2011/SUB-Informatics-2011-4-29-35.pdf">Generalized Nets Model of offspring Reinsertion in Genetic Algorithm</see>
/// </remarks>
/// </summary>
[DisplayName("Uniform")]
public class UniformReinsertion : ReinsertionBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.UniformReinsertion"/> class.
/// </summary>
public UniformReinsertion() : base(false, true)
{
}
#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>
protected override IList<IChromosome> PerformSelectChromosomes(IPopulation population, IList<IChromosome> offspring, IList<IChromosome> parents)
{
if (offspring.Count == 0)
{
throw new ReinsertionException(this, "The minimum size of the offspring is 1.");
}
var rnd = RandomizationProvider.Current;
while (offspring.Count < population.MinSize)
{
offspring.Add(offspring[rnd.GetInt(0, offspring.Count)]);
}
return offspring;
}
#endregion
}
}