Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/MichalewiczNonUniformAllPositionsManipulator.cs @ 3172

Last change on this file since 3172 was 3172, checked in by abeham, 15 years ago

Fixed SimulatedBinaryCrossover and name of MichalewiczNonUniformAllPositionsManipulator #890

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.RealVectorEncoding {
29  /// <summary>
30  /// The solution is manipulated with diminishing strength over time. In addition the mutated values are not sampled over the entire domain, but additive.<br/>
31  /// 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.
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("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.")]
37  [StorableClass]
38  public class MichalewiczNonUniformAllPositionsManipulator : RealVectorManipulator {
39    /// <summary>
40    /// 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.
41    /// </summary>
42    public ValueLookupParameter<DoubleMatrix> BoundsParameter {
43      get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
44    }
45    /// <summary>
46    /// The current generation.
47    /// </summary>
48    public LookupParameter<IntValue> GenerationParameter {
49      get { return (LookupParameter<IntValue>)Parameters["Generation"]; }
50    }
51    /// <summary>
52    /// The maximum generation.
53    /// </summary>
54    public LookupParameter<IntValue> MaximumGenerationsParameter {
55      get { return (LookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
56    }
57    /// <summary>
58    /// The parameter describing how much the mutation should depend on the progress towards the maximum generation.
59    /// </summary>
60    public ValueLookupParameter<DoubleValue> GenerationDependencyParameter {
61      get { return (ValueLookupParameter<DoubleValue>)Parameters["GenerationDependency"]; }
62    }
63
64    /// <summary>
65    /// Initializes a new instance of <see cref="MichalewiczNonUniformAllPositionsManipulator"/> with
66    /// four parameters (<c>Bounds</c>, <c>CurrentGeneration</c>,
67    /// <c>MaximumGenerations</c> and <c>GenerationDependency</c>).
68    /// </summary>
69    public MichalewiczNonUniformAllPositionsManipulator()
70      : base() {
71      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("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."));
72      Parameters.Add(new LookupParameter<IntValue>("Generation", "Current generation of the algorithm"));
73      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "Maximum number of generations"));
74      Parameters.Add(new ValueLookupParameter<DoubleValue>("GenerationDependency", "Specifies the degree of dependency on the number of generations", new DoubleValue(5)));
75    }
76
77    /// <summary>
78    /// Performs a non uniformly distributed all position manipulation on the given
79    /// real <paramref name="vector"/>. The probability of stronger mutations reduces the more <see cref="currentGeneration"/> approaches <see cref="maximumGenerations"/>.
80    /// </summary>
81    /// <exception cref="ArgumentException">Thrown when <paramref name="currentGeneration"/> is greater than <paramref name="maximumGenerations"/>.</exception>
82    /// <param name="random">The random number generator.</param>
83    /// <param name="vector">The real vector to manipulate.</param>
84    /// <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>
85    /// <param name="currentGeneration">The current generation of the algorithm.</param>
86    /// <param name="maximumGenerations">Maximum number of generations.</param>
87    /// <param name="generationsDependency">Specifies the degree of dependency on the number of generations.</param>
88    /// <returns>The manipulated real vector.</returns>
89    public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, IntValue currentGeneration, IntValue maximumGenerations, DoubleValue generationsDependency) {
90      if (currentGeneration.Value > maximumGenerations.Value) throw new ArgumentException("MichalewiczNonUniformAllPositionManipulator: CurrentGeneration must be smaller or equal than MaximumGeneration", "currentGeneration");
91      int length = vector.Length;
92
93      double prob = Math.Pow(1 - currentGeneration.Value / maximumGenerations.Value, generationsDependency.Value);
94
95      for (int i = 0; i < length; i++) {
96        double min = bounds[i % bounds.Rows, 0];
97        double max = bounds[i % bounds.Rows, 1];
98        if (random.NextDouble() < 0.5) {
99          vector[i] = vector[i] + (max - vector[i]) * (1 - Math.Pow(random.NextDouble(), prob));
100        } else {
101          vector[i] = vector[i] - (vector[i] - min) * (1 - Math.Pow(random.NextDouble(), prob));
102        }
103      }
104    }
105
106    /// <summary>
107    /// Checks if all parameters are available and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue, IntValue, IntValue, DoubleValue)"/>.
108    /// </summary>
109    /// <param name="random">The random number generator.</param>
110    /// <param name="realVector">The real vector that should be manipulated.</param>
111    protected override void Manipulate(IRandom random, RealVector realVector) {
112      if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
113      if (GenerationParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + GenerationParameter.ActualName + " could not be found.");
114      if (MaximumGenerationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + MaximumGenerationsParameter.ActualName + " could not be found.");
115      if (GenerationDependencyParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + GenerationDependencyParameter.ActualName + " could not be found.");
116      Apply(random, realVector, BoundsParameter.ActualValue, GenerationParameter.ActualValue, MaximumGenerationsParameter.ActualValue, GenerationDependencyParameter.ActualValue);
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.