#3145: refactored infix formatter to improve output (less parenthesis) and added...
[heuristiclab.git] / HeuristicLab.Algorithms.CMAEvolutionStrategy / 3.4 / CMAAnalyzer.cs
blob00876b66f47dd5c1e5ac76b8b81d22b9f62457d4
1 #region License Information
2 /* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 * This file is part of HeuristicLab.
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.
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.
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/>.
20 #endregion
22 using System;
23 using System.Linq;
24 using HeuristicLab.Analysis;
25 using HeuristicLab.Common;
26 using HeuristicLab.Core;
27 using HeuristicLab.Data;
28 using HeuristicLab.Encodings.RealVectorEncoding;
29 using HeuristicLab.Operators;
30 using HeuristicLab.Optimization;
31 using HeuristicLab.Parameters;
32 using HEAL.Attic;
34 namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
35 [Item("CMAAnalyzer", "Analyzes the development of strategy parameters and visualizes the performance of CMA-ES.")]
36 [StorableType("7E36BAC4-D2A5-405D-B46F-FF91BC592D43")]
37 public sealed class CMAAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
39 public bool EnabledByDefault {
40 get { return false; }
43 #region Parameter Properties
44 public ILookupParameter<CMAParameters> StrategyParametersParameter {
45 get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
48 public ILookupParameter<RealVector> MeanParameter {
49 get { return (ILookupParameter<RealVector>)Parameters["Mean"]; }
52 public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
56 public ILookupParameter<ResultCollection> ResultsParameter {
57 get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
59 #endregion
61 [StorableConstructor]
62 private CMAAnalyzer(StorableConstructorFlag _) : base(_) { }
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."));
72 public override IDeepCloneable Clone(Cloner cloner) {
73 return new CMAAnalyzer(this, cloner);
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;
87 avg /= qualities.Length;
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));
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);
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));
118 for (int i = 0; i < sp.C.GetLength(0); i++)
119 scaling.Rows["Axis" + i.ToString()].Values.Add(sp.D[i]);
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));
130 for (int i = 0; i < vector.Length; i++)
131 realVector.Rows["Axis" + i.ToString()].Values.Add(vector[i]);
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));
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);
150 return base.Apply();