1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Encodings.BinaryVectorEncoding {
|
---|
11 | /// <summary>
|
---|
12 | /// Flips some bits of a binary vector.
|
---|
13 | /// </summary>
|
---|
14 | /// <remarks>
|
---|
15 | /// It is implemented as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 43.
|
---|
16 | /// </remarks>
|
---|
17 | [Item("SomePositionsBitflipManipulator", "Flips some bits of a binary vector, each position is flipped with a probability of pm. It is implemented as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 43.")]
|
---|
18 | [StorableClass]
|
---|
19 | public class SomePositionsBitflipManipulator : BinaryVectorManipulator {
|
---|
20 | /// <summary>
|
---|
21 | /// Mmutation probability for each position.
|
---|
22 | /// </summary>
|
---|
23 | public ValueLookupParameter<DoubleValue> MutationProbabilityParameter {
|
---|
24 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MutationProbability"]; }
|
---|
25 | }
|
---|
26 |
|
---|
27 | /// <summary>
|
---|
28 | /// Initializes a new instance of <see cref="NPointCrossover"/>
|
---|
29 | /// </summary>
|
---|
30 | public SomePositionsBitflipManipulator() {
|
---|
31 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MutationProbability", "The mutation probability for each position", new DoubleValue(0.2)));
|
---|
32 | }
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Performs the some positions bitflip mutation on a binary vector.
|
---|
36 | /// </summary>
|
---|
37 | /// <param name="random">The random number generator to use.</param>
|
---|
38 | /// <param name="vector">The vector that should be manipulated.</param>
|
---|
39 | /// <param name="pm">The probability a bit is flipped.</param>
|
---|
40 | public static void Apply(IRandom random, BinaryVector vector, DoubleValue pm) {
|
---|
41 | for (int i = 0; i < vector.Length; i++) {
|
---|
42 | if (random.NextDouble() < pm.Value) {
|
---|
43 | vector[i] = !vector[i];
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Forwards the call to <see cref="Apply(IRandom, BinaryVector)"/>.
|
---|
50 | /// </summary>
|
---|
51 | /// <param name="random">The random number generator to use.</param>
|
---|
52 | /// <param name="realVector">The vector of binary values to manipulate.</param>
|
---|
53 | protected override void Manipulate(IRandom random, BinaryVector binaryVector) {
|
---|
54 | Apply(random, binaryVector, MutationProbabilityParameter.Value);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|