[2900] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2915] | 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
[3376] | 23 | using HeuristicLab.Common;
|
---|
[2900] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[2915] | 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2900] | 28 |
|
---|
[3053] | 29 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[2900] | 30 | /// <summary>
|
---|
[2915] | 31 | /// The solution is manipulated with diminishing strength over time. In addition the mutated value is not sampled over the entire domain, but additive at the selected position.<br/>
|
---|
| 32 | /// Initially, the space will be searched uniformly and very locally at later stages. This increases the probability of generating the new number closer to the current value.
|
---|
[2900] | 33 | /// </summary>
|
---|
[2915] | 34 | /// <remarks>
|
---|
| 35 | /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
|
---|
| 36 | /// </remarks>
|
---|
| 37 | [Item("MichalewiczNonUniformOnePositionManipulator", "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] | 38 | [StorableClass]
|
---|
[2915] | 39 | public class MichalewiczNonUniformOnePositionManipulator : RealVectorManipulator {
|
---|
[2921] | 40 | /// <summary>
|
---|
| 41 | /// The current generation.
|
---|
| 42 | /// </summary>
|
---|
[3048] | 43 | public LookupParameter<IntValue> GenerationParameter {
|
---|
[3677] | 44 | get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
[2915] | 45 | }
|
---|
[2921] | 46 | /// <summary>
|
---|
| 47 | /// The maximum generation.
|
---|
| 48 | /// </summary>
|
---|
[3048] | 49 | public LookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 50 | get { return (LookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
[2915] | 51 | }
|
---|
[2921] | 52 | /// <summary>
|
---|
| 53 | /// The parameter describing how much the mutation should depend on the progress towards the maximum generation.
|
---|
| 54 | /// </summary>
|
---|
[3048] | 55 | public ValueLookupParameter<DoubleValue> GenerationDependencyParameter {
|
---|
| 56 | get { return (ValueLookupParameter<DoubleValue>)Parameters["GenerationDependency"]; }
|
---|
[2915] | 57 | }
|
---|
[2900] | 58 |
|
---|
| 59 | /// <summary>
|
---|
[3123] | 60 | /// Initializes a new instance of <see cref="MichalewiczNonUniformOnePositionManipulator"/> with four
|
---|
| 61 | /// parameters (<c>Bounds</c>, <c>CurrentGeneration</c>, <c>MaximumGenerations</c>
|
---|
[2915] | 62 | /// and <c>GenerationDependency</c>).
|
---|
[2900] | 63 | /// </summary>
|
---|
| 64 | public MichalewiczNonUniformOnePositionManipulator()
|
---|
| 65 | : base() {
|
---|
[3677] | 66 | Parameters.Add(new LookupParameter<IntValue>("Generations", "Current generation of the algorithm"));
|
---|
[3048] | 67 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "Maximum number of generations"));
|
---|
[3677] | 68 | Parameters.Add(new ValueLookupParameter<DoubleValue>("GenerationDependency", "Specifies the degree of dependency on the number of generations. A value of 0 means no dependency and the higher the value the stronger the progress towards maximum generations will be taken into account by sampling closer around the current position. Value must be >= 0.", new DoubleValue(5)));
|
---|
[2900] | 69 | }
|
---|
| 70 |
|
---|
| 71 | /// <summary>
|
---|
| 72 | /// Performs a non uniformly distributed one position manipulation on the given
|
---|
[2915] | 73 | /// real <paramref name="vector"/>. The probability of stronger mutations reduces the more <see cref="currentGeneration"/> approaches <see cref="maximumGenerations"/>.
|
---|
[2900] | 74 | /// </summary>
|
---|
[2915] | 75 | /// <exception cref="ArgumentException">Thrown when <paramref name="currentGeneration"/> is greater than <paramref name="maximumGenerations"/>.</exception>
|
---|
[2900] | 76 | /// <param name="random">The random number generator.</param>
|
---|
| 77 | /// <param name="vector">The real vector to manipulate.</param>
|
---|
[3123] | 78 | /// <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>
|
---|
[2900] | 79 | /// <param name="currentGeneration">The current generation of the algorithm.</param>
|
---|
| 80 | /// <param name="maximumGenerations">Maximum number of generations.</param>
|
---|
[3677] | 81 | /// <param name="generationsDependency">Specifies the degree of dependency on the number of generations. A value of 0 means no dependency and the higher the value the stronger the progress towards maximum generations will be taken into account by sampling closer around the current position. Value must be >= 0.</param>
|
---|
[2900] | 82 | /// <returns>The manipulated real vector.</returns>
|
---|
[3123] | 83 | public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, IntValue currentGeneration, IntValue maximumGenerations, DoubleValue generationsDependency) {
|
---|
[2915] | 84 | if (currentGeneration.Value > maximumGenerations.Value) throw new ArgumentException("MichalewiczNonUniformOnePositionManipulator: CurrentGeneration must be smaller or equal than MaximumGeneration", "currentGeneration");
|
---|
[3677] | 85 | if (generationsDependency.Value < 0) throw new ArgumentException("MichalewiczNonUniformOnePositionManipulator: GenerationsDependency must be >= 0.");
|
---|
[2900] | 86 | int length = vector.Length;
|
---|
[2915] | 87 | int index = random.Next(length);
|
---|
[2900] | 88 |
|
---|
[2915] | 89 | double prob = (1 - Math.Pow(random.NextDouble(), Math.Pow(1 - currentGeneration.Value / maximumGenerations.Value, generationsDependency.Value)));
|
---|
| 90 |
|
---|
[3123] | 91 | double min = bounds[index % bounds.Rows, 0];
|
---|
| 92 | double max = bounds[index % bounds.Rows, 1];
|
---|
| 93 |
|
---|
[2900] | 94 | if (random.NextDouble() < 0.5) {
|
---|
[3123] | 95 | vector[index] = vector[index] + (max - vector[index]) * prob;
|
---|
[2900] | 96 | } else {
|
---|
[3123] | 97 | vector[index] = vector[index] - (vector[index] - min) * prob;
|
---|
[2900] | 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[2915] | 101 | /// <summary>
|
---|
[3060] | 102 | /// Checks if all parameters are available and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue, IntValue, IntValue, DoubleValue)"/>.
|
---|
[2915] | 103 | /// </summary>
|
---|
| 104 | /// <param name="random">The random number generator.</param>
|
---|
| 105 | /// <param name="realVector">The real vector that should be manipulated.</param>
|
---|
[3060] | 106 | protected override void Manipulate(IRandom random, RealVector realVector) {
|
---|
[3123] | 107 | if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
|
---|
[2915] | 108 | if (GenerationParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + GenerationParameter.ActualName + " could not be found.");
|
---|
| 109 | if (MaximumGenerationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MaximumGenerationsParameter.ActualName + " could not be found.");
|
---|
| 110 | if (GenerationDependencyParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + GenerationDependencyParameter.ActualName + " could not be found.");
|
---|
[3123] | 111 | Apply(random, realVector, BoundsParameter.ActualValue, GenerationParameter.ActualValue, MaximumGenerationsParameter.ActualValue, GenerationDependencyParameter.ActualValue);
|
---|
[2900] | 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|