Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Analysis/3.3/ValueAnalysis/MinAverageMaxValueAnalyzer.cs @ 18140

Last change on this file since 18140 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 12.8 KB
RevLine 
[3623]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3623]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;
[3623]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>
[3662]34  /// An operator which analyzes the minimum, average and maximum of a value in the scope tree.
[3623]35  /// </summary>
[3662]36  [Item("MinAverageMaxValueAnalyzer", "An operator which analyzes the minimum, average and maximum of a value in the scope tree.")]
[3623]37  [StorableClass]
[3662]38  public sealed class MinAverageMaxValueAnalyzer : AlgorithmOperator, IAnalyzer {
[3623]39    #region Parameter properties
[3659]40    public ScopeTreeLookupParameter<DoubleValue> ValueParameter {
41      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Value"]; }
[3623]42    }
43    public ValueLookupParameter<DoubleValue> MinValueParameter {
44      get { return (ValueLookupParameter<DoubleValue>)Parameters["MinValue"]; }
45    }
46    public ValueLookupParameter<DoubleValue> AverageValueParameter {
47      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageValue"]; }
48    }
49    public ValueLookupParameter<DoubleValue> MaxValueParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaxValue"]; }
51    }
52    public ValueLookupParameter<DataTable> ValuesParameter {
53      get { return (ValueLookupParameter<DataTable>)Parameters["Values"]; }
54    }
55    public ValueLookupParameter<VariableCollection> ResultsParameter {
56      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
57    }
[3710]58    public ValueParameter<BoolValue> CollectMinValueInResultsParameter {
59      get { return (ValueParameter<BoolValue>)Parameters["CollectMinValueInResults"]; }
60    }
61    public ValueParameter<BoolValue> CollectMaxValueInResultsParameter {
62      get { return (ValueParameter<BoolValue>)Parameters["CollectMaxValueInResults"]; }
63    }
64    public ValueParameter<BoolValue> CollectAverageValueInResultsParameter {
65      get { return (ValueParameter<BoolValue>)Parameters["CollectAverageValueInResults"]; }
66    }
[3623]67    #endregion
68
[3662]69    #region Properties
[7172]70    public bool EnabledByDefault {
71      get { return true; }
72    }
[3662]73    private MinAverageMaxValueCalculator MinAverageMaxValueCalculator {
74      get { return (MinAverageMaxValueCalculator)OperatorGraph.InitialOperator; }
[3623]75    }
[3710]76    private BoolValue CollectMinValueInResults {
77      get { return CollectMinValueInResultsParameter.Value; }
78    }
79    private BoolValue CollectMaxValueInResults {
80      get { return CollectMaxValueInResultsParameter.Value; }
81    }
82    private BoolValue CollectAverageValueInResults {
83      get { return CollectAverageValueInResultsParameter.Value; }
84    }
[3662]85    #endregion
[7172]86
[3710]87    [Storable]
88    private ResultsCollector resultsCollector;
[4722]89
90    #region Storing & Cloning
91    [StorableConstructor]
92    private MinAverageMaxValueAnalyzer(bool deserializing) : base(deserializing) { }
93    private MinAverageMaxValueAnalyzer(MinAverageMaxValueAnalyzer original, Cloner cloner)
94      : base(original, cloner) {
95      resultsCollector = cloner.Clone(original.resultsCollector);
[7351]96      RegisterEventHandlers();
[4722]97    }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new MinAverageMaxValueAnalyzer(this, cloner);
100    }
101    #endregion
[3662]102    public MinAverageMaxValueAnalyzer()
103      : base() {
[3623]104      #region Create parameters
[3662]105      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Value", "The value contained in the scope tree which should be analyzed."));
[3623]106      Parameters.Add(new ValueLookupParameter<DoubleValue>("MinValue", "The minimum of the value."));
[3710]107      Parameters.Add(new ValueParameter<BoolValue>("CollectMinValueInResults", "Determines if the minimum of the value should also be stored in the results.", new BoolValue(true)));
[3623]108      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageValue", "The average of the value."));
[3710]109      Parameters.Add(new ValueParameter<BoolValue>("CollectAverageValueInResults", "Determines if the average of the value should also be stored in the results.", new BoolValue(true)));
[3623]110      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxValue", "The maximum of the value."));
[3710]111      Parameters.Add(new ValueParameter<BoolValue>("CollectMaxValueInResults", "Determines if the maximum of the value should also be stored in the results.", new BoolValue(true)));
[3623]112      Parameters.Add(new ValueLookupParameter<DataTable>("Values", "The data table to store the minimum, average and maximum of the value."));
113      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The results collection where the analysis values should be stored."));
114      #endregion
115
116      #region Create operators
117      MinAverageMaxValueCalculator minAverageMaxValueCalculator = new MinAverageMaxValueCalculator();
118      DataTableValuesCollector dataTableValuesCollector = new DataTableValuesCollector();
[3710]119      resultsCollector = new ResultsCollector();
[3623]120
[3662]121      minAverageMaxValueCalculator.AverageValueParameter.ActualName = AverageValueParameter.Name;
122      minAverageMaxValueCalculator.MaxValueParameter.ActualName = MaxValueParameter.Name;
123      minAverageMaxValueCalculator.MinValueParameter.ActualName = MinValueParameter.Name;
124      minAverageMaxValueCalculator.ValueParameter.ActualName = ValueParameter.Name;
125      minAverageMaxValueCalculator.ValueParameter.Depth = ValueParameter.Depth;
[3623]126
[3662]127      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MinValue", null, MinValueParameter.Name));
128      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("AverageValue", null, AverageValueParameter.Name));
129      dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MaxValue", null, MaxValueParameter.Name));
130      dataTableValuesCollector.DataTableParameter.ActualName = ValuesParameter.Name;
[3623]131
[3710]132      if (CollectMinValueInResults.Value)
133        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MinValue", null, MinValueParameter.Name));
134      if (CollectAverageValueInResults.Value)
135        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("AverageValue", null, AverageValueParameter.Name));
136      if (CollectMaxValueInResults.Value)
137        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MaxValue", null, MaxValueParameter.Name));
[3662]138      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(ValuesParameter.Name));
139      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
[3623]140      #endregion
141
142      #region Create operator graph
143      OperatorGraph.InitialOperator = minAverageMaxValueCalculator;
144      minAverageMaxValueCalculator.Successor = dataTableValuesCollector;
145      dataTableValuesCollector.Successor = resultsCollector;
146      resultsCollector.Successor = null;
147      #endregion
[3662]148
[7351]149      RegisterEventHandlers();
[3623]150    }
[3662]151
152    [StorableHook(HookType.AfterDeserialization)]
[7351]153    private void AfterDeserialization() {
154      RegisterEventHandlers();
155    }
156
157    private void RegisterEventHandlers() {
[3662]158      ValueParameter.DepthChanged += new EventHandler(ValueParameter_DepthChanged);
[3710]159      CollectMinValueInResultsParameter.ValueChanged += new EventHandler(CollectMinValueInResultsParameter_ValueChanged);
[4722]160      CollectMinValueInResultsParameter.Value.ValueChanged += new EventHandler(CollectMinValueInResultsParameter_Value_ValueChanged);
[3710]161      CollectMaxValueInResultsParameter.ValueChanged += new EventHandler(CollectMaxValueInResultsParameter_ValueChanged);
[4722]162      CollectMaxValueInResultsParameter.Value.ValueChanged += new EventHandler(CollectMaxValueInResultsParameter_Value_ValueChanged);
[3710]163      CollectAverageValueInResultsParameter.ValueChanged += new EventHandler(CollectAverageValueInResultsParameter_ValueChanged);
[4722]164      CollectAverageValueInResultsParameter.Value.ValueChanged += new EventHandler(CollectAverageValueInResultsParameter_Value_ValueChanged);
[3662]165    }
166
[3710]167    private void CollectAverageValueInResultsParameter_ValueChanged(object sender, EventArgs e) {
[4722]168      IParameter avgValueParameter;
169      resultsCollector.CollectedValues.TryGetValue("AverageValue", out avgValueParameter);
170      if (CollectAverageValueInResults.Value && avgValueParameter == null)
[3710]171        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("AverageValue", null, AverageValueParameter.Name));
[4722]172      else if (!CollectAverageValueInResults.Value && avgValueParameter != null)
[3710]173        resultsCollector.CollectedValues.Remove(avgValueParameter);
[4722]174      CollectAverageValueInResultsParameter.Value.ValueChanged += new EventHandler(CollectAverageValueInResultsParameter_Value_ValueChanged);
[3710]175    }
176
[4722]177    private void CollectAverageValueInResultsParameter_Value_ValueChanged(object sender, EventArgs e) {
178      IParameter avgValueParameter;
179      resultsCollector.CollectedValues.TryGetValue("AverageValue", out avgValueParameter);
180      if (CollectAverageValueInResults.Value && avgValueParameter == null)
181        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("AverageValue", null, AverageValueParameter.Name));
182      else if (!CollectAverageValueInResults.Value && avgValueParameter != null)
183        resultsCollector.CollectedValues.Remove(avgValueParameter);
184    }
185
[3710]186    private void CollectMaxValueInResultsParameter_ValueChanged(object sender, EventArgs e) {
[4722]187      IParameter maxValueParameter;
188      resultsCollector.CollectedValues.TryGetValue("MaxValue", out maxValueParameter);
189      if (CollectMaxValueInResults.Value && maxValueParameter == null)
[3710]190        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MaxValue", null, MaxValueParameter.Name));
[4722]191      else if (!CollectMaxValueInResults.Value && maxValueParameter != null)
[3710]192        resultsCollector.CollectedValues.Remove(maxValueParameter);
[4722]193      CollectMaxValueInResultsParameter.Value.ValueChanged += new EventHandler(CollectMaxValueInResultsParameter_Value_ValueChanged);
[3710]194    }
195
[4722]196    private void CollectMaxValueInResultsParameter_Value_ValueChanged(object sender, EventArgs e) {
197      IParameter maxValueParameter;
198      resultsCollector.CollectedValues.TryGetValue("MaxValue", out maxValueParameter);
199      if (CollectMaxValueInResults.Value && maxValueParameter == null)
200        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MaxValue", null, MaxValueParameter.Name));
201      else if (!CollectMaxValueInResults.Value && maxValueParameter != null)
202        resultsCollector.CollectedValues.Remove(maxValueParameter);
[7172]203    }
[4722]204
[3710]205    private void CollectMinValueInResultsParameter_ValueChanged(object sender, EventArgs e) {
[4722]206      IParameter minValueParameter;
207      resultsCollector.CollectedValues.TryGetValue("MinValue", out minValueParameter);
208      if (CollectMinValueInResults.Value && minValueParameter == null)
[3710]209        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MinValue", null, MinValueParameter.Name));
[4722]210      else if (!CollectMinValueInResults.Value && minValueParameter != null)
[3710]211        resultsCollector.CollectedValues.Remove(minValueParameter);
[4722]212      CollectMinValueInResultsParameter.Value.ValueChanged += new EventHandler(CollectMinValueInResultsParameter_Value_ValueChanged);
[3710]213    }
214
[4722]215    private void CollectMinValueInResultsParameter_Value_ValueChanged(object sender, EventArgs e) {
216      IParameter minValueParameter;
217      resultsCollector.CollectedValues.TryGetValue("MinValue", out minValueParameter);
218      if (CollectMinValueInResults.Value && minValueParameter == null)
219        resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("MinValue", null, MinValueParameter.Name));
220      else if (!CollectMinValueInResults.Value && minValueParameter != null)
221        resultsCollector.CollectedValues.Remove(minValueParameter);
[3662]222    }
223
224    private void ValueParameter_DepthChanged(object sender, System.EventArgs e) {
225      MinAverageMaxValueCalculator.ValueParameter.Depth = ValueParameter.Depth;
226    }
[3623]227  }
228}
Note: See TracBrowser for help on using the repository browser.