Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Analysis/3.3/QualityAnalysis/QualityDistributionAnalyzer.cs @ 14778

Last change on this file since 14778 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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, ISingleObjectiveOperator {
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 virtual bool EnabledByDefault {
58      get { return true; }
59    }
60
61    public string HistogramName {
62      get { return HistogramNameParameter.Value.Value; }
63      set { HistogramNameParameter.Value.Value = value; }
64    }
65
66    public bool StoreHistory {
67      get { return StoreHistoryParameter.Value.Value; }
68      set { StoreHistoryParameter.Value.Value = value; }
69    }
70
71    [StorableConstructor]
72    protected QualityDistributionAnalyzer(bool deserializing) : base(deserializing) { }
73    protected QualityDistributionAnalyzer(QualityDistributionAnalyzer original, Cloner cloner)
74      : base(original, cloner) {
75    }
76    public QualityDistributionAnalyzer()
77      : base() {
78      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
79      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
80      Parameters.Add(new FixedValueParameter<StringValue>("HistogramName", "The name of the histogram that gets injected in to the results collection.", new StringValue("Quality Distribution")));
81      Parameters.Add(new FixedValueParameter<BoolValue>("StoreHistory", "True if the history should be stored in addition to the current distribution", new BoolValue(false)));
82      Parameters.Add(new LookupParameter<IntValue>("Iterations", "Optional: A value indicating the current iteration."));
83      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Unused", new IntValue(-1)));
84
85      QualityParameter.Hidden = true;
86      ResultsParameter.Hidden = true;
87      IterationsParameter.Hidden = true;
88      MaximumIterationsParameter.Hidden = true;
89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new QualityDistributionAnalyzer(this, cloner);
93    }
94
95    public override IOperation Apply() {
96      DataTable qualityDistribution = null;
97      ResultCollection results = ResultsParameter.ActualValue;
98      string description = "Shows the quality distributions in the current population.";
99      if (results.ContainsKey(HistogramName)) {
100        qualityDistribution = results[HistogramName].Value as DataTable;
101      } else {
102        qualityDistribution = new DataTable("Population Quality Distribution", description);
103        qualityDistribution.VisualProperties.XAxisTitle = QualityParameter.ActualName;
104        qualityDistribution.VisualProperties.YAxisTitle = "Frequency";
105        results.Add(new Result(HistogramName, description, qualityDistribution));
106      }
107      DataRow row;
108      if (!qualityDistribution.Rows.TryGetValue("QualityDistribution", out row)) {
109        row = new DataRow("QualityDistribution");
110        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
111        qualityDistribution.Rows.Add(row);
112      }
113      var qualities = QualityParameter.ActualValue;
114      row.Values.Replace(qualities.Select(x => x.Value));
115
116      if (StoreHistory) {
117        string historyResultName = HistogramName + " History";
118        DataTableHistory qdHistory = null;
119        if (results.ContainsKey(historyResultName)) {
120          qdHistory = results[historyResultName].Value as DataTableHistory;
121        } else {
122          qdHistory = new DataTableHistory();
123          results.Add(new Result(historyResultName, qdHistory));
124        }
125        DataTable table = (DataTable)qualityDistribution.Clone();
126        IntValue iteration = IterationsParameter.ActualValue;
127        if (iteration != null) {
128          string iterationName = IterationsParameter.ActualName;
129          if (iterationName.EndsWith("s")) iterationName = iterationName.Remove(iterationName.Length - 1);
130          string appendix = " at " + iterationName + " " + iteration.Value.ToString();
131          table.Name += appendix;
132          table.Rows["QualityDistribution"].VisualProperties.DisplayName += appendix;
133        }
134        qdHistory.Add(table);
135      }
136      return base.Apply();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.