Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Analysis/3.3/QualityAnalysis/BestAverageWorstQualityAnalyzer.cs @ 17842

Last change on this file since 17842 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 11.7 KB
RevLine 
[3616]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3616]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
[3662]22using System;
23using HeuristicLab.Common;
[3616]24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
[17097]30using HEAL.Attic;
[3616]31
32namespace HeuristicLab.Analysis {
33  /// <summary>
[3662]34  /// An operator which analyzes the best, average and worst quality of solutions in the scope tree.
[3616]35  /// </summary>
[3662]36  [Item("BestAverageWorstQualityAnalyzer", "An operator which analyzes the best, average and worst quality of solutions in the scope tree.")]
[17097]37  [StorableType("D10450C6-A822-416E-ABC0-B2703317D5C1")]
[12005]38  public sealed class BestAverageWorstQualityAnalyzer : AlgorithmOperator, IAnalyzer, ISingleObjectiveOperator {
[3616]39    #region Parameter properties
40    public ValueLookupParameter<BoolValue> MaximizationParameter {
41      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
42    }
[3659]43    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
44      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3616]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<DoubleValue> CurrentBestQualityParameter {
53      get { return (ValueLookupParameter<DoubleValue>)Parameters["CurrentBestQuality"]; }
54    }
55    public ValueLookupParameter<DoubleValue> CurrentAverageQualityParameter {
56      get { return (ValueLookupParameter<DoubleValue>)Parameters["CurrentAverageQuality"]; }
57    }
58    public ValueLookupParameter<DoubleValue> CurrentWorstQualityParameter {
59      get { return (ValueLookupParameter<DoubleValue>)Parameters["CurrentWorstQuality"]; }
60    }
[3623]61    public ValueLookupParameter<DataTable> QualitiesParameter {
62      get { return (ValueLookupParameter<DataTable>)Parameters["Qualities"]; }
63    }
[3616]64    public ValueLookupParameter<DoubleValue> AbsoluteDifferenceBestKnownToBestParameter {
65      get { return (ValueLookupParameter<DoubleValue>)Parameters["AbsoluteDifferenceBestKnownToBest"]; }
66    }
67    public ValueLookupParameter<PercentValue> RelativeDifferenceBestKnownToBestParameter {
68      get { return (ValueLookupParameter<PercentValue>)Parameters["RelativeDifferenceBestKnownToBest"]; }
69    }
[6042]70    public ValueLookupParameter<ResultCollection> ResultsParameter {
71      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
[3616]72    }
73    #endregion
74
[3662]75    #region Properties
[7172]76    public bool EnabledByDefault {
77      get { return true; }
78    }
[3662]79    private BestQualityMemorizer BestQualityMemorizer {
80      get { return (BestQualityMemorizer)OperatorGraph.InitialOperator; }
[3616]81    }
[3662]82    private BestAverageWorstQualityCalculator BestAverageWorstQualityCalculator {
[3787]83      get { return (BestAverageWorstQualityCalculator)BestQualityMemorizer.Successor; }
[3662]84    }
85    #endregion
[3616]86
[4722]87    #region Storing & Cloning
88    [StorableConstructor]
[17097]89    private BestAverageWorstQualityAnalyzer(StorableConstructorFlag _) : base(_) { }
[4722]90    private BestAverageWorstQualityAnalyzer(BestAverageWorstQualityAnalyzer original, Cloner cloner)
91      : base(original, cloner) {
92      Initialize();
93    }
94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new BestAverageWorstQualityAnalyzer(this, cloner);
96    }
97    #endregion
[3662]98    public BestAverageWorstQualityAnalyzer()
99      : base() {
[3616]100      #region Create parameters
101      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
[3659]102      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[3616]103      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
104      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestQuality", "The best quality value found in the current run."));
105      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentBestQuality", "The best quality value found in the current population."));
106      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentAverageQuality", "The average quality value of all solutions in the current population."));
107      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentWorstQuality", "The worst quality value found in the current population."));
[3623]108      Parameters.Add(new ValueLookupParameter<DataTable>("Qualities", "The data table to store the current best, current average, current worst, best and best known quality value."));
[3616]109      Parameters.Add(new ValueLookupParameter<DoubleValue>("AbsoluteDifferenceBestKnownToBest", "The absolute difference of the best known quality value to the best quality value."));
110      Parameters.Add(new ValueLookupParameter<PercentValue>("RelativeDifferenceBestKnownToBest", "The relative difference of the best known quality value to the best quality value."));
[6042]111      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
[6051]112
113      BestQualityParameter.Hidden = true;
114      CurrentBestQualityParameter.Hidden = true;
115      CurrentAverageQualityParameter.Hidden = true;
116      CurrentWorstQualityParameter.Hidden = true;
117      QualitiesParameter.Hidden = true;
118      AbsoluteDifferenceBestKnownToBestParameter.Hidden = true;
119      RelativeDifferenceBestKnownToBestParameter.Hidden = true;
[3616]120      #endregion
121
122      #region Create operators
[3662]123      BestQualityMemorizer bestQualityMemorizer = new BestQualityMemorizer();
[3616]124      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator = new BestAverageWorstQualityCalculator();
125      DataTableValuesCollector dataTableValuesCollector = new DataTableValuesCollector();
126      QualityDifferenceCalculator qualityDifferenceCalculator = new QualityDifferenceCalculator();
127      ResultsCollector resultsCollector = new ResultsCollector();
128
[3662]129      bestQualityMemorizer.BestQualityParameter.ActualName = BestQualityParameter.Name;
130      bestQualityMemorizer.MaximizationParameter.ActualName = MaximizationParameter.Name;
131      bestQualityMemorizer.QualityParameter.ActualName = QualityParameter.Name;
132      bestQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
[3616]133
[3662]134      bestAverageWorstQualityCalculator.AverageQualityParameter.ActualName = CurrentAverageQualityParameter.Name;
135      bestAverageWorstQualityCalculator.BestQualityParameter.ActualName = CurrentBestQualityParameter.Name;
136      bestAverageWorstQualityCalculator.MaximizationParameter.ActualName = MaximizationParameter.Name;
137      bestAverageWorstQualityCalculator.QualityParameter.ActualName = QualityParameter.Name;
138      bestAverageWorstQualityCalculator.QualityParameter.Depth = QualityParameter.Depth;
139      bestAverageWorstQualityCalculator.WorstQualityParameter.ActualName = CurrentWorstQualityParameter.Name;
[3616]140
[3662]141      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentBestQuality", null, CurrentBestQualityParameter.Name));
142      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentAverageQuality", null, CurrentAverageQualityParameter.Name));
143      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentWorstQuality", null, CurrentWorstQualityParameter.Name));
144      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality", null, BestQualityParameter.Name));
145      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestKnownQuality", null, BestKnownQualityParameter.Name));
146      dataTableValuesCollector.DataTableParameter.ActualName = QualitiesParameter.Name;
[3616]147
[3662]148      qualityDifferenceCalculator.AbsoluteDifferenceParameter.ActualName = AbsoluteDifferenceBestKnownToBestParameter.Name;
149      qualityDifferenceCalculator.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
150      qualityDifferenceCalculator.RelativeDifferenceParameter.ActualName = RelativeDifferenceBestKnownToBestParameter.Name;
151      qualityDifferenceCalculator.SecondQualityParameter.ActualName = BestQualityParameter.Name;
[3616]152
[3662]153      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentBestQuality", null, CurrentBestQualityParameter.Name));
154      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentAverageQuality", null, CurrentAverageQualityParameter.Name));
155      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentWorstQuality", null, CurrentWorstQualityParameter.Name));
156      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality", null, BestQualityParameter.Name));
157      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestKnownQuality", null, BestKnownQualityParameter.Name));
158      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("AbsoluteDifferenceBestKnownToBest", null, AbsoluteDifferenceBestKnownToBestParameter.Name));
159      resultsCollector.CollectedValues.Add(new LookupParameter<PercentValue>("RelativeDifferenceBestKnownToBest", null, RelativeDifferenceBestKnownToBestParameter.Name));
160      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(QualitiesParameter.Name));
161      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
[3616]162      #endregion
163
164      #region Create operator graph
[3662]165      OperatorGraph.InitialOperator = bestQualityMemorizer;
[3787]166      bestQualityMemorizer.Successor = bestAverageWorstQualityCalculator;
[3616]167      bestAverageWorstQualityCalculator.Successor = dataTableValuesCollector;
168      dataTableValuesCollector.Successor = qualityDifferenceCalculator;
169      qualityDifferenceCalculator.Successor = resultsCollector;
170      resultsCollector.Successor = null;
171      #endregion
[3662]172
173      Initialize();
[3616]174    }
[3662]175
176    [StorableHook(HookType.AfterDeserialization)]
[4722]177    private void AfterDeserialization() {
178      Initialize();
[6042]179      // BackwardsCompatibility3.3
180      #region Backwards compatible code, remove with 3.4
181      if (Parameters["Results"] is ValueLookupParameter<VariableCollection>) {
182        Parameters.Remove("Results");
183        Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
184      }
185      #endregion
[4722]186    }
[6042]187
[3662]188    private void Initialize() {
189      QualityParameter.DepthChanged += new EventHandler(QualityParameter_DepthChanged);
190    }
191
192    private void QualityParameter_DepthChanged(object sender, System.EventArgs e) {
193      BestQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
194      BestAverageWorstQualityCalculator.QualityParameter.Depth = QualityParameter.Depth;
195    }
[3616]196  }
197}
Note: See TracBrowser for help on using the repository browser.