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.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using System;
|
---|
30 |
|
---|
31 | namespace 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 | } |
---|