Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Analysis/3.3/ValueAnalysis/MinAverageMaxValueCalculator.cs @ 17991

Last change on this file since 17991 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.0 KB
RevLine 
[3623]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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
[4722]22using HeuristicLab.Common;
[3623]23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
[17097]27using HEAL.Attic;
[3623]28
29namespace HeuristicLab.Analysis {
30  /// <summary>
[3662]31  /// An operator which calculates the minimum, average and maximum of a value in the scope tree.
[3623]32  /// </summary>
[3662]33  [Item("MinAverageMaxValueCalculator", "An operator which calculates the minimum, average and maximum of a value in the scope tree.")]
[17097]34  [StorableType("C10C55BB-737F-4A0A-BAB0-F3DAC28FBCB8")]
[3623]35  public sealed class MinAverageMaxValueCalculator : SingleSuccessorOperator {
[3662]36    public ScopeTreeLookupParameter<DoubleValue> ValueParameter {
37      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Value"]; }
[3623]38    }
[3662]39    public ValueLookupParameter<DoubleValue> MinValueParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["MinValue"]; }
[3623]41    }
[3662]42    public ValueLookupParameter<DoubleValue> AverageValueParameter {
43      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageValue"]; }
[3623]44    }
[3662]45    public ValueLookupParameter<DoubleValue> MaxValueParameter {
46      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaxValue"]; }
[3623]47    }
48
[4722]49    #region Storing & Cloning
50    [StorableConstructor]
[17097]51    private MinAverageMaxValueCalculator(StorableConstructorFlag _) : base(_) { }
[4722]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
[3623]57    public MinAverageMaxValueCalculator()
58      : base() {
[3662]59      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Value", "The value contained in the scope tree for which the minimum, average and maximum should be calculated."));
[3623]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.