source: branches/2994-AutoDiffForIntervals/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Manipulators/SomePositionsBitflipManipulator.cs @ 17209

Last change on this file since 17209 was 17209, checked in by gkronber, 4 months ago

#2994: merged r17132:17198 from trunk to branch

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HEAL.Attic;
28
29namespace HeuristicLab.Encodings.BinaryVectorEncoding {
30  /// <summary>
31  /// Flips some bits of a binary vector.
32  /// </summary>
33  /// <remarks>
34  /// 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.
35  /// </remarks>
36  [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.")]
37  [StorableType("058E0380-0BFB-4E39-82DC-C4E3CCD8EAF5")]
38  public sealed class SomePositionsBitflipManipulator : BinaryVectorManipulator {
39    /// <summary>
40    /// Mmutation probability for each position.
41    /// </summary>
42    public IValueLookupParameter<DoubleValue> MutationProbabilityParameter {
43      get { return (IValueLookupParameter<DoubleValue>)Parameters["MutationProbability"]; }
44    }
45
46    [StorableConstructor]
47    private SomePositionsBitflipManipulator(StorableConstructorFlag _) : base(_) { }
48    private SomePositionsBitflipManipulator(SomePositionsBitflipManipulator original, Cloner cloner) : base(original, cloner) { }
49    /// <summary>
50    /// Initializes a new instance of <see cref="NPointCrossover"/>
51    /// </summary>
52    public SomePositionsBitflipManipulator()
53      : base() {
54      Parameters.Add(new ValueLookupParameter<DoubleValue>("MutationProbability", "The mutation probability for each position", new DoubleValue(0.2)));
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new SomePositionsBitflipManipulator(this, cloner);
59    }
60
61    /// <summary>
62    /// Performs the some positions bitflip mutation on a binary vector.
63    /// </summary>
64    /// <param name="random">The random number generator to use.</param>
65    /// <param name="vector">The vector that should be manipulated.</param>
66    /// <param name="pm">The probability a bit is flipped.</param>
67    public static void Apply(IRandom random, BinaryVector vector, DoubleValue pm) {
68      for (int i = 0; i < vector.Length; i++) {
69        if (random.NextDouble() < pm.Value) {
70          vector[i] = !vector[i];
71        }
72      }
73    }
74
75    /// <summary>
76    /// Forwards the call to <see cref="Apply(IRandom, BinaryVector)"/>.
77    /// </summary>
78    /// <param name="random">The random number generator to use.</param>
79    /// <param name="realVector">The vector of binary values to manipulate.</param>
80    protected override void Manipulate(IRandom random, BinaryVector binaryVector) {
81      if (MutationProbabilityParameter.ActualValue == null) throw new InvalidOperationException("SomePositionsBitflipManipulator: Parameter " + MutationProbabilityParameter.ActualName + " could not be found.");
82      Apply(random, binaryVector, MutationProbabilityParameter.ActualValue);
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.