Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PausableBasicAlgorithm/HeuristicLab.Analysis/3.3/QualityAnalysis/QualityAnalyzer.cs @ 14877

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

#2212 merged r12008, r12009, r12010 back into trunk

File size: 9.7 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;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Analysis {
33  /// <summary>
34  /// An operator which analyzes the quality of solutions in the scope tree.
35  /// </summary>
36  [Item("QualityAnalyzer", "An operator which analyzes the quality of solutions in the scope tree.")]
37  [StorableClass]
38  public sealed class QualityAnalyzer : AlgorithmOperator, IAnalyzer, ISingleObjectiveOperator {
39    #region Parameter properties
40    public ValueLookupParameter<BoolValue> MaximizationParameter {
41      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
42    }
43    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
44      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
45    }
46    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
47      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
48    }
49    public ValueLookupParameter<DoubleValue> BestQualityParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestQuality"]; }
51    }
52    public ValueLookupParameter<DataTable> QualitiesParameter {
53      get { return (ValueLookupParameter<DataTable>)Parameters["Qualities"]; }
54    }
55    public ValueLookupParameter<DoubleValue> AbsoluteDifferenceBestKnownToBestParameter {
56      get { return (ValueLookupParameter<DoubleValue>)Parameters["AbsoluteDifferenceBestKnownToBest"]; }
57    }
58    public ValueLookupParameter<PercentValue> RelativeDifferenceBestKnownToBestParameter {
59      get { return (ValueLookupParameter<PercentValue>)Parameters["RelativeDifferenceBestKnownToBest"]; }
60    }
61    public ValueLookupParameter<VariableCollection> ResultsParameter {
62      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
63    }
64    #endregion
65
66    #region Properties
67    public bool EnabledByDefault {
68      get { return true; }
69    }
70    private BestQualityMemorizer BestQualityMemorizer {
71      get { return (BestQualityMemorizer)OperatorGraph.InitialOperator; }
72    }
73    private BestQualityMemorizer BestKnownQualityMemorizer {
74      get { return (BestQualityMemorizer)BestQualityMemorizer.Successor; }
75    }
76    private DataTableValuesCollector DataTableValuesCollector {
77      get { return (DataTableValuesCollector)BestKnownQualityMemorizer.Successor; }
78    }
79    private ResultsCollector ResultsCollector {
80      get { return (ResultsCollector)((QualityDifferenceCalculator)DataTableValuesCollector.Successor).Successor; }
81    }
82    #endregion
83
84    #region Storing & Cloning
85    [StorableConstructor]
86    private QualityAnalyzer(bool deserializing) : base(deserializing) { }
87    private QualityAnalyzer(QualityAnalyzer original, Cloner cloner)
88      : base(original, cloner) {
89      Initialize();
90    }
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new QualityAnalyzer(this, cloner);
93    }
94    #endregion
95    public QualityAnalyzer()
96      : base() {
97      #region Create parameters
98      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
99      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
100      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
101      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestQuality", "The best quality value found in the current run."));
102      Parameters.Add(new ValueLookupParameter<DataTable>("Qualities", "The data table to store the current best, current average, current worst, best and best known quality value."));
103      Parameters.Add(new ValueLookupParameter<DoubleValue>("AbsoluteDifferenceBestKnownToBest", "The absolute difference of the best known quality value to the best quality value."));
104      Parameters.Add(new ValueLookupParameter<PercentValue>("RelativeDifferenceBestKnownToBest", "The relative difference of the best known quality value to the best quality value."));
105      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The results collection where the analysis values should be stored."));
106      #endregion
107
108      #region Create operators
109      BestQualityMemorizer bestQualityMemorizer = new BestQualityMemorizer();
110      BestQualityMemorizer bestKnownQualityMemorizer = new BestQualityMemorizer();
111      DataTableValuesCollector dataTableValuesCollector = new DataTableValuesCollector();
112      QualityDifferenceCalculator qualityDifferenceCalculator = new QualityDifferenceCalculator();
113      ResultsCollector resultsCollector = new ResultsCollector();
114
115      bestQualityMemorizer.BestQualityParameter.ActualName = BestQualityParameter.Name;
116      bestQualityMemorizer.MaximizationParameter.ActualName = MaximizationParameter.Name;
117      bestQualityMemorizer.QualityParameter.ActualName = QualityParameter.Name;
118      bestQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
119
120      bestKnownQualityMemorizer.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
121      bestKnownQualityMemorizer.MaximizationParameter.ActualName = MaximizationParameter.Name;
122      bestKnownQualityMemorizer.QualityParameter.ActualName = QualityParameter.Name;
123      bestKnownQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
124
125      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality", null, BestQualityParameter.Name));
126      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestKnownQuality", null, BestKnownQualityParameter.Name));
127      dataTableValuesCollector.CollectedValues.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", null, QualityParameter.Name));
128      ((ScopeTreeLookupParameter<DoubleValue>)dataTableValuesCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
129      dataTableValuesCollector.DataTableParameter.ActualName = QualitiesParameter.Name;
130
131      qualityDifferenceCalculator.AbsoluteDifferenceParameter.ActualName = AbsoluteDifferenceBestKnownToBestParameter.Name;
132      qualityDifferenceCalculator.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
133      qualityDifferenceCalculator.RelativeDifferenceParameter.ActualName = RelativeDifferenceBestKnownToBestParameter.Name;
134      qualityDifferenceCalculator.SecondQualityParameter.ActualName = BestQualityParameter.Name;
135
136      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality", null, BestQualityParameter.Name));
137      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestKnownQuality", null, BestKnownQualityParameter.Name));
138      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", null, QualityParameter.Name));
139      ((ScopeTreeLookupParameter<DoubleValue>)resultsCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
140      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("AbsoluteDifferenceBestKnownToBest", null, AbsoluteDifferenceBestKnownToBestParameter.Name));
141      resultsCollector.CollectedValues.Add(new LookupParameter<PercentValue>("RelativeDifferenceBestKnownToBest", null, RelativeDifferenceBestKnownToBestParameter.Name));
142      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(QualitiesParameter.Name));
143      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
144      #endregion
145
146      #region Create operator graph
147      OperatorGraph.InitialOperator = bestQualityMemorizer;
148      bestQualityMemorizer.Successor = bestKnownQualityMemorizer;
149      bestKnownQualityMemorizer.Successor = dataTableValuesCollector;
150      dataTableValuesCollector.Successor = qualityDifferenceCalculator;
151      qualityDifferenceCalculator.Successor = resultsCollector;
152      resultsCollector.Successor = null;
153      #endregion
154
155      Initialize();
156    }
157
158    [StorableHook(HookType.AfterDeserialization)]
159    private void AfterDeserialization() {
160      Initialize();
161    }
162
163    private void Initialize() {
164      QualityParameter.DepthChanged += new EventHandler(QualityParameter_DepthChanged);
165    }
166
167    private void QualityParameter_DepthChanged(object sender, System.EventArgs e) {
168      BestQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
169      BestKnownQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
170      ((ScopeTreeLookupParameter<DoubleValue>)DataTableValuesCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
171      ((ScopeTreeLookupParameter<DoubleValue>)ResultsCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.