source: branches/CloningRefactoring/HeuristicLab.Analysis/3.3/MinAverageMaxValueAnalyzer.cs @ 4714

Last change on this file since 4714 was 4714, checked in by abeham, 10 years ago

#922

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