forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TworsMutation.cs
48 lines (45 loc) · 1.78 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
47
48
using System.ComponentModel;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Randomizations;
namespace GeneticSharp.Domain.Mutations
{
/// <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
}
}