-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
CutAndSpliceCrossover.cs
63 lines (55 loc) · 2.49 KB
/
CutAndSpliceCrossover.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// Cut and Splice crossover.
/// <remarks>
/// Results in a change in length of the children strings. The reason for this difference is that each parent string has a separate choice of crossover point.
/// <see href="http://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)#.22Cut_and_splice.22">Wikipedia</see>
/// </remarks>
/// </summary>
[DisplayName("Cut and Splice")]
public class CutAndSpliceCrossover : CrossoverBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CutAndSpliceCrossover"/> class.
/// </summary>
public CutAndSpliceCrossover()
: base(2, 2)
{
IsOrdered = false;
}
#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 parent2 = parents[1];
// The minium swap point is 1 to safe generate a gene with at least two genes.
var parent1Point = RandomizationProvider.Current.GetInt(1, parent1.Length) + 1;
var parent2Point = RandomizationProvider.Current.GetInt(1, parent2.Length) + 1;
var offspring1 = CreateOffspring(parent1, parent2, parent1Point, parent2Point);
var offspring2 = CreateOffspring(parent2, parent1, parent2Point, parent1Point);
return new List<IChromosome>() { offspring1, offspring2 };
}
private static IChromosome CreateOffspring(IChromosome leftParent, IChromosome rightParent, int leftParentPoint, int rightParentPoint)
{
var offspring = leftParent.CreateNew();
offspring.Resize(leftParentPoint + (rightParent.Length - rightParentPoint));
offspring.ReplaceGenes(0, leftParent.GetGenes().Take(leftParentPoint).ToArray());
offspring.ReplaceGenes(leftParentPoint, rightParent.GetGenes().Skip(rightParentPoint).ToArray());
return offspring;
}
#endregion
}
}