[2900] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2900] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[2969] | 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2900] | 28 |
|
---|
[3053] | 29 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[2900] | 30 | /// <summary>
|
---|
[2969] | 31 | /// 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).
|
---|
| 32 | /// Note that the interval is not uniformly sampled, but smaller values are sampled more often.
|
---|
[2900] | 33 | /// </summary>
|
---|
[2969] | 34 | /// <remarks>
|
---|
| 35 | /// 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/>
|
---|
| 36 | /// </remarks>
|
---|
| 37 | [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.")]
|
---|
[3017] | 38 | [StorableClass]
|
---|
[2969] | 39 | public class BreederGeneticAlgorithmManipulator : RealVectorManipulator {
|
---|
| 40 | 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 };
|
---|
[3048] | 41 | public ValueLookupParameter<DoubleValue> SearchIntervalFactorParameter {
|
---|
| 42 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SearchIntervalFactor"]; }
|
---|
[2969] | 43 | }
|
---|
[4722] | 44 |
|
---|
| 45 | [StorableConstructor]
|
---|
| 46 | protected BreederGeneticAlgorithmManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 47 | protected BreederGeneticAlgorithmManipulator(BreederGeneticAlgorithmManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
[2900] | 48 | /// <summary>
|
---|
[3123] | 49 | /// Initializes a new instance of <see cref="BreederGeneticAlgorithmManipulator"/> with two
|
---|
| 50 | /// parameters (<c>Bounds</c> and <c>SearchIntervalFactor</c>).
|
---|
[2900] | 51 | /// </summary>
|
---|
| 52 | public BreederGeneticAlgorithmManipulator()
|
---|
| 53 | : base() {
|
---|
[6629] | 54 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SearchIntervalFactor", @"Scales the manipulation strength as a factor of the range. The range is determined by the bounds interval.
|
---|
| 55 | A value of 0.1 means that certain components of the vector are moved by values in the non-uniform interval [0;0.1*range].", new DoubleValue(0.1)));
|
---|
[2900] | 56 | }
|
---|
| 57 |
|
---|
[4722] | 58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 59 | return new BreederGeneticAlgorithmManipulator(this, cloner);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[2900] | 62 | /// <summary>
|
---|
[3026] | 63 | /// Performs a breeder genetic algorithm manipulation on the given <paramref name="vector"/>.
|
---|
[2900] | 64 | /// </summary>
|
---|
| 65 | /// <param name="random">A random number generator.</param>
|
---|
| 66 | /// <param name="vector">The real vector to manipulate.</param>
|
---|
[3123] | 67 | /// <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] | 68 | /// <param name="searchIntervalFactor">The factor determining the size of the search interval.</param>
|
---|
[3123] | 69 | public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, DoubleValue searchIntervalFactor) {
|
---|
[2900] | 70 | int length = vector.Length;
|
---|
[2969] | 71 | double prob, value;
|
---|
| 72 | do {
|
---|
| 73 | value = Sigma(random);
|
---|
| 74 | } while (value == 0);
|
---|
[2900] | 75 |
|
---|
[2969] | 76 | prob = 1.0 / (double)length;
|
---|
| 77 | bool wasMutated = false;
|
---|
| 78 |
|
---|
| 79 | for (int i = 0; i < length; i++) {
|
---|
| 80 | if (random.NextDouble() < prob) {
|
---|
[3123] | 81 | double range = bounds[i % bounds.Rows, 1] - bounds[i % bounds.Rows, 0];
|
---|
[2969] | 82 | if (random.NextDouble() < 0.5) {
|
---|
[3123] | 83 | vector[i] = vector[i] + value * searchIntervalFactor.Value * range;
|
---|
[2969] | 84 | } else {
|
---|
[3123] | 85 | vector[i] = vector[i] - value * searchIntervalFactor.Value * range;
|
---|
[2969] | 86 | }
|
---|
| 87 | wasMutated = true;
|
---|
| 88 | }
|
---|
[2900] | 89 | }
|
---|
| 90 |
|
---|
[2969] | 91 | // make sure at least one gene was mutated
|
---|
| 92 | if (!wasMutated) {
|
---|
| 93 | int pos = random.Next(length);
|
---|
[3123] | 94 | double range = bounds[pos % bounds.Rows, 1] - bounds[pos % bounds.Rows, 0];
|
---|
[2969] | 95 | if (random.NextDouble() < 0.5) {
|
---|
[3123] | 96 | vector[pos] = vector[pos] + value * searchIntervalFactor.Value * range;
|
---|
[2969] | 97 | } else {
|
---|
[3123] | 98 | vector[pos] = vector[pos] - value * searchIntervalFactor.Value * range;
|
---|
[2969] | 99 | }
|
---|
| 100 | }
|
---|
[2900] | 101 | }
|
---|
| 102 |
|
---|
| 103 | private static double Sigma(IRandom random) {
|
---|
| 104 | double sigma = 0;
|
---|
| 105 | int limit = 16;
|
---|
| 106 |
|
---|
| 107 | for (int i = 0; i < limit; i++) {
|
---|
| 108 | if (random.Next(limit) == 15) {
|
---|
| 109 | // execute this statement with a probability of 1/16
|
---|
[2969] | 110 | sigma += powerOfTwo[i];
|
---|
[2900] | 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | return sigma;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /// <summary>
|
---|
[3123] | 118 | /// Checks the parameters Bounds, and SearchIntervalFactor and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue, DoubleValue)"/>.
|
---|
[2900] | 119 | /// </summary>
|
---|
[2969] | 120 | /// <param name="random">A random number generator.</param>
|
---|
| 121 | /// <param name="realVector">The real vector to manipulate.</param>
|
---|
[3060] | 122 | protected override void Manipulate(IRandom random, RealVector realVector) {
|
---|
[3123] | 123 | if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
|
---|
[2969] | 124 | if (SearchIntervalFactorParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Paraemter " + SearchIntervalFactorParameter.ActualName + " could not be found.");
|
---|
[3123] | 125 | Apply(random, realVector, BoundsParameter.ActualValue, SearchIntervalFactorParameter.ActualValue);
|
---|
[2900] | 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|