forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElitistReinsertion.cs
52 lines (47 loc) · 1.8 KB
/
ElitistReinsertion.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// Elitist reinsertion.
/// <remarks>
/// When there are less offspring than parents, select the best parents to be reinserted together with the offspring.
/// <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("Elitist")]
public class ElitistReinsertion : ReinsertionBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.ElitistReinsertion"/> class.
/// </summary>
public ElitistReinsertion() : 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)
{
var diff = population.MinSize - offspring.Count;
if (diff > 0)
{
var bestParents = parents.OrderByDescending(p => p.Fitness).Take(diff).ToList();
for (int i = 0; i < bestParents.Count; i++)
{
offspring.Add(bestParents[i]);
}
}
return offspring;
}
#endregion
}
}