[7686] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12694] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7686] | 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.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Random;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Manipulates each dimension in the integer vector with the mutation strength given
|
---|
| 33 | /// in the sigma parameter vector and rounds the result to the next feasible value.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("RoundedNormalAllPositionsManipulator", "This manipulation operator adds a value sigma_i * N_i(0,1) to the current value in each position i given the values for sigma_i in the parameter. The result is rounded to the next feasible value. If there are less elements in Sigma than positions, then Sigma is cycled.")]
|
---|
| 36 | [StorableClass]
|
---|
[8017] | 37 | public class RoundedNormalAllPositionsManipulator : BoundedIntegerVectorManipulator {
|
---|
[7686] | 38 |
|
---|
| 39 | public IValueParameter<DoubleArray> SigmaParameter {
|
---|
| 40 | get { return (IValueParameter<DoubleArray>)Parameters["Sigma"]; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | [StorableConstructor]
|
---|
| 44 | protected RoundedNormalAllPositionsManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 45 | protected RoundedNormalAllPositionsManipulator(RoundedNormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 46 | /// <summary>
|
---|
| 47 | /// Initializes a new instance of <see cref="RoundedNormalAllPositionsManipulator"/> with one
|
---|
| 48 | /// parameter (<c>Sigma</c>).
|
---|
| 49 | /// </summary>
|
---|
| 50 | public RoundedNormalAllPositionsManipulator()
|
---|
| 51 | : base() {
|
---|
| 52 | Parameters.Add(new ValueParameter<DoubleArray>("Sigma", "The vector containing the standard deviations used for manipulating each dimension. If it is only of length one the same sigma will be used for every dimension.", new DoubleArray(new double[] { 1 })));
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 56 | return new RoundedNormalAllPositionsManipulator(this, cloner);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /// <summary>
|
---|
| 60 | /// Performs a normally distributed all position manipulation on the given
|
---|
| 61 | /// <paramref name="vector"/> and rounds the result to the next feasible value.
|
---|
| 62 | /// </summary>
|
---|
| 63 | /// <exception cref="InvalidOperationException">Thrown when the sigma vector is null or of length 0.</exception>
|
---|
| 64 | /// <param name="sigma">The sigma vector determining the strength of the mutation.</param>
|
---|
| 65 | /// <param name="random">A random number generator.</param>
|
---|
[8017] | 66 | /// <param name="vector">The integer vector to manipulate.</param>#
|
---|
| 67 | /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
|
---|
[7686] | 68 | /// <returns>The manipulated integer vector.</returns>
|
---|
| 69 | public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, DoubleArray sigma) {
|
---|
[8017] | 70 | if (sigma == null || sigma.Length == 0) throw new ArgumentException("RoundedNormalAllPositionsManipulator: Vector containing the standard deviations is not defined.", "sigma");
|
---|
[7686] | 71 | if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("RoundedNormalAllPositionsManipulator: Invalid bounds specified.", "bounds");
|
---|
| 72 | var N = new NormalDistributedRandom(random, 0.0, 1.0);
|
---|
| 73 | for (int i = 0; i < vector.Length; i++) {
|
---|
| 74 | int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
|
---|
| 75 | if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
|
---|
| 76 |
|
---|
| 77 | int value = (vector[i] + (int)Math.Round((N.NextDouble() * sigma[i % sigma.Length])) - min) / step;
|
---|
[8790] | 78 | max = FloorFeasible(min, max, step, max - 1);
|
---|
[8017] | 79 | vector[i] = RoundFeasible(min, max, step, value);
|
---|
[7686] | 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /// <summary>
|
---|
| 84 | /// Retrieves the bounds and forwards the call to the static Apply method.
|
---|
| 85 | /// </summary>
|
---|
| 86 | /// <param name="random">The random number generator.</param>
|
---|
| 87 | /// <param name="vector">The vector of integer values that is manipulated.</param>
|
---|
[8017] | 88 | /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
|
---|
| 89 | protected override void ManipulateBounded(IRandom random, IntegerVector vector, IntMatrix bounds) {
|
---|
| 90 | Apply(random, vector, bounds, SigmaParameter.Value);
|
---|
[7686] | 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|