Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14627 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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    #region Storing & Cloning
50    [StorableConstructor]
51    private MinAverageMaxValueCalculator(bool deserializing) : base(deserializing) { }
52    private MinAverageMaxValueCalculator(MinAverageMaxValueCalculator original, Cloner cloner) : base(original, cloner) { }
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new MinAverageMaxValueCalculator(this, cloner);
55    }
56    #endregion
57    public MinAverageMaxValueCalculator()
58      : base() {
59      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Value", "The value contained in the scope tree for which the minimum, average and maximum should be calculated."));
60      Parameters.Add(new ValueLookupParameter<DoubleValue>("MinValue", "The minimum of the value."));
61      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageValue", "The average of the value."));
62      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxValue", "The maximum of the value."));
63    }
64
65    public override IOperation Apply() {
66      ItemArray<DoubleValue> values = ValueParameter.ActualValue;
67
68      if (values.Length > 0) {
69        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
70        for (int i = 0; i < values.Length; i++) {
71          if (values[i].Value < min) min = values[i].Value;
72          if (values[i].Value > max) max = values[i].Value;
73          sum += values[i].Value;
74        }
75
76        DoubleValue minValue = MinValueParameter.ActualValue;
77        if (minValue == null) MinValueParameter.ActualValue = new DoubleValue(min);
78        else minValue.Value = min;
79        DoubleValue averageValue = AverageValueParameter.ActualValue;
80        if (averageValue == null) AverageValueParameter.ActualValue = new DoubleValue(sum / values.Length);
81        else averageValue.Value = sum / values.Length;
82        DoubleValue maxValue = MaxValueParameter.ActualValue;
83        if (maxValue == null) MaxValueParameter.ActualValue = new DoubleValue(max);
84        else maxValue.Value = max;
85      }
86      return base.Apply();
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.