-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
DisplacementMutation.cs
60 lines (56 loc) · 2.23 KB
/
DisplacementMutation.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// Displacement Mutation.
/// <remarks>
/// In the displacement mutation operator, a substring is randomly selected from chromosome, is removed, then replaced at a randomly selected position.
/// On implementation, we take a sequence S limited by two positions i and j randomly chosen, The selected substring in this sequence
/// will be left shifted or right shifted randomly to give the effect of removing the substring and inserting it on another position.
/// <see href="https://web.cs.elte.hu/blobs/diplomamunkak/bsc_alkmat/2017/keresztury_bence.pdf">Genetic algorithms and the Traveling Salesman Problem</see>
/// </remarks>
/// </summary>
[DisplayName("Displacement")]
public class DisplacementMutation : SequenceMutationBase
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="DisplacementMutation"/> class.
/// </summary>
public DisplacementMutation()
{
IsOrdered = true;
}
#endregion
#region Methods
/// <summary>
/// Mutate selected sequence.
/// </summary>
/// <returns>The resulted sequence after mutation operation.</returns>
/// <param name="sequence">The sequence to be mutated.</param>
protected override IEnumerable<T> MutateOnSequence<T>(IEnumerable<T> sequence)
{
var geneToShift = DetermineGeneToShift(sequence.Count() - 1);
if (RandomizationProvider.Current.GetDouble() <= 0.5)
{
return sequence.LeftShift(geneToShift);
}
else
{
return sequence.RightShift(geneToShift);
}
}
/// <summary>
/// Determines genes to shift.
/// <returns>Count of genes to be shifted</returns>
/// </summary>
/// <param name="maxCount">max possible count of genes to shift.</param>
protected virtual int DetermineGeneToShift(int maxCount)
{
return RandomizationProvider.Current.GetInt(0, maxCount) + 1;
}
#endregion
}
}