forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseSequenceMutation.cs
57 lines (52 loc) · 2.25 KB
/
ReverseSequenceMutation.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
using System.ComponentModel;
using System.Linq;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Randomizations;
using GeneticSharp.Infrastructure.Framework.Texts;
namespace GeneticSharp.Domain.Mutations
{
/// <summary>
/// Reverse Sequence Mutation (RSM).
/// <remarks>
/// In the reverse sequence mutation operator, we take a sequence S limited by two
/// positions i and j randomly chosen, such that i<j. The gene order in this sequence
/// will be reversed by the same way as what has been covered in the previous operation.
/// <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("Reverse Sequence (RSM)")]
public class ReverseSequenceMutation : MutationBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ReverseSequenceMutation"/> class.
/// </summary>
public ReverseSequenceMutation()
{
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 (chromosome.Length < 3)
{
throw new MutationException(this, "A chromosome should have, at least, 3 genes. {0} has only {1} gene.".With(chromosome.GetType().Name, chromosome.Length));
}
if (RandomizationProvider.Current.GetDouble() <= probability)
{
var indexes = RandomizationProvider.Current.GetUniqueInts(2, 0, chromosome.Length).OrderBy(i => i).ToArray();
var firstIndex = indexes[0];
var secondIndex = indexes[1];
var revertedSequence = chromosome.GetGenes().Skip(firstIndex).Take((secondIndex - firstIndex) + 1).Reverse().ToArray();
chromosome.ReplaceGenes(firstIndex, revertedSequence);
}
}
#endregion
}
}