#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.BinaryVectorEncoding { /// /// Flips some bits of a binary vector. /// /// /// 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. /// [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.")] [StorableClass] public sealed class SomePositionsBitflipManipulator : BinaryVectorManipulator { /// /// Mmutation probability for each position. /// public IValueLookupParameter MutationProbabilityParameter { get { return (IValueLookupParameter)Parameters["MutationProbability"]; } } [StorableConstructor] private SomePositionsBitflipManipulator(bool deserializing) : base(deserializing) { } private SomePositionsBitflipManipulator(SomePositionsBitflipManipulator original, Cloner cloner) : base(original, cloner) { } /// /// Initializes a new instance of /// public SomePositionsBitflipManipulator() : base() { Parameters.Add(new ValueLookupParameter("MutationProbability", "The mutation probability for each position", new DoubleValue(0.2))); } public override IDeepCloneable Clone(Cloner cloner) { return new SomePositionsBitflipManipulator(this, cloner); } /// /// Performs the some positions bitflip mutation on a binary vector. /// /// The random number generator to use. /// The vector that should be manipulated. /// The probability a bit is flipped. public static void Apply(IRandom random, BinaryVector vector, DoubleValue pm) { for (int i = 0; i < vector.Length; i++) { if (random.NextDouble() < pm.Value) { vector[i] = !vector[i]; } } } /// /// Forwards the call to . /// /// The random number generator to use. /// The vector of binary values to manipulate. protected override void Manipulate(IRandom random, BinaryVector binaryVector) { if (MutationProbabilityParameter.ActualValue == null) throw new InvalidOperationException("SomePositionsBitflipManipulator: Parameter " + MutationProbabilityParameter.ActualName + " could not be found."); Apply(random, binaryVector, MutationProbabilityParameter.ActualValue); } } }