-
Notifications
You must be signed in to change notification settings - Fork 3
/
DoubleShrinkMutationManager.cs
40 lines (35 loc) · 1.45 KB
/
DoubleShrinkMutationManager.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
using GeneticAlgorithm.Components.Interfaces;
using GeneticAlgorithm.Exceptions;
namespace GeneticAlgorithm.Components.MutationManagers
{
/// <summary>
/// This operator adds a random number taken from a Gaussian distribution with mean equal to the original genome.
/// </summary>
public class DoubleShrinkMutationManager : IMutationManager<double>
{
private readonly double minValue;
private readonly double maxValue;
private readonly double standardDeviation;
public DoubleShrinkMutationManager(double minValue, double maxValue)
{
this.minValue = minValue;
this.maxValue = maxValue;
standardDeviation = (maxValue - minValue) / 4.0;
}
public DoubleShrinkMutationManager(double minValue, double maxValue, double standardDeviation)
{
this.minValue = minValue;
this.maxValue = maxValue;
if (standardDeviation < 0)
throw new GeneticAlgorithmException($"{nameof(standardDeviation)} can't be nagitave");
this.standardDeviation = standardDeviation;
}
public double[] Mutate(double[] vector)
{
for (int i = 0; i < vector.Length; i++)
if (ProbabilityUtils.P(1.0 / vector.Length))
vector[i] = ProbabilityUtils.GaussianDistribution(standardDeviation, vector[i]).Clip(minValue, maxValue);
return vector;
}
}
}