Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Manipulators/SomePositionsBitflipManipulator.cs @ 3062

Last change on this file since 3062 was 3062, checked in by svonolfe, 15 years ago

Implemented operators and test cases for the BinaryVector encoding (#914)

File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Data;
8using HeuristicLab.Parameters;
9
10namespace 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}
Note: See TracBrowser for help on using the repository browser.