-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
TworsMutation.cs
46 lines (43 loc) · 1.68 KB
/
TworsMutation.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
using System.ComponentModel;
namespace GeneticSharp
{
/// <summary>
/// Twors mutation allows the exchange of position of two genes randomly chosen.
/// <remarks>
/// <see href="http://arxiv.org/ftp/arxiv/papers/1203/1203.3099.pdf">Analyzing the Performance of Mutation Operators to Solve the Travelling Salesman Problem</see>
/// </remarks>
/// </summary>
[DisplayName("Twors")]
public class TworsMutation : MutationBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="TworsMutation"/> class.
/// </summary>
public TworsMutation()
{
IsOrdered = true;
}
#endregion
#region Methods
/// <summary>
/// Mutate the specified chromosome.
/// </summary>
/// <param name="chromosome">The chromosome.</param>
/// <param name="probability">The probability to mutate each chromosome.</param>
protected override void PerformMutate(IChromosome chromosome, float probability)
{
if (RandomizationProvider.Current.GetDouble() <= probability)
{
var indexes = RandomizationProvider.Current.GetUniqueInts(2, 0, chromosome.Length);
var firstIndex = indexes[0];
var secondIndex = indexes[1];
var firstGene = chromosome.GetGene(firstIndex);
var secondGene = chromosome.GetGene(secondIndex);
chromosome.ReplaceGene(firstIndex, secondGene);
chromosome.ReplaceGene(secondIndex, firstGene);
}
}
#endregion
}
}