Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Analysis/3.3/MinAverageMaxValueCalculator.cs @ 13323

Last change on this file since 13323 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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