Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Analysis/3.3/QualityAnalyzer.cs @ 13419

Last change on this file since 13419 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 {
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    private BestQualityMemorizer BestQualityMemorizer {
68      get { return (BestQualityMemorizer)OperatorGraph.InitialOperator; }
69    }
70    private BestQualityMemorizer BestKnownQualityMemorizer {
71      get { return (BestQualityMemorizer)BestQualityMemorizer.Successor; }
72    }
73    private DataTableValuesCollector DataTableValuesCollector {
74      get { return (DataTableValuesCollector)BestKnownQualityMemorizer.Successor; }
75    }
76    private ResultsCollector ResultsCollector {
77      get { return (ResultsCollector)((QualityDifferenceCalculator)DataTableValuesCollector.Successor).Successor; }
78    }
79    #endregion
80
81    #region Storing & Cloning
82    [StorableConstructor]
83    private QualityAnalyzer(bool deserializing) : base(deserializing) { }
84    private QualityAnalyzer(QualityAnalyzer original, Cloner cloner)
85      : base(original, cloner) {
86      Initialize();
87    }
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new QualityAnalyzer(this, cloner);
90    }
91    #endregion
92    public QualityAnalyzer()
93      : base() {
94      #region Create parameters
95      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
96      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
97      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
98      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestQuality", "The best quality value found in the current run."));
99      Parameters.Add(new ValueLookupParameter<DataTable>("Qualities", "The data table to store the current best, current average, current worst, best and best known quality value."));
100      Parameters.Add(new ValueLookupParameter<DoubleValue>("AbsoluteDifferenceBestKnownToBest", "The absolute difference of the best known quality value to the best quality value."));
101      Parameters.Add(new ValueLookupParameter<PercentValue>("RelativeDifferenceBestKnownToBest", "The relative difference of the best known quality value to the best quality value."));
102      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The results collection where the analysis values should be stored."));
103      #endregion
104
105      #region Create operators
106      BestQualityMemorizer bestQualityMemorizer = new BestQualityMemorizer();
107      BestQualityMemorizer bestKnownQualityMemorizer = new BestQualityMemorizer();
108      DataTableValuesCollector dataTableValuesCollector = new DataTableValuesCollector();
109      QualityDifferenceCalculator qualityDifferenceCalculator = new QualityDifferenceCalculator();
110      ResultsCollector resultsCollector = new ResultsCollector();
111
112      bestQualityMemorizer.BestQualityParameter.ActualName = BestQualityParameter.Name;
113      bestQualityMemorizer.MaximizationParameter.ActualName = MaximizationParameter.Name;
114      bestQualityMemorizer.QualityParameter.ActualName = QualityParameter.Name;
115      bestQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
116
117      bestKnownQualityMemorizer.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
118      bestKnownQualityMemorizer.MaximizationParameter.ActualName = MaximizationParameter.Name;
119      bestKnownQualityMemorizer.QualityParameter.ActualName = QualityParameter.Name;
120      bestKnownQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
121
122      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality", null, BestQualityParameter.Name));
123      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestKnownQuality", null, BestKnownQualityParameter.Name));
124      dataTableValuesCollector.CollectedValues.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", null, QualityParameter.Name));
125      ((ScopeTreeLookupParameter<DoubleValue>)dataTableValuesCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
126      dataTableValuesCollector.DataTableParameter.ActualName = QualitiesParameter.Name;
127
128      qualityDifferenceCalculator.AbsoluteDifferenceParameter.ActualName = AbsoluteDifferenceBestKnownToBestParameter.Name;
129      qualityDifferenceCalculator.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
130      qualityDifferenceCalculator.RelativeDifferenceParameter.ActualName = RelativeDifferenceBestKnownToBestParameter.Name;
131      qualityDifferenceCalculator.SecondQualityParameter.ActualName = BestQualityParameter.Name;
132
133      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality", null, BestQualityParameter.Name));
134      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestKnownQuality", null, BestKnownQualityParameter.Name));
135      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", null, QualityParameter.Name));
136      ((ScopeTreeLookupParameter<DoubleValue>)resultsCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
137      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("AbsoluteDifferenceBestKnownToBest", null, AbsoluteDifferenceBestKnownToBestParameter.Name));
138      resultsCollector.CollectedValues.Add(new LookupParameter<PercentValue>("RelativeDifferenceBestKnownToBest", null, RelativeDifferenceBestKnownToBestParameter.Name));
139      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(QualitiesParameter.Name));
140      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
141      #endregion
142
143      #region Create operator graph
144      OperatorGraph.InitialOperator = bestQualityMemorizer;
145      bestQualityMemorizer.Successor = bestKnownQualityMemorizer;
146      bestKnownQualityMemorizer.Successor = dataTableValuesCollector;
147      dataTableValuesCollector.Successor = qualityDifferenceCalculator;
148      qualityDifferenceCalculator.Successor = resultsCollector;
149      resultsCollector.Successor = null;
150      #endregion
151
152      Initialize();
153    }
154
155    [StorableHook(HookType.AfterDeserialization)]
156    private void AfterDeserialization() {
157      Initialize();
158    }
159
160    private void Initialize() {
161      QualityParameter.DepthChanged += new EventHandler(QualityParameter_DepthChanged);
162    }
163
164    private void QualityParameter_DepthChanged(object sender, System.EventArgs e) {
165      BestQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
166      BestKnownQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
167      ((ScopeTreeLookupParameter<DoubleValue>)DataTableValuesCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
168      ((ScopeTreeLookupParameter<DoubleValue>)ResultsCollector.CollectedValues["Quality"]).Depth = QualityParameter.Depth;
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.