Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/QualityAnalyzer.cs @ 4712

Last change on this file since 4712 was 3662, checked in by swagner, 15 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

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