1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.RealVector {
|
---|
29 | /// <summary>
|
---|
30 | /// Michalewicz, Z. (1992). Genetic Algorithms + Data Structures = Evolution Programs <br/>
|
---|
31 | /// Non-uniformly distributed change of all positions of a real vector.
|
---|
32 | /// </summary>
|
---|
33 | public class MichalewiczNonUniformAllPositionsManipulator : RealVectorManipulatorBase {
|
---|
34 | /// <inheritdoc select="summary"/>
|
---|
35 | public override string Description {
|
---|
36 | get { return
|
---|
37 | @"Non-uniformly distributed change of all positions of a real vector (Michalewicz 1992)
|
---|
38 | Initially, the space will be searched uniformly and very locally at later stages. This increases the probability of generating the new number closer to its successor instead of a random number.
|
---|
39 |
|
---|
40 | Michalewicz, Z. (1992). Genetic Algorithms + Data Structures = Evolution Programs. Springer Verlag.";
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Initializes a new instance of <see cref="MichalewiczNonUniformAllPositionsManipulator"/> with
|
---|
46 | /// five variable infos (<c>Minimum</c>, <c>Maximum</c>, <c>CurrentGeneration</c>,
|
---|
47 | /// <c>MaximumGenerations</c> and <c>GenerationsDependency</c>).
|
---|
48 | /// </summary>
|
---|
49 | public MichalewiczNonUniformAllPositionsManipulator()
|
---|
50 | : base() {
|
---|
51 | AddVariableInfo(new VariableInfo("Minimum", "Minimum of the sampling range for the vector element (included)", typeof(DoubleData), VariableKind.In));
|
---|
52 | AddVariableInfo(new VariableInfo("Maximum", "Maximum of the sampling range for the vector element (excluded)", typeof(DoubleData), VariableKind.In));
|
---|
53 | AddVariableInfo(new VariableInfo("CurrentGeneration", "Current Generation of the algorithm", typeof(IntData), VariableKind.In));
|
---|
54 | AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of Generations", typeof(IntData), VariableKind.In));
|
---|
55 | VariableInfo genDepInfo = new VariableInfo("GenerationsDependency", "Specifies the degree of dependency on the number of generations", typeof(IntData), VariableKind.In);
|
---|
56 | genDepInfo.Local = true;
|
---|
57 | AddVariableInfo(genDepInfo);
|
---|
58 | AddVariable(new Variable("GenerationsDependency", new IntData(5)));
|
---|
59 | }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Performs a non uniformly distributed all position manipulation on the given
|
---|
63 | /// real <paramref name="vector"/>, published by Z. Michalewicz, 1992.
|
---|
64 | /// </summary>
|
---|
65 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
66 | /// <param name="scope">The current scope.</param>
|
---|
67 | /// <param name="random">The random number generator.</param>
|
---|
68 | /// <param name="vector">The real vector to manipulate.</param>
|
---|
69 | /// <returns>The manipulated real vector.</returns>
|
---|
70 | protected override double[] Manipulate(IScope scope, IRandom random, double[] vector) {
|
---|
71 | double min = GetVariableValue<DoubleData>("Minimum", scope, true).Data;
|
---|
72 | double max = GetVariableValue<DoubleData>("Maximum", scope, true).Data;
|
---|
73 | int currentGeneration = GetVariableValue<IntData>("CurrentGeneration", scope, true).Data;
|
---|
74 | int maximumGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true).Data;
|
---|
75 | int generationsDependency = GetVariableValue<IntData>("GenerationsDependency", scope, true).Data;
|
---|
76 | return Apply(random, vector, min, max, currentGeneration, maximumGenerations, generationsDependency);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Performs a non uniformly distributed all position manipulation on the given
|
---|
81 | /// real <paramref name="vector"/>, published by Z. Michalewicz, 1992.
|
---|
82 | /// </summary>
|
---|
83 | /// <param name="random">The random number generator.</param>
|
---|
84 | /// <param name="vector">The real vector to manipulate.</param>
|
---|
85 | /// <param name="min">The minimum value of the sampling range for the vector element (inclusive).</param>
|
---|
86 | /// <param name="max">The maximum value of the sampling range for the vector element (exclusive).</param>
|
---|
87 | /// <param name="currentGeneration">The current generation of the algorithm.</param>
|
---|
88 | /// <param name="maximumGenerations">Maximum number of generations.</param>
|
---|
89 | /// <param name="generationsDependency">Specifies the degree of dependency on the number of generations.</param>
|
---|
90 | /// <returns>The manipulated real vector.</returns>
|
---|
91 | public static double[] Apply(IRandom random, double[] vector, double min, double max, int currentGeneration, int maximumGenerations, int generationsDependency) {
|
---|
92 | int length = vector.Length;
|
---|
93 | double[] result = new double[length];
|
---|
94 |
|
---|
95 | for (int i = 0; i < length; i++) {
|
---|
96 | if (random.NextDouble() < 0.5) {
|
---|
97 | vector[i] = vector[i] + Delta(random, currentGeneration, max - vector[i], maximumGenerations, generationsDependency);
|
---|
98 | } else {
|
---|
99 | vector[i] = vector[i] - Delta(random, currentGeneration, vector[i] - min, maximumGenerations, generationsDependency);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return vector;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // returns a value between 0 and y (both included)
|
---|
106 | private static double Delta(IRandom random, int currentGeneration, double y, int maximumGenerations, int generationsDependency) {
|
---|
107 | return y * (1 - Math.Pow(random.NextDouble(), Math.Pow(1 - currentGeneration / maximumGenerations, generationsDependency)));
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|