forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniformMutation.cs
93 lines (83 loc) · 3.45 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
90
91
92
93
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Randomizations;
using GeneticSharp.Infrastructure.Framework.Texts;
using GeneticSharp.Infrastructure.Framework.Commons;
namespace GeneticSharp.Domain.Mutations
{
/// <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;
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "mall")]
private bool m_allGenesMutable;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.Domain.Mutations.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.Domain.Mutations.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.Domain.Mutations.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);
}
}
foreach (var i in m_mutableGenesIndexes)
{
if (i >= genesLength)
{
throw new MutationException(this, "The chromosome has no gene on index {0}. The chromosome genes length is {1}.".With(i, genesLength));
}
if (RandomizationProvider.Current.GetDouble() <= probability)
{
chromosome.ReplaceGene(i, chromosome.GenerateGene(i));
}
}
}
#endregion
}
}