Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/MinAverageMaxValueCalculator.cs @ 3721

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

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

  • adapted analyzers
File size: 3.7 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Analysis {
30  /// <summary>
31  /// An operator which calculates the minimum, average and maximum of a value in the scope tree.
32  /// </summary>
33  [Item("MinAverageMaxValueCalculator", "An operator which calculates the minimum, average and maximum of a value in the scope tree.")]
34  [StorableClass]
35  public sealed class MinAverageMaxValueCalculator : SingleSuccessorOperator {
36    public ScopeTreeLookupParameter<DoubleValue> ValueParameter {
37      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Value"]; }
38    }
39    public ValueLookupParameter<DoubleValue> MinValueParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["MinValue"]; }
41    }
42    public ValueLookupParameter<DoubleValue> AverageValueParameter {
43      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageValue"]; }
44    }
45    public ValueLookupParameter<DoubleValue> MaxValueParameter {
46      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaxValue"]; }
47    }
48
49    public MinAverageMaxValueCalculator()
50      : base() {
51      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Value", "The value contained in the scope tree for which the minimum, average and maximum should be calculated."));
52      Parameters.Add(new ValueLookupParameter<DoubleValue>("MinValue", "The minimum of the value."));
53      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageValue", "The average of the value."));
54      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxValue", "The maximum of the value."));
55    }
56
57    public override IOperation Apply() {
58      ItemArray<DoubleValue> values = ValueParameter.ActualValue;
59
60      if (values.Length > 0) {
61        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
62        for (int i = 0; i < values.Length; i++) {
63          if (values[i].Value < min) min = values[i].Value;
64          if (values[i].Value > max) max = values[i].Value;
65          sum += values[i].Value;
66        }
67
68        DoubleValue minValue = MinValueParameter.ActualValue;
69        if (minValue == null) MinValueParameter.ActualValue = new DoubleValue(min);
70        else minValue.Value = min;
71        DoubleValue averageValue = AverageValueParameter.ActualValue;
72        if (averageValue == null) AverageValueParameter.ActualValue = new DoubleValue(sum / values.Length);
73        else averageValue.Value = sum / values.Length;
74        DoubleValue maxValue = MaxValueParameter.ActualValue;
75        if (maxValue == null) MaxValueParameter.ActualValue = new DoubleValue(max);
76        else maxValue.Value = max;
77      }
78      return base.Apply();
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.