1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence;
|
---|
28 |
|
---|
29 | namespace 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("b72b291b-c8ad-41da-80da-c8b82182e8d9")]
|
---|
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 deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|