Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/BreederGeneticAlgorithmManipulator.cs @ 3051

Last change on this file since 3051 was 3048, checked in by swagner, 15 years ago

Renamed classes of HeuristicLab.Data (#909)

File size: 6.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.RealVector {
29  /// <summary>
30  /// Changes one position of a real vector by adding/substracting a value of the interval [(2^-15)*range;~2*range], where range is SearchIntervalFactor * (max - min).
31  /// Note that the interval is not uniformly sampled, but smaller values are sampled more often.
32  /// </summary>
33  /// <remarks>
34  /// It is implemented as described by Mühlenbein, H. and Schlierkamp-Voosen, D. 1993. Predictive Models for the Breeder Genetic Algorithm - I. Continuous Parameter Optimization. Evolutionary Computation, 1(1), pp. 25-49.<br/>
35  /// </remarks>
36  [Item("BreederGeneticAlgorithmManipulator", "It is implemented as described by Mühlenbein, H. and Schlierkamp-Voosen, D. 1993. Predictive Models for the Breeder Genetic Algorithm - I. Continuous Parameter Optimization. Evolutionary Computation, 1(1), pp. 25-49.")]
37  [StorableClass]
38  public class BreederGeneticAlgorithmManipulator : RealVectorManipulator {
39    private static readonly double[] powerOfTwo = new double[] { 1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, 0.001953125, 0.0009765625, 0.00048828125, 0.000244140625, 0.0001220703125, 0.00006103515625, 0.000030517578125 };
40    public ValueLookupParameter<DoubleValue> MinimumParameter {
41      get { return (ValueLookupParameter<DoubleValue>)Parameters["Minimum"]; }
42    }
43    public ValueLookupParameter<DoubleValue> MaximumParameter {
44      get { return (ValueLookupParameter<DoubleValue>)Parameters["Maximum"]; }
45    }
46    public ValueLookupParameter<DoubleValue> SearchIntervalFactorParameter {
47      get { return (ValueLookupParameter<DoubleValue>)Parameters["SearchIntervalFactor"]; }
48    }
49    /// <summary>
50    /// Initializes a new instance of <see cref="BreederGeneticAlgorithmManipulator"/> with three variable
51    /// infos (<c>Minimum</c>, <c>Maximum</c> and <c>SearchIntervalFactor</c>).
52    /// </summary>
53    public BreederGeneticAlgorithmManipulator()
54      : base() {
55      Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "The lower bound for each element in the vector."));
56      Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "The upper bound for each element in the vector."));
57      Parameters.Add(new ValueLookupParameter<DoubleValue>("SearchIntervalFactor", "The factor determining the size of the search interval, that will be added/removed to/from the allele selected for manipulation.", new DoubleValue(0.1)));
58    }
59
60    /// <summary>
61    /// Performs a breeder genetic algorithm manipulation on the given <paramref name="vector"/>.
62    /// </summary>
63    /// <param name="random">A random number generator.</param>
64    /// <param name="vector">The real vector to manipulate.</param>
65    /// <param name="min">The minimum number of the sampling range for the vector element (inclusive).</param>
66    /// <param name="max">The maximum number of the sampling range for the vector element (exclusive).</param>
67    /// <param name="searchIntervalFactor">The factor determining the size of the search interval.</param>
68    public static void Apply(IRandom random, DoubleArray vector, DoubleValue min, DoubleValue max, DoubleValue searchIntervalFactor) {
69      int length = vector.Length;
70      double prob, value;
71      do {
72        value = Sigma(random);
73      } while (value == 0);
74      value *= searchIntervalFactor.Value * (max.Value - min.Value);
75
76      prob = 1.0 / (double)length;
77      bool wasMutated = false;
78
79      for (int i = 0; i < length; i++) {
80        if (random.NextDouble() < prob) {
81          if (random.NextDouble() < 0.5) {
82            vector[i] = vector[i] + value;
83          } else {
84            vector[i] = vector[i] - value;
85          }
86          wasMutated = true;
87        }
88      }
89
90      // make sure at least one gene was mutated
91      if (!wasMutated) {
92        int pos = random.Next(length);
93        if (random.NextDouble() < 0.5) {
94          vector[pos] = vector[pos] + value;
95        } else {
96          vector[pos] = vector[pos] - value;
97        }
98      }
99    }
100
101    private static double Sigma(IRandom random) {
102      double sigma = 0;
103      int limit = 16;
104
105      for (int i = 0; i < limit; i++) {
106        if (random.Next(limit) == 15) {
107          // execute this statement with a probability of 1/16
108          sigma += powerOfTwo[i];
109        }
110      }
111
112      return sigma;
113    }
114
115    /// <summary>
116    /// Checks the parameters Minimum, Maximum, and SearchIntervalFactor and forwards the call to <see cref="Apply(IRandom, DoubleArray, DoubleValue, DoubleValue, DoubleValue)"/>.
117    /// </summary>
118    /// <param name="random">A random number generator.</param>
119    /// <param name="realVector">The real vector to manipulate.</param>
120    protected override void Manipulate(IRandom random, DoubleArray realVector) {
121      if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Parameter " + MinimumParameter.ActualName + " could not be found.");
122      if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Paraemter " + MaximumParameter.ActualName + " could not be found.");
123      if (SearchIntervalFactorParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Paraemter " + SearchIntervalFactorParameter.ActualName + " could not be found.");
124      Apply(random, realVector, MinimumParameter.ActualValue, MaximumParameter.ActualValue, SearchIntervalFactorParameter.ActualValue);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.