Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Analysis/3.3/QualityAnalysis/QualityDistributionAnalyzer.cs @ 6760

Last change on this file since 6760 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

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 = QualityParameter.ActualName;
100        qualityDistribution.VisualProperties.YAxisTitle = "Frequency";
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.Replace(qualities.Select(x => x.Value));
111
112      if (StoreHistory) {
113        string historyResultName = HistogramName + " History";
114        DataTableHistory qdHistory = null;
115        if (results.ContainsKey(historyResultName)) {
116          qdHistory = results[historyResultName].Value as DataTableHistory;
117        } else {
118          qdHistory = new DataTableHistory();
119          results.Add(new Result(historyResultName, qdHistory));
120        }
121        DataTable table = (DataTable)qualityDistribution.Clone();
122        IntValue iteration = IterationsParameter.ActualValue;
123        if (iteration != null) {
124          string iterationName = IterationsParameter.ActualName;
125          if (iterationName.EndsWith("s")) iterationName = iterationName.Remove(iterationName.Length - 1);
126          string appendix = " at " + iterationName + " " + iteration.Value.ToString();
127          table.Name += appendix;
128          table.Rows["QualityDistribution"].VisualProperties.DisplayName += appendix;
129        }
130        qdHistory.Add(table);
131      }
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.