Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Analysis/3.3/QualityAnalysis/QualityDistributionAnalyzer.cs @ 6013

Last change on this file since 6013 was 6013, checked in by abeham, 12 years ago

#1465

  • removed AggregatedHistogramHistoryView
  • removed Histogram and HistogramHistory data structures
  • removed HistogramView and HistogramHistoryView, but left the HistogramControl
  • adapted the QualityDistributionAnalyzer to create a DataTable instead
  • added a StoreHistory parameter
  • adapted the temporary test project
File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis.QualityAnalysis {
32  [Item("QualityDistributionAnalyzer", "Analyzes the distribution of the quality values in that it adds a Histogram of them into the result collection.")]
33  [StorableClass]
34  public class QualityDistributionAnalyzer : SingleSuccessorOperator, IAnalyzer, IIterationBasedOperator {
35
36    #region Parameter properties
37    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
38      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
39    }
40    public IValueLookupParameter<ResultCollection> ResultsParameter {
41      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
42    }
43    private ValueParameter<StringValue> HistogramNameParameter {
44      get { return (ValueParameter<StringValue>)Parameters["HistogramName"]; }
45    }
46    private ValueParameter<BoolValue> StoreHistoryParameter {
47      get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
48    }
49    public ILookupParameter<IntValue> IterationsParameter {
50      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
51    }
52    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
53      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
54    }
55    #endregion
56
57    public string HistogramName {
58      get { return HistogramNameParameter.Value.Value; }
59      set { HistogramNameParameter.Value.Value = value; }
60    }
61
62    public bool StoreHistory {
63      get { return StoreHistoryParameter.Value.Value; }
64      set { StoreHistoryParameter.Value.Value = value; }
65    }
66
67    [StorableConstructor]
68    protected QualityDistributionAnalyzer(bool deserializing) : base(deserializing) { }
69    protected QualityDistributionAnalyzer(QualityDistributionAnalyzer original, Cloner cloner)
70      : base(original, cloner) {
71    }
72    public QualityDistributionAnalyzer()
73      : base() {
74      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
75      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
76      Parameters.Add(new FixedValueParameter<StringValue>("HistogramName", "The name of the histogram that gets injected in to the results collection.", new StringValue("Quality Distribution")));
77      Parameters.Add(new FixedValueParameter<BoolValue>("StoreHistory", "True if the history should be stored in addition to the current distribution", new BoolValue(false)));
78      Parameters.Add(new LookupParameter<IntValue>("Iterations", "Optional: A value indicating the current iteration."));
79      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Unused", new IntValue(-1)));
80
81      QualityParameter.Hidden = true;
82      ResultsParameter.Hidden = true;
83      IterationsParameter.Hidden = true;
84      MaximumIterationsParameter.Hidden = true;
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new QualityDistributionAnalyzer(this, cloner);
89    }
90
91    public override IOperation Apply() {
92      DataTable qualityDistribution = null;
93      ResultCollection results = ResultsParameter.ActualValue;
94      string description = "Shows the quality distributions in the current population.";
95      if (results.ContainsKey(HistogramName)) {
96        qualityDistribution = results[HistogramName].Value as DataTable;
97      } else {
98        qualityDistribution = new DataTable("Population Quality Distribution", description);
99        qualityDistribution.VisualProperties.XAxisTitle = IterationsParameter.ActualName;
100        qualityDistribution.VisualProperties.YAxisTitle = QualityParameter.ActualName;
101        results.Add(new Result(HistogramName, description, qualityDistribution));
102      }
103      DataRow row;
104      if (!qualityDistribution.Rows.TryGetValue("QualityDistribution", out row)) {
105        row = new DataRow("QualityDistribution");
106        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
107        qualityDistribution.Rows.Add(row);
108      }
109      var qualities = QualityParameter.ActualValue;
110      row.Values.Clear();
111      row.Values.AddRange(qualities.Select(x => x.Value));
112
113      if (StoreHistory) {
114        string historyResultName = HistogramName + " History";
115        DataTableHistory qdHistory = null;
116        if (results.ContainsKey(historyResultName)) {
117          qdHistory = results[historyResultName].Value as DataTableHistory;
118        } else {
119          qdHistory = new DataTableHistory();
120          results.Add(new Result(historyResultName, qdHistory));
121        }
122        DataTable table = (DataTable)qualityDistribution.Clone();
123        IntValue iteration = IterationsParameter.ActualValue;
124        if (iteration != null) {
125          string iterationName = IterationsParameter.ActualName;
126          if (iterationName.EndsWith("s")) iterationName = iterationName.Remove(iterationName.Length - 1);
127          string appendix = " at " + iterationName + " " + iteration.Value.ToString();
128          table.Name += appendix;
129          table.Rows["QualityDistribution"].Name += appendix;
130        }
131        qdHistory.Add(table);
132      }
133      return base.Apply();
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.