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.Encodings.RealVectorEncoding;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 | using System;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
|
---|
34 | [Item("CMAMutator", "Mutates the solution vector according to the CMA-ES scheme.")]
|
---|
35 | [StorableClass]
|
---|
36 | public sealed class CMAMutator : SingleSuccessorOperator, IStochasticOperator, ICMAManipulator, IIterationBasedOperator {
|
---|
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> PopulationSizeParameter {
|
---|
48 | get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
52 | get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
56 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public ILookupParameter<RealVector> MeanParameter {
|
---|
60 | get { return (ILookupParameter<RealVector>)Parameters["Mean"]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
|
---|
64 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public IValueLookupParameter<DoubleMatrix> BoundsParameter {
|
---|
68 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public ILookupParameter<CMAParameters> StrategyParametersParameter {
|
---|
72 | get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public IValueParameter<IntValue> MaxTriesParameter {
|
---|
76 | get { return (IValueParameter<IntValue>)Parameters["MaxTries"]; }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | [StorableConstructor]
|
---|
81 | private CMAMutator(bool deserializing) : base(deserializing) { }
|
---|
82 | private CMAMutator(CMAMutator original, Cloner cloner) : base(original, cloner) { }
|
---|
83 | public CMAMutator()
|
---|
84 | : base() {
|
---|
85 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
86 | Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The population size (lambda) determines how many offspring should be created."));
|
---|
87 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration that is being processed."));
|
---|
88 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations to be processed."));
|
---|
89 | Parameters.Add(new LookupParameter<RealVector>("Mean", "The current mean solution."));
|
---|
90 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The solution vector of real values."));
|
---|
91 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The bounds for the dimensions."));
|
---|
92 | Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The CMA-ES strategy parameters used for mutation."));
|
---|
93 | Parameters.Add(new ValueParameter<IntValue>("MaxTries", "The maximum number of tries a mutation should be performed if it was outside the bounds.", new IntValue(1000)));
|
---|
94 | }
|
---|
95 |
|
---|
96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
97 | return new CMAMutator(this, cloner);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override IOperation Apply() {
|
---|
101 | var maxTries = MaxTriesParameter.Value.Value;
|
---|
102 | var random = RandomParameter.ActualValue;
|
---|
103 | var lambda = PopulationSizeParameter.ActualValue.Value;
|
---|
104 | var xmean = MeanParameter.ActualValue;
|
---|
105 | var arx = RealVectorParameter.ActualValue;
|
---|
106 | var sp = StrategyParametersParameter.ActualValue;
|
---|
107 | var iterations = IterationsParameter.ActualValue.Value;
|
---|
108 | var initialIterations = sp.InitialIterations;
|
---|
109 | var bounds = BoundsParameter.ActualValue;
|
---|
110 |
|
---|
111 | if (arx == null || arx.Length == 0) {
|
---|
112 | arx = new ItemArray<RealVector>(lambda);
|
---|
113 | for (int i = 0; i < lambda; i++) arx[i] = new RealVector(xmean.Length);
|
---|
114 | RealVectorParameter.ActualValue = arx;
|
---|
115 | }
|
---|
116 | var nd = new NormalDistributedRandom(random, 0, 1);
|
---|
117 |
|
---|
118 | for (int i = 0; i < lambda; i++) {
|
---|
119 | int tries = 0;
|
---|
120 | bool inRange;
|
---|
121 | if (initialIterations > iterations) {
|
---|
122 | for (int k = 0; k < arx[i].Length; k++) {
|
---|
123 | do {
|
---|
124 | arx[i][k] = xmean[k] + sp.Sigma * sp.D[k] * nd.NextDouble();
|
---|
125 | inRange = bounds[k % bounds.Rows, 0] <= arx[i][k] && arx[i][k] <= bounds[k % bounds.Rows, 1];
|
---|
126 | if (!inRange) tries++;
|
---|
127 | } while (!inRange && tries < maxTries);
|
---|
128 | if (!inRange && maxTries > 1) {
|
---|
129 | if (bounds[k % bounds.Rows, 0] > arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 0];
|
---|
130 | else if (bounds[k % bounds.Rows, 1] < arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 1];
|
---|
131 | }
|
---|
132 | }
|
---|
133 | } else {
|
---|
134 | var B = sp.B;
|
---|
135 | do {
|
---|
136 | tries++;
|
---|
137 | inRange = true;
|
---|
138 | var artmp = new double[arx[0].Length];
|
---|
139 | for (int k = 0; k < arx[0].Length; ++k) {
|
---|
140 | artmp[k] = sp.D[k] * nd.NextDouble();
|
---|
141 | }
|
---|
142 |
|
---|
143 | for (int k = 0; k < arx[0].Length; k++) {
|
---|
144 | var sum = 0.0;
|
---|
145 | for (int j = 0; j < arx[0].Length; j++)
|
---|
146 | sum += B[k, j] * artmp[j];
|
---|
147 | arx[i][k] = xmean[k] + sp.Sigma * sum; // m + sig * Normal(0,C)
|
---|
148 | if (bounds[k % bounds.Rows, 0] > arx[i][k] || arx[i][k] > bounds[k % bounds.Rows, 1])
|
---|
149 | inRange = false;
|
---|
150 | }
|
---|
151 | } while (!inRange && tries < maxTries);
|
---|
152 | if (!inRange && maxTries > 1) {
|
---|
153 | for (int k = 0; k < arx[0].Length; k++) {
|
---|
154 | if (bounds[k % bounds.Rows, 0] > arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 0];
|
---|
155 | else if (bounds[k % bounds.Rows, 1] < arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 1];
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 | return base.Apply();
|
---|
161 | }
|
---|
162 | }
|
---|
163 | } |
---|