Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAOperators/CMARecombinator.cs @ 10511

Last change on this file since 10511 was 9297, checked in by abeham, 12 years ago

#1961:

  • Improved performance of CMA-ES without ALGLIB
    • All CMA-ES parameter are standard .NET types instead of HL types
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.Encodings.RealVectorEncoding;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System;
29
30namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
31  [Item("CMARecombinator", "Base class that calculates the weighted mean of a number of offspring.")]
32  [StorableClass]
33  public abstract class CMARecombinator : SingleSuccessorOperator, ICMARecombinator {
34
35    public Type CMAType {
36      get { return typeof(CMAParameters); }
37    }
38
39    #region Parameter Properties
40    public IScopeTreeLookupParameter<RealVector> OffspringParameter {
41      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Offspring"]; }
42    }
43
44    public ILookupParameter<RealVector> MeanParameter {
45      get { return (ILookupParameter<RealVector>)Parameters["Mean"]; }
46    }
47
48    public ILookupParameter<RealVector> OldMeanParameter {
49      get { return (ILookupParameter<RealVector>)Parameters["OldMean"]; }
50    }
51
52    public ILookupParameter<CMAParameters> StrategyParametersParameter {
53      get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
54    }
55    #endregion
56
57    [StorableConstructor]
58    protected CMARecombinator(bool deserializing) : base(deserializing) { }
59    protected CMARecombinator(CMARecombinator original, Cloner cloner) : base(original, cloner) { }
60    protected CMARecombinator()
61      : base() {
62      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Offspring", "The offspring that should be recombined."));
63      Parameters.Add(new LookupParameter<RealVector>("Mean", "The new mean solution."));
64      Parameters.Add(new LookupParameter<RealVector>("OldMean", "The old mean solution."));
65      Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The CMA-ES strategy parameters used for mutation."));
66      OffspringParameter.ActualName = "RealVector";
67      MeanParameter.ActualName = "XMean";
68      OldMeanParameter.ActualName = "XOld";
69    }
70
71    public override IOperation Apply() {
72      var sp = StrategyParametersParameter.ActualValue;
73      if (sp.Weights == null) sp.Weights = GetWeights(sp.Mu);
74
75      var offspring = OffspringParameter.ActualValue;
76      var mean = new RealVector(offspring[0].Length);
77      for (int i = 0; i < mean.Length; i++) {
78        for (int j = 0; j < sp.Mu; j++)
79          mean[i] += sp.Weights[j] * offspring[j][i];
80      }
81
82      var oldMean = MeanParameter.ActualValue;
83      MeanParameter.ActualValue = mean;
84      OldMeanParameter.ActualValue = oldMean;
85      return base.Apply();
86    }
87
88    protected abstract double[] GetWeights(int mu);
89  }
90}
Note: See TracBrowser for help on using the repository browser.