-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
PartiallyMappedCrossover.cs
106 lines (90 loc) · 4.65 KB
/
PartiallyMappedCrossover.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// Partially mapped crossover (PMX).
/// <remarks>
/// The partially-mapped crossover operator was suggested by Gold- berg and Lingle (1985).
/// It passes on ordering and value information from the parent tours to the offspring tours.
/// A portion of one parent’s string is mapped onto a portion of the other parent’s string and the remaining information is exchanged.
/// <see href="http://www.dca.fee.unicamp.br/~gomide/courses/EA072/artigos/Genetic_Algorithm_TSPR_eview_Larranaga_1999.pdf">Genetic Algorithms for the Travelling Salesman Problem - A Review of Representations and Operators</see>
/// </remarks>
/// </summary>
[DisplayName("Partially Mapped (PMX)")]
public class PartiallyMappedCrossover : CrossoverBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.PartiallyMappedCrossover"/> class.
/// </summary>
public PartiallyMappedCrossover() : base(2, 2, 3)
{
IsOrdered = true;
}
#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)
{
if (parents.AnyHasRepeatedGene())
{
throw new CrossoverException(this, "The Partially Mapped Crossover (PMX) can be only used with ordered chromosomes. The specified chromosome has repeated genes.");
}
// Choose the thow parents.
var parent1 = parents[0];
var parent2 = parents[1];
// Create, sort and define the cut point indexes.
var cutPointsIndexes = RandomizationProvider.Current.GetUniqueInts(2, 0, parent1.Length);
Array.Sort(cutPointsIndexes);
var firstCutPointIndex = cutPointsIndexes[0];
var secondCutPointIndex = cutPointsIndexes[1];
// Parent1 creates the mapping section.
var parent1Genes = parent1.GetGenes();
var parent1MappingSection = parent1Genes.Skip(firstCutPointIndex).Take((secondCutPointIndex - firstCutPointIndex) + 1).ToArray();
// Parent12 creates the mapping section.
var parent2Genes = parent2.GetGenes();
var parent2MappingSection = parent2Genes.Skip(firstCutPointIndex).Take((secondCutPointIndex - firstCutPointIndex) + 1).ToArray();
// The new offsprings are created and
// their genes ar replaced start in the first cut point index
// and using the genes in the mapping section from parent 1 e 2.
var offspring1 = parent1.CreateNew();
var offspring2 = parent2.CreateNew();
offspring2.ReplaceGenes(firstCutPointIndex, parent1MappingSection);
offspring1.ReplaceGenes(firstCutPointIndex, parent2MappingSection);
var length = parent1.Length;
for (int i = 0; i < length; i++)
{
if (i >= firstCutPointIndex && i <= secondCutPointIndex)
{
continue;
}
var geneForOffspring1 = GetGeneNotInMappingSection(parent1Genes[i], parent2MappingSection, parent1MappingSection);
offspring1.ReplaceGene(i, geneForOffspring1);
var geneForOffspring2 = GetGeneNotInMappingSection(parent2Genes[i], parent1MappingSection, parent2MappingSection);
offspring2.ReplaceGene(i, geneForOffspring2);
}
return new List<IChromosome>() { offspring1, offspring2 };
}
private Gene GetGeneNotInMappingSection(Gene candidateGene, Gene[] mappingSection, Gene[] otherParentMappingSection)
{
var indexOnMappingSection = mappingSection
.Select((item, index) => new { Gene = item, Index = index })
.FirstOrDefault(g => g.Gene.Equals(candidateGene));
if (indexOnMappingSection != null)
{
return GetGeneNotInMappingSection(otherParentMappingSection[indexOnMappingSection.Index], mappingSection, otherParentMappingSection);
}
return candidateGene;
}
#endregion
}
}