forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SequenceMutationBase.cs
57 lines (51 loc) · 2.19 KB
/
SequenceMutationBase.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.Collections.Generic;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Randomizations;
using System.Linq;
using GeneticSharp.Infrastructure.Framework.Texts;
namespace GeneticSharp.Domain.Mutations
{
/// <summary>
/// Base class for Mutations on a Sub-Sequence.
/// </summary>
public abstract class SequenceMutationBase : MutationBase
{
#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)
{
ValidateLength(chromosome);
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 sequenceLength = (secondIndex - firstIndex) + 1;
var mutatedSequence = MutateOnSequence(chromosome.GetGenes().Skip(firstIndex).Take(sequenceLength)).ToArray();
chromosome.ReplaceGenes(firstIndex, mutatedSequence);
}
}
/// <summary>
/// Validate length of the chromosome.
/// </summary>
/// <param name="chromosome">The chromosome.</param>
protected virtual void ValidateLength(IChromosome chromosome)
{
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));
}
}
/// <summary>
/// Mutate selected sequence.
/// </summary>
/// <returns>The resulted sequence after mutation operation.</returns>
/// <param name="sequence">The sequence to be mutated.</param>
protected abstract IEnumerable<T> MutateOnSequence<T>(IEnumerable<T> sequence);
#endregion
}
}