[9129] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9129] | 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 |
|
---|
[9303] | 22 | using System;
|
---|
[9129] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[16565] | 30 | using HEAL.Attic;
|
---|
[9291] | 31 | using HeuristicLab.Random;
|
---|
[9129] | 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
|
---|
| 34 | [Item("CMAMutator", "Mutates the solution vector according to the CMA-ES scheme.")]
|
---|
[16565] | 35 | [StorableType("13539C1A-1D94-4CD7-8514-2173C661333D")]
|
---|
[9129] | 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 |
|
---|
[9709] | 75 | public IFixedValueParameter<IntValue> MaxTriesParameter {
|
---|
| 76 | get { return (IFixedValueParameter<IntValue>)Parameters["MaxTries"]; }
|
---|
[9129] | 77 | }
|
---|
[9709] | 78 |
|
---|
| 79 | public IFixedValueParameter<BoolValue> TruncateAtBoundsParameter {
|
---|
| 80 | get { return (IFixedValueParameter<BoolValue>)Parameters["TruncateAtBounds"]; }
|
---|
| 81 | }
|
---|
[9129] | 82 | #endregion
|
---|
| 83 |
|
---|
| 84 | [StorableConstructor]
|
---|
[16565] | 85 | private CMAMutator(StorableConstructorFlag _) : base(_) { }
|
---|
[9129] | 86 | private CMAMutator(CMAMutator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 87 | public CMAMutator()
|
---|
| 88 | : base() {
|
---|
| 89 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
| 90 | Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The population size (lambda) determines how many offspring should be created."));
|
---|
| 91 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration that is being processed."));
|
---|
| 92 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations to be processed."));
|
---|
| 93 | Parameters.Add(new LookupParameter<RealVector>("Mean", "The current mean solution."));
|
---|
| 94 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The solution vector of real values."));
|
---|
| 95 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The bounds for the dimensions."));
|
---|
| 96 | Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The CMA-ES strategy parameters used for mutation."));
|
---|
[9709] | 97 | Parameters.Add(new FixedValueParameter<IntValue>("MaxTries", "The maximum number of tries a mutation should be performed if it was outside the bounds.", new IntValue(100)));
|
---|
| 98 | Parameters.Add(new FixedValueParameter<BoolValue>("TruncateAtBounds", "Whether the point should be truncated at the bounds if none of the tries resulted in a point within the bounds.", new BoolValue(true)));
|
---|
[9129] | 99 | }
|
---|
| 100 |
|
---|
| 101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 102 | return new CMAMutator(this, cloner);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | public override IOperation Apply() {
|
---|
| 106 | var maxTries = MaxTriesParameter.Value.Value;
|
---|
[9709] | 107 | var truncateAtBounds = TruncateAtBoundsParameter.Value.Value;
|
---|
[9129] | 108 | var random = RandomParameter.ActualValue;
|
---|
| 109 | var lambda = PopulationSizeParameter.ActualValue.Value;
|
---|
| 110 | var xmean = MeanParameter.ActualValue;
|
---|
| 111 | var arx = RealVectorParameter.ActualValue;
|
---|
| 112 | var sp = StrategyParametersParameter.ActualValue;
|
---|
| 113 | var iterations = IterationsParameter.ActualValue.Value;
|
---|
[9297] | 114 | var initialIterations = sp.InitialIterations;
|
---|
[9129] | 115 | var bounds = BoundsParameter.ActualValue;
|
---|
| 116 |
|
---|
| 117 | if (arx == null || arx.Length == 0) {
|
---|
| 118 | arx = new ItemArray<RealVector>(lambda);
|
---|
| 119 | for (int i = 0; i < lambda; i++) arx[i] = new RealVector(xmean.Length);
|
---|
| 120 | RealVectorParameter.ActualValue = arx;
|
---|
| 121 | }
|
---|
[9291] | 122 | var nd = new NormalDistributedRandom(random, 0, 1);
|
---|
[9129] | 123 |
|
---|
[9303] | 124 | var length = arx[0].Length;
|
---|
| 125 |
|
---|
[9129] | 126 | for (int i = 0; i < lambda; i++) {
|
---|
| 127 | int tries = 0;
|
---|
[9199] | 128 | bool inRange;
|
---|
[9129] | 129 | if (initialIterations > iterations) {
|
---|
[9303] | 130 | for (int k = 0; k < length; k++) {
|
---|
[9129] | 131 | do {
|
---|
[9297] | 132 | arx[i][k] = xmean[k] + sp.Sigma * sp.D[k] * nd.NextDouble();
|
---|
[9245] | 133 | inRange = bounds[k % bounds.Rows, 0] <= arx[i][k] && arx[i][k] <= bounds[k % bounds.Rows, 1];
|
---|
[9199] | 134 | if (!inRange) tries++;
|
---|
| 135 | } while (!inRange && tries < maxTries);
|
---|
[9709] | 136 | if (!inRange && truncateAtBounds) {
|
---|
[9199] | 137 | if (bounds[k % bounds.Rows, 0] > arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 0];
|
---|
[9244] | 138 | else if (bounds[k % bounds.Rows, 1] < arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 1];
|
---|
[9199] | 139 | }
|
---|
[9129] | 140 | }
|
---|
| 141 | } else {
|
---|
| 142 | var B = sp.B;
|
---|
| 143 | do {
|
---|
| 144 | tries++;
|
---|
| 145 | inRange = true;
|
---|
[9303] | 146 | var artmp = new double[length];
|
---|
| 147 | for (int k = 0; k < length; ++k) {
|
---|
[9297] | 148 | artmp[k] = sp.D[k] * nd.NextDouble();
|
---|
[9148] | 149 | }
|
---|
[9129] | 150 |
|
---|
[9303] | 151 | for (int k = 0; k < length; k++) {
|
---|
[9129] | 152 | var sum = 0.0;
|
---|
[9303] | 153 | for (int j = 0; j < length; j++)
|
---|
[9129] | 154 | sum += B[k, j] * artmp[j];
|
---|
[9297] | 155 | arx[i][k] = xmean[k] + sp.Sigma * sum; // m + sig * Normal(0,C)
|
---|
[9129] | 156 | if (bounds[k % bounds.Rows, 0] > arx[i][k] || arx[i][k] > bounds[k % bounds.Rows, 1])
|
---|
| 157 | inRange = false;
|
---|
| 158 | }
|
---|
| 159 | } while (!inRange && tries < maxTries);
|
---|
[9709] | 160 | if (!inRange && truncateAtBounds) {
|
---|
[9303] | 161 | for (int k = 0; k < length; k++) {
|
---|
[9199] | 162 | if (bounds[k % bounds.Rows, 0] > arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 0];
|
---|
[9244] | 163 | else if (bounds[k % bounds.Rows, 1] < arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 1];
|
---|
[9199] | 164 | }
|
---|
| 165 | }
|
---|
[9129] | 166 | }
|
---|
| 167 | }
|
---|
| 168 | return base.Apply();
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | } |
---|