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 | using HeuristicLab.Random;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
32 | /// <summary>
|
---|
33 | /// Manipulates each dimension in the real vector with the mutation strength given
|
---|
34 | /// in the strategy parameter vector.
|
---|
35 | /// </summary>
|
---|
36 | /// <remarks>
|
---|
37 | /// It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.<br/>
|
---|
38 | /// The strategy vector can be of smaller length than the solution vector, in which case values are taken from the beginning again once the end of the strategy vector is reached.
|
---|
39 | /// </remarks>
|
---|
40 | [Item("NormalAllPositionsManipulator", "This manipulation operator adds a value sigma_i * N(0,1) to the current value in each position i. The values for sigma_i are taken from the strategy vector, if there are less elements in the strategy vector than positions, then the strategy vector is cycled. It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.")]
|
---|
41 | [StorableClass]
|
---|
42 | public class NormalAllPositionsManipulator : RealVectorManipulator, ISelfAdaptiveManipulator {
|
---|
43 | public Type StrategyParameterType {
|
---|
44 | get { return typeof(IRealVectorStdDevStrategyParameterOperator); }
|
---|
45 | }
|
---|
46 | /// <summary>
|
---|
47 | /// Parameter for the strategy vector.
|
---|
48 | /// </summary>
|
---|
49 | public IValueLookupParameter<RealVector> StrategyParameterParameter {
|
---|
50 | get { return (IValueLookupParameter<RealVector>)Parameters["StrategyParameter"]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | IParameter ISelfAdaptiveManipulator.StrategyParameterParameter {
|
---|
54 | get { return StrategyParameterParameter; }
|
---|
55 | }
|
---|
56 | /// <summary>
|
---|
57 | /// Initializes a new instance of <see cref="NormalAllPositionsManipulator"/> with one
|
---|
58 | /// parameter (<c>StrategyVector</c>).
|
---|
59 | /// </summary>
|
---|
60 | public NormalAllPositionsManipulator()
|
---|
61 | : base() {
|
---|
62 | Parameters.Add(new ValueLookupParameter<RealVector>("StrategyParameter", "The vector containing the endogenous strategy parameters."));
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// Performs an adaptive normally distributed all position manipulation on the given
|
---|
67 | /// <paramref name="vector"/>.
|
---|
68 | /// </summary>
|
---|
69 | /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
|
---|
70 | /// as long as the vector to get manipulated.</exception>
|
---|
71 | /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param>
|
---|
72 | /// <param name="random">A random number generator.</param>
|
---|
73 | /// <param name="vector">The real vector to manipulate.</param>
|
---|
74 | /// <returns>The manipulated real vector.</returns>
|
---|
75 | public static void Apply(IRandom random, RealVector vector, RealVector strategyParameters) {
|
---|
76 | NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
|
---|
77 | if (strategyParameters != null && strategyParameters.Length > 0) {
|
---|
78 | for (int i = 0; i < vector.Length; i++) {
|
---|
79 | vector[i] = vector[i] + (N.NextDouble() * strategyParameters[i % strategyParameters.Length]);
|
---|
80 | }
|
---|
81 | } else {
|
---|
82 | for (int i = 0; i < vector.Length; i++) {
|
---|
83 | vector[i] = vector[i] + N.NextDouble();
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /// <summary>
|
---|
89 | /// Checks that the strategy vector is not null and forwards the call to <see cref="Apply(IRandom, RealVector, RealVector)"/>.
|
---|
90 | /// </summary>
|
---|
91 | /// <param name="random">The random number generator.</param>
|
---|
92 | /// <param name="realVector">The vector of real values that is manipulated.</param>
|
---|
93 | protected override void Manipulate(IRandom random, RealVector realVector) {
|
---|
94 | Apply(random, realVector, StrategyParameterParameter.ActualValue);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|