Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAAnalyzer.cs @ 12487

Last change on this file since 12487 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 6.4 KB
RevLine 
[9129]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
[9302]22using System;
23using System.Linq;
[9129]24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
[9302]27using HeuristicLab.Data;
[9129]28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
35  [Item("CMAAnalyzer", "Analyzes the development of strategy parameters and visualizes the performance of CMA-ES.")]
36  [StorableClass]
[11970]37  public sealed class CMAAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
[9129]38
39    public bool EnabledByDefault {
40      get { return false; }
41    }
42
43    #region Parameter Properties
44    public ILookupParameter<CMAParameters> StrategyParametersParameter {
45      get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
46    }
47
[9132]48    public ILookupParameter<RealVector> MeanParameter {
49      get { return (ILookupParameter<RealVector>)Parameters["Mean"]; }
[9129]50    }
51
[9302]52    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
54    }
55
[9129]56    public ILookupParameter<ResultCollection> ResultsParameter {
57      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
58    }
59    #endregion
60
61    [StorableConstructor]
62    private CMAAnalyzer(bool deserializing) : base(deserializing) { }
63    private CMAAnalyzer(CMAAnalyzer original, Cloner cloner) : base(original, cloner) { }
64    public CMAAnalyzer()
65      : base() {
66      Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The CMA strategy parameters to be analyzed."));
[9132]67      Parameters.Add(new LookupParameter<RealVector>("Mean", "The mean real vector that is being optimized."));
[9302]68      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions."));
[9129]69      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection to store the results in."));
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new CMAAnalyzer(this, cloner);
74    }
75
76    public override IOperation Apply() {
77      var sp = StrategyParametersParameter.ActualValue;
[9132]78      var vector = MeanParameter.ActualValue;
[9129]79      var results = ResultsParameter.ActualValue;
[9302]80      var qualities = QualityParameter.ActualValue;
81      double min = qualities[0].Value, max = qualities[0].Value, avg = qualities[0].Value;
82      for (int i = 1; i < qualities.Length; i++) {
83        if (qualities[i].Value < min) min = qualities[i].Value;
84        if (qualities[i].Value > max) max = qualities[i].Value;
85        avg += qualities[i].Value;
[9129]86      }
[9302]87      avg /= qualities.Length;
[9129]88
[9302]89      DataTable progress;
90      if (results.ContainsKey("Progress")) {
91        progress = (DataTable)results["Progress"].Value;
[9129]92      } else {
[9302]93        progress = new DataTable("Progress");
94        progress.Rows.Add(new DataRow("AxisRatio"));
95        progress.Rows.Add(new DataRow("Sigma"));
96        progress.Rows.Add(new DataRow("Min Quality"));
97        progress.Rows.Add(new DataRow("Max Quality"));
98        progress.Rows.Add(new DataRow("Avg Quality"));
99        progress.VisualProperties.YAxisLogScale = true;
100        results.Add(new Result("Progress", progress));
[9129]101      }
[9302]102      progress.Rows["AxisRatio"].Values.Add(sp.AxisRatio);
103      progress.Rows["Sigma"].Values.Add(sp.Sigma);
104      progress.Rows["Min Quality"].Values.Add(min);
105      progress.Rows["Max Quality"].Values.Add(max);
106      progress.Rows["Avg Quality"].Values.Add(avg);
[9129]107
108      DataTable scaling;
109      if (results.ContainsKey("Scaling")) {
110        scaling = (DataTable)results["Scaling"].Value;
111      } else {
112        scaling = new DataTable("Scaling");
[9141]113        scaling.VisualProperties.YAxisLogScale = true;
[9297]114        for (int i = 0; i < sp.C.GetLength(0); i++)
[9129]115          scaling.Rows.Add(new DataRow("Axis" + i.ToString()));
116        results.Add(new Result("Scaling", scaling));
117      }
[9297]118      for (int i = 0; i < sp.C.GetLength(0); i++)
[9129]119        scaling.Rows["Axis" + i.ToString()].Values.Add(sp.D[i]);
120
121      DataTable realVector;
[9302]122      if (results.ContainsKey("Object Variables")) {
123        realVector = (DataTable)results["Object Variables"].Value;
[9129]124      } else {
[9302]125        realVector = new DataTable("Object Variables");
[9129]126        for (int i = 0; i < vector.Length; i++)
127          realVector.Rows.Add(new DataRow("Axis" + i.ToString()));
[9302]128        results.Add(new Result("Object Variables", realVector));
[9129]129      }
130      for (int i = 0; i < vector.Length; i++)
131        realVector.Rows["Axis" + i.ToString()].Values.Add(vector[i]);
132
[9140]133      DataTable stdDevs;
[9302]134      if (results.ContainsKey("Standard Deviations")) {
135        stdDevs = (DataTable)results["Standard Deviations"].Value;
[9140]136      } else {
[9302]137        stdDevs = new DataTable("Standard Deviations");
[9141]138        stdDevs.VisualProperties.YAxisLogScale = true;
[9302]139        stdDevs.Rows.Add(new DataRow("MinStdDev"));
140        stdDevs.Rows.Add(new DataRow("MaxStdDev"));
[9140]141        for (int i = 0; i < vector.Length; i++)
142          stdDevs.Rows.Add(new DataRow("Axis" + i.ToString()));
[9302]143        results.Add(new Result("Standard Deviations", stdDevs));
[9140]144      }
145      for (int i = 0; i < vector.Length; i++)
[9148]146        stdDevs.Rows["Axis" + i.ToString()].Values.Add(Math.Sqrt(sp.C[i, i]));
[9302]147      stdDevs.Rows["MinStdDev"].Values.Add(sp.D.Min() * sp.Sigma);
148      stdDevs.Rows["MaxStdDev"].Values.Add(sp.D.Max() * sp.Sigma);
[9140]149
[9129]150      return base.Apply();
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.