#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Random;
namespace HeuristicLab.Encodings.IntegerVectorEncoding {
///
/// Manipulates each dimension in the integer vector with the mutation strength given
/// in the strategy parameter vector.
///
[Item("SelfAdaptiveRoundedNormalAllPositionsManipulator", "This manipulation operator adds a value sigma_i * N(0,1) to the current value in each position i. The resulting value is rounded to the next feasible value. The values for sigma_i are looked up dynamically. If there are less elements in the strategy vector than positions, then the strategy vector is cycled.")]
[StorableClass]
public class SelfAdaptiveRoundedNormalAllPositionsManipulator : BoundedIntegerVectorManipulator, ISelfAdaptiveManipulator {
public Type StrategyParameterType {
get { return typeof(IIntegerVectorStdDevStrategyParameterOperator); }
}
///
/// Parameter for the strategy vector.
///
public ILookupParameter StrategyParameterParameter {
get { return (ILookupParameter)Parameters["StrategyParameter"]; }
}
IParameter ISelfAdaptiveManipulator.StrategyParameterParameter {
get { return StrategyParameterParameter; }
}
[StorableConstructor]
protected SelfAdaptiveRoundedNormalAllPositionsManipulator(bool deserializing) : base(deserializing) { }
protected SelfAdaptiveRoundedNormalAllPositionsManipulator(SelfAdaptiveRoundedNormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
///
/// Initializes a new instance of with one.
///
public SelfAdaptiveRoundedNormalAllPositionsManipulator()
: base() {
Parameters.Add(new LookupParameter("StrategyParameter", "The vector containing the endogenous strategy parameters."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SelfAdaptiveRoundedNormalAllPositionsManipulator(this, cloner);
}
///
/// Performs an adaptive normally distributed all position manipulation on the given
/// and rounding the results to the next feasible value.
///
/// Thrown when the strategy vector is not
/// as long as the vector to get manipulated.
/// The strategy vector determining the strength of the mutation.
/// A random number generator.
/// The integer vector to manipulate.
/// The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).
public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, DoubleArray strategyParameters) {
if (strategyParameters == null || strategyParameters.Length == 0) throw new ArgumentException("SelfAdaptiveRoundedNormalAllPositionsManipulator: Vector containing the standard deviations is not defined.", "sigma");
if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("SelfAdaptiveRoundedNormalAllPositionsManipulator: Invalid bounds specified.", "bounds");
var N = new NormalDistributedRandom(random, 0.0, 1.0);
if (strategyParameters != null) {
for (int i = 0; i < vector.Length; i++) {
int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
int value = (vector[i] + (int)Math.Round((N.NextDouble() * strategyParameters[i % strategyParameters.Length])) - min) / step;
max = FloorFeasible(min, max, step, max - 1);
vector[i] = RoundFeasible(min, max, step, value);
}
}
}
///
/// Checks that the strategy vector is not null and forwards the call to the static Apply method.
///
/// The random number generator.
/// The vector of integer values that is manipulated.
/// The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).
protected override void ManipulateBounded(IRandom random, IntegerVector vector, IntMatrix bounds) {
Apply(random, vector, bounds, StrategyParameterParameter.ActualValue);
}
}
}