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.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
31 | /// <summary>
|
---|
32 | /// 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/>
|
---|
33 | /// 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.
|
---|
34 | /// </summary>
|
---|
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>
|
---|
38 | [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.")]
|
---|
39 | [StorableClass]
|
---|
40 | public class MichalewiczNonUniformOnePositionManipulator : RealVectorManipulator, IIterationBasedOperator {
|
---|
41 | /// <summary>
|
---|
42 | /// The current iteration.
|
---|
43 | /// </summary>
|
---|
44 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
45 | get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
46 | }
|
---|
47 | /// <summary>
|
---|
48 | /// The maximum iteration.
|
---|
49 | /// </summary>
|
---|
50 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
51 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
52 | }
|
---|
53 | /// <summary>
|
---|
54 | /// The parameter describing how much the mutation should depend on the progress towards the maximum iterations.
|
---|
55 | /// </summary>
|
---|
56 | public ValueLookupParameter<DoubleValue> IterationDependencyParameter {
|
---|
57 | get { return (ValueLookupParameter<DoubleValue>)Parameters["IterationDependency"]; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Initializes a new instance of <see cref="MichalewiczNonUniformOnePositionManipulator"/> with three
|
---|
62 | /// parameters (<c>Iterations</c>, <c>MaximumIterations</c> and <c>IterationDependency</c>).
|
---|
63 | /// </summary>
|
---|
64 | public MichalewiczNonUniformOnePositionManipulator()
|
---|
65 | : base() {
|
---|
66 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "Current iteration of the algorithm."));
|
---|
67 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Maximum number of iterations."));
|
---|
68 | 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)));
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Performs a non uniformly distributed one position manipulation on the given
|
---|
73 | /// real <paramref name="vector"/>. The probability of stronger mutations reduces the more <see cref="currentIteration"/> approaches <see cref="maximumIterations"/>.
|
---|
74 | /// </summary>
|
---|
75 | /// <exception cref="ArgumentException">Thrown when <paramref name="currentIteration"/> is greater than <paramref name="maximumIterations"/>.</exception>
|
---|
76 | /// <param name="random">The random number generator.</param>
|
---|
77 | /// <param name="vector">The real vector to manipulate.</param>
|
---|
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>
|
---|
79 | /// <param name="currentIteration">The current iteration of the algorithm.</param>
|
---|
80 | /// <param name="maximumIterations">Maximum number of iterations.</param>
|
---|
81 | /// <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>
|
---|
82 | /// <returns>The manipulated real vector.</returns>
|
---|
83 | public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, IntValue currentIteration, IntValue maximumIterations, DoubleValue iterationDependency) {
|
---|
84 | if (currentIteration.Value > maximumIterations.Value) throw new ArgumentException("MichalewiczNonUniformOnePositionManipulator: CurrentIteration must be smaller or equal than MaximumIterations", "currentIteration");
|
---|
85 | if (iterationDependency.Value < 0) throw new ArgumentException("MichalewiczNonUniformOnePositionManipulator: iterationDependency must be >= 0.", "iterationDependency");
|
---|
86 | int length = vector.Length;
|
---|
87 | int index = random.Next(length);
|
---|
88 |
|
---|
89 | double prob = (1 - Math.Pow(random.NextDouble(), Math.Pow(1 - currentIteration.Value / maximumIterations.Value, iterationDependency.Value)));
|
---|
90 |
|
---|
91 | double min = bounds[index % bounds.Rows, 0];
|
---|
92 | double max = bounds[index % bounds.Rows, 1];
|
---|
93 |
|
---|
94 | if (random.NextDouble() < 0.5) {
|
---|
95 | vector[index] = vector[index] + (max - vector[index]) * prob;
|
---|
96 | } else {
|
---|
97 | vector[index] = vector[index] - (vector[index] - min) * prob;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /// <summary>
|
---|
102 | /// Checks if all parameters are available and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue, IntValue, IntValue, DoubleValue)"/>.
|
---|
103 | /// </summary>
|
---|
104 | /// <param name="random">The random number generator.</param>
|
---|
105 | /// <param name="realVector">The real vector that should be manipulated.</param>
|
---|
106 | protected override void Manipulate(IRandom random, RealVector realVector) {
|
---|
107 | if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
|
---|
108 | if (IterationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + IterationsParameter.ActualName + " could not be found.");
|
---|
109 | if (MaximumIterationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MaximumIterationsParameter.ActualName + " could not be found.");
|
---|
110 | if (IterationDependencyParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + IterationDependencyParameter.ActualName + " could not be found.");
|
---|
111 | Apply(random, realVector, BoundsParameter.ActualValue, IterationsParameter.ActualValue, MaximumIterationsParameter.ActualValue, IterationDependencyParameter.ActualValue);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|