1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.BinaryVectorEncoding {
|
---|
29 | /// <summary>
|
---|
30 | /// Flips some bits of a binary vector.
|
---|
31 | /// </summary>
|
---|
32 | /// <remarks>
|
---|
33 | /// 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.
|
---|
34 | /// </remarks>
|
---|
35 | [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.")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class SomePositionsBitflipManipulator : BinaryVectorManipulator {
|
---|
38 | /// <summary>
|
---|
39 | /// Mmutation probability for each position.
|
---|
40 | /// </summary>
|
---|
41 | public ValueLookupParameter<DoubleValue> MutationProbabilityParameter {
|
---|
42 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MutationProbability"]; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | private SomePositionsBitflipManipulator(bool deserializing) : base(deserializing) { }
|
---|
47 | private SomePositionsBitflipManipulator(SomePositionsBitflipManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
48 | /// <summary>
|
---|
49 | /// Initializes a new instance of <see cref="NPointCrossover"/>
|
---|
50 | /// </summary>
|
---|
51 | public SomePositionsBitflipManipulator()
|
---|
52 | : base() {
|
---|
53 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MutationProbability", "The mutation probability for each position", new DoubleValue(0.2)));
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new SomePositionsBitflipManipulator(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Performs the some positions bitflip mutation on a binary vector.
|
---|
62 | /// </summary>
|
---|
63 | /// <param name="random">The random number generator to use.</param>
|
---|
64 | /// <param name="vector">The vector that should be manipulated.</param>
|
---|
65 | /// <param name="pm">The probability a bit is flipped.</param>
|
---|
66 | public static void Apply(IRandom random, BinaryVector vector, DoubleValue pm) {
|
---|
67 | for (int i = 0; i < vector.Length; i++) {
|
---|
68 | if (random.NextDouble() < pm.Value) {
|
---|
69 | vector[i] = !vector[i];
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Forwards the call to <see cref="Apply(IRandom, BinaryVector)"/>.
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="random">The random number generator to use.</param>
|
---|
78 | /// <param name="realVector">The vector of binary values to manipulate.</param>
|
---|
79 | protected override void Manipulate(IRandom random, BinaryVector binaryVector) {
|
---|
80 | Apply(random, binaryVector, MutationProbabilityParameter.Value);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|