forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeParentCrossover.cs
64 lines (59 loc) · 2.24 KB
/
ThreeParentCrossover.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
using System.Collections.Generic;
using System.ComponentModel;
using GeneticSharp.Domain.Chromosomes;
namespace GeneticSharp.Domain.Crossovers
{
/// <summary>
/// Three Parent Crossover.
/// <remarks>
/// In this technique, the child is derived from three parents.
/// They are randomly chosen. Each bit of first parent is checked with bit of second parent whether they are same.
/// If same then the bit is taken for the offspring otherwise the bit from the third parent is taken for the offspring.
/// <see href="http://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)#Three_parent_crossover">Wikipedia</see>
/// </remarks>
/// </summary>
[DisplayName("Three Parent")]
public class ThreeParentCrossover : CrossoverBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ThreeParentCrossover"/> class.
/// </summary>
public ThreeParentCrossover()
: base(3, 1)
{
}
#endregion
#region Methods
/// <summary>
/// Performs the cross with specified parents generating the children.
/// </summary>
/// <param name="parents">The parents chromosomes.</param>
/// <returns>
/// The offspring (children) of the parents.
/// </returns>
protected override IList<IChromosome> PerformCross(IList<IChromosome> parents)
{
var parent1 = parents[0];
var parent1Genes = parent1.GetGenes();
var parent2Genes = parents[1].GetGenes();
var parent3Genes = parents[2].GetGenes();
var offspring = parent1.CreateNew();
Gene parent1Gene;
for (int i = 0; i < parent1.Length; i++)
{
parent1Gene = parent1Genes[i];
if (parent1Gene == parent2Genes[i])
{
offspring.ReplaceGene(i, parent1Gene);
}
else
{
offspring.ReplaceGene(i, parent3Genes[i]);
}
}
return new List<IChromosome>() { offspring };
}
#endregion
}
}