Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/MichalewiczNonUniformAllPositionsManipulator.cs @ 16057

Last change on this file since 16057 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 8.0 KB
RevLine 
[2900]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2900]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;
[4722]23using HeuristicLab.Common;
[2900]24using HeuristicLab.Core;
25using HeuristicLab.Data;
[3750]26using HeuristicLab.Optimization;
[2915]27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2900]29
[3053]30namespace HeuristicLab.Encodings.RealVectorEncoding {
[2900]31  /// <summary>
[2915]32  /// The solution is manipulated with diminishing strength over time. In addition the mutated values are not sampled over the entire domain, but additive.<br/>
33  /// Initially, the space will be searched uniformly and very locally at later stages. This increases the probability of generating the new numbers closer to the current value.
[2900]34  /// </summary>
[2915]35  /// <remarks>
36  /// 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  /// </remarks>
[3172]38  [Item("MichalewiczNonUniformAllPositionsManipulator", "It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
[3017]39  [StorableClass]
[3750]40  public class MichalewiczNonUniformAllPositionsManipulator : RealVectorManipulator, IIterationBasedOperator {
[2921]41    /// <summary>
[3750]42    /// The current iteration.
[2921]43    /// </summary>
[3750]44    public ILookupParameter<IntValue> IterationsParameter {
45      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
[2915]46    }
[2921]47    /// <summary>
[3750]48    /// The maximum iteration.
[2921]49    /// </summary>
[3750]50    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
51      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
[2915]52    }
[2921]53    /// <summary>
[3750]54    /// The parameter describing how much the mutation should depend on the progress towards the maximum iteration.
[2921]55    /// </summary>
[3750]56    public ValueLookupParameter<DoubleValue> IterationDependencyParameter {
57      get { return (ValueLookupParameter<DoubleValue>)Parameters["IterationDependency"]; }
[2915]58    }
[2900]59
[4722]60    [StorableConstructor]
61    protected MichalewiczNonUniformAllPositionsManipulator(bool deserializing) : base(deserializing) { }
62    protected MichalewiczNonUniformAllPositionsManipulator(MichalewiczNonUniformAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
[2900]63    /// <summary>
[3750]64    /// Initializes a new instance of <see cref="MichalewiczNonUniformAllPositionsManipulator"/> with three
65    /// parameters (<c>Iterations</c>, <c>MaximumIterations</c> and <c>IterationDependency</c>).
[2900]66    /// </summary>
67    public MichalewiczNonUniformAllPositionsManipulator()
68      : base() {
[3750]69      Parameters.Add(new LookupParameter<IntValue>("Iterations", "Current iteration of the algorithm"));
70      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Maximum number of iterations"));
71      Parameters.Add(new ValueLookupParameter<DoubleValue>("IterationDependency", "Specifies the degree of dependency on the number of iterations. A value of 0 means no dependency and the higher the value the stronger the progress towards maximum iterations will be taken into account by sampling closer around the current position. Value must be >= 0.", new DoubleValue(5)));
[2900]72    }
73
[4722]74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new MichalewiczNonUniformAllPositionsManipulator(this, cloner);
76    }
77
[2900]78    /// <summary>
[3750]79    /// Performs a non uniformly distributed all positions manipulation on the given
80    /// real <paramref name="vector"/>. The probability of stronger mutations reduces the more <see cref="currentIteration"/> approaches <see cref="maximumIterations"/>.
[2900]81    /// </summary>
[3750]82    /// <exception cref="ArgumentException">Thrown when <paramref name="currentIteration"/> is greater than <paramref name="maximumIterations"/>.</exception>
[2900]83    /// <param name="random">The random number generator.</param>
84    /// <param name="vector">The real vector to manipulate.</param>
[3123]85    /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param>
[3750]86    /// <param name="currentIteration">The current iteration of the algorithm.</param>
87    /// <param name="maximumIterations">Maximum number of iterations.</param>
88    /// <param name="iterationDependency">Specifies the degree of dependency on the number of iterations. A value of 0 means no dependency and the higher the value the stronger the progress towards maximum iterations will be taken into account by sampling closer around the current position. Value must be >= 0.</param>
[2900]89    /// <returns>The manipulated real vector.</returns>
[3750]90    public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, IntValue currentIteration, IntValue maximumIterations, DoubleValue iterationDependency) {
91      if (currentIteration.Value > maximumIterations.Value) throw new ArgumentException("MichalewiczNonUniformAllPositionsManipulator: CurrentIteration must be smaller or equal than MaximumIterations", "currentIteration");
92      if (iterationDependency.Value < 0) throw new ArgumentException("MichalewiczNonUniformAllPositionsManipulator: iterationDependency must be >= 0.", "iterationDependency");
[2900]93      int length = vector.Length;
94
[3750]95      double prob = Math.Pow(1 - currentIteration.Value / maximumIterations.Value, iterationDependency.Value);
[2915]96
[2900]97      for (int i = 0; i < length; i++) {
[3123]98        double min = bounds[i % bounds.Rows, 0];
99        double max = bounds[i % bounds.Rows, 1];
[2900]100        if (random.NextDouble() < 0.5) {
[3123]101          vector[i] = vector[i] + (max - vector[i]) * (1 - Math.Pow(random.NextDouble(), prob));
[2900]102        } else {
[3123]103          vector[i] = vector[i] - (vector[i] - min) * (1 - Math.Pow(random.NextDouble(), prob));
[2900]104        }
105      }
106    }
107
[2915]108    /// <summary>
[3060]109    /// Checks if all parameters are available and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue, IntValue, IntValue, DoubleValue)"/>.
[2915]110    /// </summary>
111    /// <param name="random">The random number generator.</param>
112    /// <param name="realVector">The real vector that should be manipulated.</param>
[3060]113    protected override void Manipulate(IRandom random, RealVector realVector) {
[3123]114      if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
[3750]115      if (IterationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + IterationsParameter.ActualName + " could not be found.");
116      if (MaximumIterationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + MaximumIterationsParameter.ActualName + " could not be found.");
117      if (IterationDependencyParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + IterationDependencyParameter.ActualName + " could not be found.");
118      Apply(random, realVector, BoundsParameter.ActualValue, IterationsParameter.ActualValue, MaximumIterationsParameter.ActualValue, IterationDependencyParameter.ActualValue);
[2900]119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.