Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAAnalyzer.cs @ 11578

Last change on this file since 11578 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
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]
37  public sealed class CMAAnalyzer : SingleSuccessorOperator, IAnalyzer {
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
48    public ILookupParameter<RealVector> MeanParameter {
49      get { return (ILookupParameter<RealVector>)Parameters["Mean"]; }
50    }
51
52    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
54    }
55
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."));
67      Parameters.Add(new LookupParameter<RealVector>("Mean", "The mean real vector that is being optimized."));
68      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions."));
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;
78      var vector = MeanParameter.ActualValue;
79      var results = ResultsParameter.ActualValue;
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;
86      }
87      avg /= qualities.Length;
88
89      DataTable progress;
90      if (results.ContainsKey("Progress")) {
91        progress = (DataTable)results["Progress"].Value;
92      } else {
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));
101      }
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);
107
108      DataTable scaling;
109      if (results.ContainsKey("Scaling")) {
110        scaling = (DataTable)results["Scaling"].Value;
111      } else {
112        scaling = new DataTable("Scaling");
113        scaling.VisualProperties.YAxisLogScale = true;
114        for (int i = 0; i < sp.C.GetLength(0); i++)
115          scaling.Rows.Add(new DataRow("Axis" + i.ToString()));
116        results.Add(new Result("Scaling", scaling));
117      }
118      for (int i = 0; i < sp.C.GetLength(0); i++)
119        scaling.Rows["Axis" + i.ToString()].Values.Add(sp.D[i]);
120
121      DataTable realVector;
122      if (results.ContainsKey("Object Variables")) {
123        realVector = (DataTable)results["Object Variables"].Value;
124      } else {
125        realVector = new DataTable("Object Variables");
126        for (int i = 0; i < vector.Length; i++)
127          realVector.Rows.Add(new DataRow("Axis" + i.ToString()));
128        results.Add(new Result("Object Variables", realVector));
129      }
130      for (int i = 0; i < vector.Length; i++)
131        realVector.Rows["Axis" + i.ToString()].Values.Add(vector[i]);
132
133      DataTable stdDevs;
134      if (results.ContainsKey("Standard Deviations")) {
135        stdDevs = (DataTable)results["Standard Deviations"].Value;
136      } else {
137        stdDevs = new DataTable("Standard Deviations");
138        stdDevs.VisualProperties.YAxisLogScale = true;
139        stdDevs.Rows.Add(new DataRow("MinStdDev"));
140        stdDevs.Rows.Add(new DataRow("MaxStdDev"));
141        for (int i = 0; i < vector.Length; i++)
142          stdDevs.Rows.Add(new DataRow("Axis" + i.ToString()));
143        results.Add(new Result("Standard Deviations", stdDevs));
144      }
145      for (int i = 0; i < vector.Length; i++)
146        stdDevs.Rows["Axis" + i.ToString()].Values.Add(Math.Sqrt(sp.C[i, i]));
147      stdDevs.Rows["MinStdDev"].Values.Add(sp.D.Min() * sp.Sigma);
148      stdDevs.Rows["MaxStdDev"].Values.Add(sp.D.Max() * sp.Sigma);
149
150      return base.Apply();
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.