1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 | using System;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
33 | [Item("CovarianceMatrixMutator", "Mutates the solution vector according to the CMA-ES scheme.")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class CMAMutator : SingleSuccessorOperator, IStochasticOperator, IRealVectorOperator, ICMAESManipulator, IIterationBasedOperator {
|
---|
36 | private const int MaxTries = 1000;
|
---|
37 |
|
---|
38 | public Type CMAType {
|
---|
39 | get { return typeof(CMAParameters); }
|
---|
40 | }
|
---|
41 |
|
---|
42 | #region Parameter Properties
|
---|
43 | public ILookupParameter<IRandom> RandomParameter {
|
---|
44 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
48 | get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
52 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public ILookupParameter<RealVector> RealVectorParameter {
|
---|
56 | get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public IValueLookupParameter<DoubleMatrix> BoundsParameter {
|
---|
60 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public ILookupParameter<CMAParameters> StrategyParametersParameter {
|
---|
64 | get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
|
---|
65 | }
|
---|
66 | #endregion
|
---|
67 |
|
---|
68 | [StorableConstructor]
|
---|
69 | private CMAMutator(bool deserializing) : base(deserializing) { }
|
---|
70 | private CMAMutator(CMAMutator original, Cloner cloner) : base(original, cloner) { }
|
---|
71 | public CMAMutator()
|
---|
72 | : base() {
|
---|
73 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
74 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration that is being processed."));
|
---|
75 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations to be processed."));
|
---|
76 | Parameters.Add(new LookupParameter<RealVector>("RealVector", "The solution vector of real values."));
|
---|
77 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The bounds for the dimensions."));
|
---|
78 | Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The CMA-ES strategy parameters used for mutation."));
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new CMAMutator(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IOperation Apply() {
|
---|
86 | var random = RandomParameter.ActualValue;
|
---|
87 | var arx = RealVectorParameter.ActualValue;
|
---|
88 | var sp = StrategyParametersParameter.ActualValue;
|
---|
89 | var iterations = IterationsParameter.ActualValue.Value;
|
---|
90 | var initialIterations = sp.InitialIterations.Value;
|
---|
91 | var bounds = BoundsParameter.ActualValue;
|
---|
92 |
|
---|
93 | var nd = new NormalDistributedRandom(random, 0, 1);
|
---|
94 |
|
---|
95 | var copy = (RealVector)arx.Clone();
|
---|
96 | int tries;
|
---|
97 | if (initialIterations > iterations) {
|
---|
98 | for (int i = 0; i < arx.Length; i++) {
|
---|
99 | tries = 0;
|
---|
100 | do {
|
---|
101 | tries++;
|
---|
102 | arx[i] = copy[i] + sp.Sigma.Value * sp.D[i] * nd.NextDouble();
|
---|
103 | } while ((bounds[i % bounds.Rows, 0] > arx[i] || arx[i] > bounds[i % bounds.Rows, 1]) && tries < MaxTries);
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 | bool inRange;
|
---|
107 | var B = sp.B;
|
---|
108 | tries = 0;
|
---|
109 | do {
|
---|
110 | tries++;
|
---|
111 | inRange = true;
|
---|
112 | var artmp = new double[arx.Length];
|
---|
113 | for (int i = 0; i < arx.Length; ++i)
|
---|
114 | artmp[i] = sp.D[i] * nd.NextDouble();
|
---|
115 |
|
---|
116 | for (int i = 0; i < arx.Length; i++) {
|
---|
117 | var sum = 0.0;
|
---|
118 | for (int j = 0; j < arx.Length; j++)
|
---|
119 | sum += B[i, j] * artmp[j];
|
---|
120 | arx[i] = copy[i] + sp.Sigma.Value * sum; // m + sig * Normal(0,C)
|
---|
121 | if (bounds[i % bounds.Rows, 0] > arx[i] || arx[i] > bounds[i % bounds.Rows, 1])
|
---|
122 | inRange = false;
|
---|
123 | }
|
---|
124 | } while (!inRange && tries < MaxTries);
|
---|
125 | }
|
---|
126 | return base.Apply();
|
---|
127 | }
|
---|
128 | }
|
---|
129 | } |
---|