Free cookie consent management tool by TermsFeed Policy Generator

source: branches/IntegerVectorEncoding/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/UniformSomePositionsManipulator.cs @ 7699

Last change on this file since 7699 was 7699, checked in by abeham, 12 years ago

#1775: Added UniformSomePositionsManipulator and minor changes

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.IntegerVectorEncoding {
30  /// <summary>
31  /// Uniformly distributed change of several, but at least one, positions of an integer vector.
32  /// </summary>
33  /// <remarks>
34  /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
35  /// </remarks>
36  [Item("UniformSomePositionsManipulator", " Uniformly distributed change of several, but at least one, positions of an integer vector. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
37  [StorableClass]
38  public class UniformSomePositionsManipulator : IntegerVectorManipulator, IBoundedIntegerVectorOperator {
39    /// <summary>
40    /// A matrix that specifies the bounds for each dimension in a separate row. The first column denotes the minimum, the second the maximum value, and the third column denotes the step size.
41    /// </summary>
42    public IValueLookupParameter<IntMatrix> BoundsParameter {
43      get { return (IValueLookupParameter<IntMatrix>)Parameters["Bounds"]; }
44    }
45
46    public IValueLookupParameter<DoubleValue> ProbabilityParameter {
47      get { return (IValueLookupParameter<DoubleValue>)Parameters["Probability"]; }
48    }
49
50    [StorableConstructor]
51    protected UniformSomePositionsManipulator(bool deserializing) : base(deserializing) { }
52    protected UniformSomePositionsManipulator(UniformSomePositionsManipulator original, Cloner cloner) : base(original, cloner) { }
53    /// <summary>
54    /// Initializes a new instance of <see cref="UniformSomePositionsManipulator"/> with two parameters
55    /// (<c>Minimum</c> and <c>Maximum</c>).
56    /// </summary>
57    public UniformSomePositionsManipulator()
58      : base() {
59      Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "The bounds matrix can contain one row for each dimension with three columns specifying minimum (inclusive), maximum (exclusive), and step size. If less rows are given the matrix is cycled."));
60      Parameters.Add(new ValueLookupParameter<DoubleValue>("Probability", "The probability for each dimension to be manipulated.", new DoubleValue(0.5)));
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new UniformSomePositionsManipulator(this, cloner);
65    }
66
67    /// <summary>
68    /// Changes randomly several, but at least one, positions in the given integer <paramref name="vector"/>, according to the given probabilities.
69    /// </summary>
70    /// <param name="random">A random number generator.</param>
71    /// <param name="vector">The integer vector to manipulate.</param>
72    /// <param name="bounds"> Contains the minimum value (inclusive), maximum value (exclusive), and step size of the sampling range for
73    /// the vector element to change.</param>
74    /// <param name="probability">The probability for each dimension to be manipulated..</param>
75    public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, double probability) {
76      if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("UniformSomePositionsManipulator: Invalid bounds specified", "bounds");
77      bool atLeastOneManipulated = false;
78      for (int index = 0; index < vector.Length; index++) {
79        if (random.NextDouble() < probability) {
80          atLeastOneManipulated = true;
81          Manipulate(random, vector, bounds, index);
82        }
83      }
84
85      if (!atLeastOneManipulated) {
86        Manipulate(random, vector, bounds, random.Next(vector.Length));
87      }
88    }
89
90    private static void Manipulate(IRandom random, IntegerVector vector, IntMatrix bounds, int index) {
91      int min = bounds[index % bounds.Rows, 0], max = bounds[index % bounds.Rows, 1], step = 1;
92      if (bounds.Columns > 2) step = bounds[index % bounds.Rows, 2];
93      int value = (max - min) / step;
94      vector[index] = random.Next(value) * step + min;
95    }
96
97    /// <summary>
98    /// Changes randomly several, but at least one, positions in the given integer <paramref name="vector"/>.
99    /// </summary>
100    /// <remarks>Calls <see cref="Apply"/>.</remarks>
101    /// <param name="random">A random number generator.</param>
102    /// <param name="vector">The integer vector to manipulate.</param>
103    protected override void Manipulate(IRandom random, IntegerVector vector) {
104      if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("UniformSomePositionsManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
105      Apply(random, vector, BoundsParameter.ActualValue, ProbabilityParameter.ActualValue.Value);
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.