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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.RealVector {
|
---|
29 | /// <summary>
|
---|
30 | /// 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/>
|
---|
31 | /// 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.
|
---|
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("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.")]
|
---|
37 | [StorableClass]
|
---|
38 | public class MichalewiczNonUniformOnePositionManipulator : RealVectorManipulator {
|
---|
39 | /// <summary>
|
---|
40 | /// The lower bound of the values in the real vector.
|
---|
41 | /// </summary>
|
---|
42 | public ValueLookupParameter<DoubleValue> MinimumParameter {
|
---|
43 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Minimum"]; }
|
---|
44 | }
|
---|
45 | /// <summary>
|
---|
46 | /// The upper bound of the values in the real vector.
|
---|
47 | /// </summary>
|
---|
48 | public ValueLookupParameter<DoubleValue> MaximumParameter {
|
---|
49 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Maximum"]; }
|
---|
50 | }
|
---|
51 | /// <summary>
|
---|
52 | /// The current generation.
|
---|
53 | /// </summary>
|
---|
54 | public LookupParameter<IntValue> GenerationParameter {
|
---|
55 | get { return (LookupParameter<IntValue>)Parameters["Generation"]; }
|
---|
56 | }
|
---|
57 | /// <summary>
|
---|
58 | /// The maximum generation.
|
---|
59 | /// </summary>
|
---|
60 | public LookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
61 | get { return (LookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
62 | }
|
---|
63 | /// <summary>
|
---|
64 | /// The parameter describing how much the mutation should depend on the progress towards the maximum generation.
|
---|
65 | /// </summary>
|
---|
66 | public ValueLookupParameter<DoubleValue> GenerationDependencyParameter {
|
---|
67 | get { return (ValueLookupParameter<DoubleValue>)Parameters["GenerationDependency"]; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Initializes a new instance of <see cref="MichalewiczNonUniformOnePositionManipulator"/> with five
|
---|
72 | /// parameters (<c>Minimum</c>, <c>Maximum</c>, <c>CurrentGeneration</c>, <c>MaximumGenerations</c>
|
---|
73 | /// and <c>GenerationDependency</c>).
|
---|
74 | /// </summary>
|
---|
75 | public MichalewiczNonUniformOnePositionManipulator()
|
---|
76 | : base() {
|
---|
77 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "Minimum of the sampling range for the vector element (included)"));
|
---|
78 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "Maximum of the sampling range for the vector element (excluded)"));
|
---|
79 | Parameters.Add(new LookupParameter<IntValue>("Generation", "Current generation of the algorithm"));
|
---|
80 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "Maximum number of generations"));
|
---|
81 | Parameters.Add(new ValueLookupParameter<DoubleValue>("GenerationDependency", "Specifies the degree of dependency on the number of generations", new DoubleValue(5)));
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Performs a non uniformly distributed one position manipulation on the given
|
---|
86 | /// real <paramref name="vector"/>. The probability of stronger mutations reduces the more <see cref="currentGeneration"/> approaches <see cref="maximumGenerations"/>.
|
---|
87 | /// </summary>
|
---|
88 | /// <exception cref="ArgumentException">Thrown when <paramref name="currentGeneration"/> is greater than <paramref name="maximumGenerations"/>.</exception>
|
---|
89 | /// <param name="random">The random number generator.</param>
|
---|
90 | /// <param name="vector">The real vector to manipulate.</param>
|
---|
91 | /// <param name="min">The minimum value of the sampling range for the vector element (inclusive).</param>
|
---|
92 | /// <param name="max">The maximum value of the sampling range for the vector element (exclusive).</param>
|
---|
93 | /// <param name="currentGeneration">The current generation of the algorithm.</param>
|
---|
94 | /// <param name="maximumGenerations">Maximum number of generations.</param>
|
---|
95 | /// <param name="generationsDependency">Specifies the degree of dependency on the number of generations.</param>
|
---|
96 | /// <returns>The manipulated real vector.</returns>
|
---|
97 | public static void Apply(IRandom random, DoubleArray vector, DoubleValue min, DoubleValue max, IntValue currentGeneration, IntValue maximumGenerations, DoubleValue generationsDependency) {
|
---|
98 | if (currentGeneration.Value > maximumGenerations.Value) throw new ArgumentException("MichalewiczNonUniformOnePositionManipulator: CurrentGeneration must be smaller or equal than MaximumGeneration", "currentGeneration");
|
---|
99 | int length = vector.Length;
|
---|
100 | int index = random.Next(length);
|
---|
101 |
|
---|
102 | double prob = (1 - Math.Pow(random.NextDouble(), Math.Pow(1 - currentGeneration.Value / maximumGenerations.Value, generationsDependency.Value)));
|
---|
103 |
|
---|
104 | if (random.NextDouble() < 0.5) {
|
---|
105 | vector[index] = vector[index] + (max.Value - vector[index]) * prob;
|
---|
106 | } else {
|
---|
107 | vector[index] = vector[index] - (vector[index] - min.Value) * prob;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /// <summary>
|
---|
112 | /// Checks if all parameters are available and forwards the call to <see cref="Apply(IRandom, DoubleArray, DoubleValue, DoubleValue, IntValue, IntValue, DoubleValue)"/>.
|
---|
113 | /// </summary>
|
---|
114 | /// <param name="random">The random number generator.</param>
|
---|
115 | /// <param name="realVector">The real vector that should be manipulated.</param>
|
---|
116 | protected override void Manipulate(IRandom random, DoubleArray realVector) {
|
---|
117 | if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found.");
|
---|
118 | if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found.");
|
---|
119 | if (GenerationParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + GenerationParameter.ActualName + " could not be found.");
|
---|
120 | if (MaximumGenerationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MaximumGenerationsParameter.ActualName + " could not be found.");
|
---|
121 | if (GenerationDependencyParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + GenerationDependencyParameter.ActualName + " could not be found.");
|
---|
122 | Apply(random, realVector, MinimumParameter.ActualValue, MaximumParameter.ActualValue, GenerationParameter.ActualValue, MaximumGenerationsParameter.ActualValue, GenerationDependencyParameter.ActualValue);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|