Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3658 was 3623, checked in by swagner, 14 years ago

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

  • added 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 current population.
32  /// </summary>
33  [Item("MinAverageMaxValueCalculator", "An operator which calculates the minimum, average and maximum of a value in the current population.")]
34  [StorableClass]
35  public sealed class MinAverageMaxValueCalculator : SingleSuccessorOperator {
36    public ILookupParameter<ItemArray<DoubleValue>> ValueParameter {
37      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Value"]; }
38    }
39    public IValueLookupParameter<DoubleValue> MinValueParameter {
40      get { return (IValueLookupParameter<DoubleValue>)Parameters["MinValue"]; }
41    }
42    public IValueLookupParameter<DoubleValue> AverageValueParameter {
43      get { return (IValueLookupParameter<DoubleValue>)Parameters["AverageValue"]; }
44    }
45    public IValueLookupParameter<DoubleValue> MaxValueParameter {
46      get { return (IValueLookupParameter<DoubleValue>)Parameters["MaxValue"]; }
47    }
48
49    public MinAverageMaxValueCalculator()
50      : base() {
51      Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Value", "The value contained in each sub-scope 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
58    public override IOperation Apply() {
59      ItemArray<DoubleValue> values = ValueParameter.ActualValue;
60
61      if (values.Length > 0) {
62        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
63        for (int i = 0; i < values.Length; i++) {
64          if (values[i].Value < min) min = values[i].Value;
65          if (values[i].Value > max) max = values[i].Value;
66          sum += values[i].Value;
67        }
68
69        DoubleValue minValue = MinValueParameter.ActualValue;
70        if (minValue == null) MinValueParameter.ActualValue = new DoubleValue(min);
71        else minValue.Value = min;
72        DoubleValue averageValue = AverageValueParameter.ActualValue;
73        if (averageValue == null) AverageValueParameter.ActualValue = new DoubleValue(sum / values.Length);
74        else averageValue.Value = sum / values.Length;
75        DoubleValue maxValue = MaxValueParameter.ActualValue;
76        if (maxValue == null) MaxValueParameter.ActualValue = new DoubleValue(max);
77        else maxValue.Value = max;
78      }
79      return base.Apply();
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.