Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Analysis.FitnessLandscape/3.3/Analysis/InformationAnalyzer.cs @ 15702

Last change on this file since 15702 was 15279, checked in by abeham, 7 years ago

#2457: readded information analysis chart

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System.Linq;
30using System;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33
34  [StorableClass]
35  public class InformationAnalyzer : SingleSuccessorOperator, IQualityTrailAnalyzer {
36    public bool EnabledByDefault {
37      get { return false; }
38    }
39
40    #region Parameters
41    public LookupParameter<DataTable> QualityTrailParameter {
42      get { return (LookupParameter<DataTable>)Parameters["Quality Trail"]; }
43    }
44    public LookupParameter<ResultCollection> ResultsParameter {
45      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
46    }
47    public ValueLookupParameter<IntValue> NQuantilesParameter {
48      get { return (ValueLookupParameter<IntValue>)Parameters["NQuantiles"]; }
49    }
50    public ValueLookupParameter<IntValue> ShapeSizeParameter {
51      get { return (ValueLookupParameter<IntValue>)Parameters["ShapeSize"]; }
52    }
53    public LookupParameter<DoubleValue> InformationContentParameter {
54      get { return (LookupParameter<DoubleValue>)Parameters["InformationContent"]; }
55    }
56    public LookupParameter<DoubleValue> PartialInformationContentParameter {
57      get { return (LookupParameter<DoubleValue>)Parameters["PartialInformationContent"]; }
58    }
59    public LookupParameter<DoubleValue> DensityBasinInformationParameter {
60      get { return (LookupParameter<DoubleValue>)Parameters["DensityBasinInformation"]; }
61    }
62    public LookupParameter<DoubleValue> TotalEntropyParameter {
63      get { return (LookupParameter<DoubleValue>)Parameters["TotalEntropy"]; }
64    }
65    public LookupParameter<DoubleValue> InformationStabilityParameter {
66      get { return (LookupParameter<DoubleValue>)Parameters["InformationStability"]; }
67    }
68    public LookupParameter<DoubleValue> RegularityParameter {
69      get { return (LookupParameter<DoubleValue>)Parameters["Regularity"]; }
70    }
71    public LookupParameter<DoubleValue> DiversityParameter {
72      get { return (LookupParameter<DoubleValue>)Parameters["Diversity"]; }
73    }
74    public LookupParameter<DoubleValue> PeakInformationContentParameter {
75      get { return (LookupParameter<DoubleValue>)Parameters["PeakInformationContent"]; }
76    }
77    public LookupParameter<DoubleValue> PeakDensityBasinInformationParameter {
78      get { return (LookupParameter<DoubleValue>)Parameters["PeakDensityBasinInformation"]; }
79    }
80    public ILookupParameter<DataTable> InformationAnalysisParameter {
81      get { return (ILookupParameter<DataTable>)Parameters["InformationAnalysis"]; }
82    }
83    #endregion
84
85    [StorableConstructor]
86    protected InformationAnalyzer(bool deserializing) : base(deserializing) { }
87    protected InformationAnalyzer(InformationAnalyzer original, Cloner cloner) : base(original, cloner) { }
88
89    public InformationAnalyzer() {
90      Parameters.Add(new LookupParameter<DataTable>("Quality Trail", "The qualities of the solutions"));
91      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection of all results of this algorithm"));
92
93      Parameters.Add(new ValueLookupParameter<IntValue>("NQuantiles", "The number of delta quantiles to display", new IntValue(20)));
94      Parameters.Add(new ValueLookupParameter<IntValue>("ShapeSize", "The number of slopes to consider as shapes.", new IntValue(2)));
95
96      Parameters.Add(new LookupParameter<DoubleValue>("InformationContent", "The information content H(0) at eps = 0"));
97      Parameters.Add(new LookupParameter<DoubleValue>("PartialInformationContent", "Partial information content M(0) at eps = 0"));
98      Parameters.Add(new LookupParameter<DoubleValue>("DensityBasinInformation", "Density Basin Information h(0) at eps = 0"));
99      Parameters.Add(new LookupParameter<DoubleValue>("TotalEntropy", "The overall disorder in the trajectory"));
100      Parameters.Add(new LookupParameter<DoubleValue>("InformationStability", "Information Stability Value"));
101      Parameters.Add(new LookupParameter<DoubleValue>("Regularity", "The number of different quality differences"));
102      Parameters.Add(new LookupParameter<DoubleValue>("Diversity", "The number of different quality values"));
103      Parameters.Add(new LookupParameter<DoubleValue>("PeakInformationContent", "Maximum information content at any quality delta."));
104      Parameters.Add(new LookupParameter<DoubleValue>("PeakDensityBasinInformation", "Maximum density basin information at any quality delta."));
105
106      Parameters.Add(new LookupParameter<DataTable>("InformationAnalysis", "Graphical analysis of information theoretic features."));
107    }
108
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new InformationAnalyzer(this, cloner);
111    }
112
113    public override IOperation Apply() {
114      var qualityTrail = QualityTrailParameter.ActualValue;
115      if (qualityTrail == null || qualityTrail.Rows.Count == 0) return base.Apply();
116
117      var qualities = QualityTrailParameter.ActualValue.Rows.First().Values.ToList();
118      if (qualities.Count <= 2) return base.Apply();
119
120      var nQuantiles = NQuantilesParameter.ActualValue.Value;
121      var shapeSize = ShapeSizeParameter.ActualValue.Value;
122      var results = ResultsParameter.ActualValue;
123
124      var analysis = new InformationAnalysis(qualities, nQuantiles, shapeSize);
125      var ic = new DoubleValue(analysis.InformationContent.FirstOrDefault());
126      InformationContentParameter.ActualValue = ic;
127      AddOrUpdateResult(results, InformationContentParameter.Name, ic);
128      var pic = new DoubleValue(analysis.PartialInformationContent.FirstOrDefault());
129      PartialInformationContentParameter.ActualValue = pic;
130      AddOrUpdateResult(results, PartialInformationContentParameter.Name, pic);
131      var dbi = new DoubleValue(analysis.DensityBasinInformation.FirstOrDefault());
132      DensityBasinInformationParameter.ActualValue = dbi;
133      AddOrUpdateResult(results, DensityBasinInformationParameter.Name, dbi);
134      var @is = new DoubleValue(analysis.InformationStability);
135      InformationStabilityParameter.ActualValue = @is;
136      AddOrUpdateResult(results, InformationStabilityParameter.Name, @is);
137      var regularity = new DoubleValue(1.0 * analysis.Regularity / qualities.Count);
138      RegularityParameter.ActualValue = regularity;
139      AddOrUpdateResult(results, RegularityParameter.Name, regularity);
140      var diversity = new DoubleValue(1.0 * analysis.Diversity / qualities.Count);
141      DiversityParameter.ActualValue = diversity;
142      AddOrUpdateResult(results, DiversityParameter.Name, diversity);
143      var totalEntropy = new DoubleValue(analysis.TotalEntropy.FirstOrDefault());
144      TotalEntropyParameter.ActualValue = totalEntropy;
145      AddOrUpdateResult(results, TotalEntropyParameter.Name, totalEntropy);
146      var peakIc = new DoubleValue(analysis.PeakInformationContent.Value);
147      PeakInformationContentParameter.ActualValue = peakIc;
148      AddOrUpdateResult(results, PeakInformationContentParameter.Name, peakIc);
149      var peakDbi = new DoubleValue(analysis.PeakDensityBasinInformation.Value);
150      PeakDensityBasinInformationParameter.ActualValue = peakDbi;
151      AddOrUpdateResult(results, PeakDensityBasinInformationParameter.Name, peakDbi);
152
153      var itable = GetInformationTable(analysis);
154      AddOrUpdateResult(results, InformationAnalysisParameter.Name, itable);
155      return base.Apply();
156    }
157
158    private static DataTable GetInformationTable(InformationAnalysis analysis) {
159      var dt = new DataTable("Information Analysis");
160      var ic = new DataRow("Information Content");
161      var pic = new DataRow("Partial Information Content");
162      var dbi = new DataRow("Density Basin Information");
163      var te = new DataRow("Total Entropy");
164      var e = new DataRow("Epsilon");
165      e.VisualProperties.SecondYAxis = true;
166      ic.Values.AddRange(analysis.InformationContent);
167      pic.Values.AddRange(analysis.PartialInformationContent);
168      dbi.Values.AddRange(analysis.DensityBasinInformation);
169      te.Values.AddRange(analysis.TotalEntropy);
170      e.Values.AddRange(analysis.QualityDelta);
171      dt.Rows.Add(ic);
172      dt.Rows.Add(pic);
173      dt.Rows.Add(dbi);
174      dt.Rows.Add(te);
175      dt.Rows.Add(e);
176      return dt;
177    }
178
179    private static void AddOrUpdateResult(ResultCollection results, string name, IItem item, bool clone = false) {
180      IResult r;
181      if (!results.TryGetValue(name, out r)) {
182        results.Add(new Result(name, clone ? (IItem)item.Clone() : item));
183      } else r.Value = clone ? (IItem)item.Clone() : item;
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.