-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
UniformMutation.cs
89 lines (78 loc) · 3.17 KB
/
UniformMutation.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.ComponentModel;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// This operator replaces the value of the chosen gene with a uniform random value selected
/// between the user-specified upper and lower bounds for that gene.
/// <see href="http://en.wikipedia.org/wiki/Mutation_(genetic_algorithm)">Wikipedia</see>
/// </summary>
[DisplayName("Uniform")]
public class UniformMutation : MutationBase
{
#region Fields
private int[] m_mutableGenesIndexes;
private readonly bool m_allGenesMutable;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.UniformMutation"/> class.
/// </summary>
/// <param name="mutableGenesIndexes">Mutable genes indexes.</param>
public UniformMutation(params int[] mutableGenesIndexes)
{
m_mutableGenesIndexes = mutableGenesIndexes;
}
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.UniformMutation"/> class.
/// </summary>
/// <param name="allGenesMutable">If set to <c>true</c> all genes are mutable.</param>
public UniformMutation(bool allGenesMutable)
{
m_allGenesMutable = allGenesMutable;
}
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.UniformMutation"/> class.
/// </summary>
/// <remarks>Creates an instance of UniformMutation where some random genes will be mutated.</remarks>
public UniformMutation() : this(false)
{
}
#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)
{
ExceptionHelper.ThrowIfNull("chromosome", chromosome);
var genesLength = chromosome.Length;
if (m_mutableGenesIndexes == null || m_mutableGenesIndexes.Length == 0)
{
if (m_allGenesMutable)
{
m_mutableGenesIndexes = Enumerable.Range(0, genesLength).ToArray();
}
else
{
m_mutableGenesIndexes = RandomizationProvider.Current.GetInts(1, 0, genesLength);
}
}
for (int i = 0; i < m_mutableGenesIndexes.Length; i++)
{
var geneIndex = m_mutableGenesIndexes[i];
if (geneIndex >= genesLength)
{
throw new MutationException(this, "The chromosome has no gene on index {0}. The chromosome genes length is {1}.".With(geneIndex, genesLength));
}
if (RandomizationProvider.Current.GetDouble() <= probability)
{
chromosome.ReplaceGene(geneIndex, chromosome.GenerateGene(geneIndex));
}
}
}
#endregion
}
}