Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/UniformSomePositionsManipulator.cs @ 17564

Last change on this file since 17564 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("1A0F372D-7256-41EA-B825-9173BC1E278B")]
38  public class UniformSomePositionsManipulator : BoundedIntegerVectorManipulator {
39
40    public IValueLookupParameter<DoubleValue> ProbabilityParameter {
41      get { return (IValueLookupParameter<DoubleValue>)Parameters["Probability"]; }
42    }
43
44    [StorableConstructor]
45    protected UniformSomePositionsManipulator(StorableConstructorFlag _) : base(_) { }
46    protected UniformSomePositionsManipulator(UniformSomePositionsManipulator original, Cloner cloner) : base(original, cloner) { }
47    public UniformSomePositionsManipulator()
48      : base() {
49      Parameters.Add(new ValueLookupParameter<DoubleValue>("Probability", "The probability for each dimension to be manipulated.", new DoubleValue(0.5)));
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new UniformSomePositionsManipulator(this, cloner);
54    }
55
56    /// <summary>
57    /// Changes randomly several, but at least one, positions in the given integer <paramref name="vector"/>, according to the given probabilities.
58    /// </summary>
59    /// <param name="random">A random number generator.</param>
60    /// <param name="vector">The integer vector to manipulate.</param>
61    /// <param name="bounds"> Contains the minimum value (inclusive), maximum value (exclusive), and step size of the sampling range for
62    /// the vector element to change.</param>
63    /// <param name="probability">The probability for each dimension to be manipulated..</param>
64    public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, double probability) {
65      if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("UniformSomePositionsManipulator: Invalid bounds specified", "bounds");
66      bool atLeastOneManipulated = false;
67      for (int index = 0; index < vector.Length; index++) {
68        if (random.NextDouble() < probability) {
69          atLeastOneManipulated = true;
70          UniformOnePositionManipulator.Manipulate(random, vector, bounds, index);
71        }
72      }
73
74      if (!atLeastOneManipulated) {
75        UniformOnePositionManipulator.Manipulate(random, vector, bounds, random.Next(vector.Length));
76      }
77    }
78
79    /// <summary>
80    /// Changes randomly several, but at least one, positions in the given integer <paramref name="vector"/>.
81    /// </summary>
82    /// <remarks>Calls <see cref="Apply"/>.</remarks>
83    /// <param name="random">A random number generator.</param>
84    /// <param name="vector">The integer vector to manipulate.</param>
85    protected override void ManipulateBounded(IRandom random, IntegerVector vector, IntMatrix bounds) {
86      Apply(random, vector, bounds, ProbabilityParameter.ActualValue.Value);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.