Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CMAES/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAOperators/CMARecombinator.cs @ 9291

Last change on this file since 9291 was 9129, checked in by abeham, 12 years ago

#1961:

  • Changed CMA-ES to reference RealVector directly and work with that encoding only (separation of algorithm and encoding was not trivial to achieve)
  • Simplified operator graph (also improves performance)
  • Simplified strategy parameters (those used only during update are initialized in update)
  • Added many termination criteria (same as in Hansen's Java version)
  • Added different ways to calculate the mean (different weightings)
File size: 3.6 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.RealVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System;
30
31namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
32  [Item("CMARecombinator", "Base class that calculates the weighted mean of a number of offspring.")]
33  [StorableClass]
34  public abstract class CMARecombinator : SingleSuccessorOperator, ICMARecombinator {
35
36    public Type CMAType {
37      get { return typeof(CMAParameters); }
38    }
39
40    #region Parameter Properties
41    public IScopeTreeLookupParameter<RealVector> OffspringParameter {
42      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Offspring"]; }
43    }
44
45    public ILookupParameter<RealVector> MeanParameter {
46      get { return (ILookupParameter<RealVector>)Parameters["Mean"]; }
47    }
48
49    public ILookupParameter<RealVector> OldMeanParameter {
50      get { return (ILookupParameter<RealVector>)Parameters["OldMean"]; }
51    }
52
53    public ILookupParameter<CMAParameters> StrategyParametersParameter {
54      get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
55    }
56    #endregion
57
58    [StorableConstructor]
59    protected CMARecombinator(bool deserializing) : base(deserializing) { }
60    protected CMARecombinator(CMARecombinator original, Cloner cloner) : base(original, cloner) { }
61    protected CMARecombinator()
62      : base() {
63      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Offspring", "The offspring that should be recombined."));
64      Parameters.Add(new LookupParameter<RealVector>("Mean", "The new mean solution."));
65      Parameters.Add(new LookupParameter<RealVector>("OldMean", "The old mean solution."));
66      Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The CMA-ES strategy parameters used for mutation."));
67      OffspringParameter.ActualName = "RealVector";
68      MeanParameter.ActualName = "XMean";
69      OldMeanParameter.ActualName = "XOld";
70    }
71
72    public override IOperation Apply() {
73      var sp = StrategyParametersParameter.ActualValue;
74      if (sp.Weights == null) sp.Weights = GetWeights(sp.Mu.Value);
75
76      var offspring = OffspringParameter.ActualValue;
77      var mean = new RealVector(offspring[0].Length);
78      for (int i = 0; i < mean.Length; i++) {
79        for (int j = 0; j < sp.Mu.Value; j++)
80          mean[i] += sp.Weights[j] * offspring[j][i];
81      }
82
83      var oldMean = MeanParameter.ActualValue;
84      MeanParameter.ActualValue = mean;
85      OldMeanParameter.ActualValue = oldMean;
86      return base.Apply();
87    }
88
89    protected abstract DoubleArray GetWeights(int mu);
90  }
91}
Note: See TracBrowser for help on using the repository browser.