Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Analysis/3.3/QualityAnalysis/QualityDistributionAnalyzer.cs @ 18183

Last change on this file since 18183 was 16956, checked in by abeham, 6 years ago

#2457: merged trunk into branch

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
29using System.Collections.Generic;
30using System.Linq;
31
32namespace HeuristicLab.Analysis.QualityAnalysis {
33  [Item("QualityDistributionAnalyzer", "Analyzes the distribution of the quality values in that it adds a Histogram of them into the result collection.")]
34  [StorableType("9B339DF1-A9D3-4C9C-B78C-84B764715EB8")]
35  public class QualityDistributionAnalyzer : SingleSuccessorOperator, IAnalyzer, IIterationBasedOperator, ISingleObjectiveOperator {
36    private const string TableDescription = "Shows the quality distributions in the current population.";
37
38    #region Parameter properties
39    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
40      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
42    public IValueLookupParameter<ResultCollection> ResultsParameter {
43      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
44    }
45    private ValueParameter<StringValue> HistogramNameParameter {
46      get { return (ValueParameter<StringValue>)Parameters["HistogramName"]; }
47    }
48    private ValueParameter<BoolValue> StoreHistoryParameter {
49      get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
50    }
51    public ILookupParameter<IntValue> IterationsParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
53    }
54    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
55      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
56    }
57    #endregion
58
59    public virtual bool EnabledByDefault {
60      get { return true; }
61    }
62
63    public string HistogramName {
64      get { return HistogramNameParameter.Value.Value; }
65      set { HistogramNameParameter.Value.Value = value; }
66    }
67
68    public bool StoreHistory {
69      get { return StoreHistoryParameter.Value.Value; }
70      set { StoreHistoryParameter.Value.Value = value; }
71    }
72
73    [StorableConstructor]
74    protected QualityDistributionAnalyzer(StorableConstructorFlag _) : base(_) { }
75    protected QualityDistributionAnalyzer(QualityDistributionAnalyzer original, Cloner cloner)
76      : base(original, cloner) {
77    }
78    public QualityDistributionAnalyzer()
79      : base() {
80      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
81      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
82      Parameters.Add(new FixedValueParameter<StringValue>("HistogramName", "The name of the histogram that gets injected in to the results collection.", new StringValue("Quality Distribution")));
83      Parameters.Add(new FixedValueParameter<BoolValue>("StoreHistory", "True if the history should be stored in addition to the current distribution", new BoolValue(false)));
84      Parameters.Add(new LookupParameter<IntValue>("Iterations", "Optional: A value indicating the current iteration."));
85      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Unused", new IntValue(-1)));
86
87      QualityParameter.Hidden = true;
88      ResultsParameter.Hidden = true;
89      IterationsParameter.Hidden = true;
90      MaximumIterationsParameter.Hidden = true;
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new QualityDistributionAnalyzer(this, cloner);
95    }
96
97    public static DataTable PrepareTable(string qualityName = "Quality") {
98      var result = new DataTable("Population Quality Distribution", TableDescription);
99      result.VisualProperties.XAxisTitle = qualityName;
100      result.VisualProperties.YAxisTitle = "Frequency";
101
102      var row = new DataRow("QualityDistribution");
103      row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
104      result.Rows.Add(row);
105
106      return result;
107    }
108
109    public static void UpdateTable(DataTable table, IEnumerable<double> qualities) {
110      var row = table.Rows["QualityDistribution"];
111      row.Values.Replace(qualities);
112    }
113
114    public override IOperation Apply() {
115      DataTable qualityDistribution = null;
116      var results = ResultsParameter.ActualValue;
117      if (results.ContainsKey(HistogramName)) {
118        qualityDistribution = (DataTable)results[HistogramName].Value;
119      } else {
120        qualityDistribution = PrepareTable(QualityParameter.ActualName);
121        results.Add(new Result(HistogramName, TableDescription, qualityDistribution));
122      }
123
124      UpdateTable(qualityDistribution, QualityParameter.ActualValue.Select(x => x.Value));
125
126      if (StoreHistory) {
127        var historyResultName = HistogramName + " History";
128        DataTableHistory qdHistory = null;
129        if (results.ContainsKey(historyResultName)) {
130          qdHistory = (DataTableHistory)results[historyResultName].Value;
131        } else {
132          qdHistory = new DataTableHistory();
133          results.Add(new Result(historyResultName, qdHistory));
134        }
135        var table = (DataTable)qualityDistribution.Clone();
136        var iteration = IterationsParameter.ActualValue;
137        if (iteration != null) {
138          var iterationName = IterationsParameter.ActualName;
139          if (iterationName.EndsWith("s")) iterationName = iterationName.Remove(iterationName.Length - 1);
140          var appendix = " at " + iterationName + " " + iteration.Value.ToString();
141          table.Name += appendix;
142          table.Rows["QualityDistribution"].VisualProperties.DisplayName += appendix;
143        }
144        qdHistory.Add(table);
145      }
146      return base.Apply();
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.