Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.ALPS/3.3/Analyzers/OldestAverageYoungestAgeCalculator.cs @ 14186

Last change on this file since 14186 was 14186, 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.Algorithms.ALPS {
30  [Item("OldestAverageYoungestAgeCalculator", "An operator which calculates the oldest, average and youngest age of solutions in the scope tree.")]
31  [StorableClass]
32  public sealed class OldestAverageYoungestAgeCalculator : SingleSuccessorOperator {
33    public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
34      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
35    }
36    public IValueLookupParameter<DoubleValue> OldestAgeParameter {
37      get { return (IValueLookupParameter<DoubleValue>)Parameters["OldestAge"]; }
38    }
39    public IValueLookupParameter<DoubleValue> AverageAgeParameter {
40      get { return (IValueLookupParameter<DoubleValue>)Parameters["AverageAge"]; }
41    }
42    public IValueLookupParameter<DoubleValue> YoungestAgeParameter {
43      get { return (IValueLookupParameter<DoubleValue>)Parameters["YoungestAge"]; }
44    }
45
46    #region Storing & Cloning
47    [StorableConstructor]
48    private OldestAverageYoungestAgeCalculator(bool deserializing) : base(deserializing) { }
49    private OldestAverageYoungestAgeCalculator(OldestAverageYoungestAgeCalculator original, Cloner cloner) : base(original, cloner) { }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new OldestAverageYoungestAgeCalculator(this, cloner);
52    }
53    #endregion
54
55    public OldestAverageYoungestAgeCalculator()
56      : base() {
57      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The value contained in the scope tree which represents the solution age."));
58      Parameters.Add(new ValueLookupParameter<DoubleValue>("OldestAge", "The age value of the oldest solution."));
59      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageAge", "The average age of all solutions."));
60      Parameters.Add(new ValueLookupParameter<DoubleValue>("YoungestAge", "The age value of the youngest solution."));
61
62      OldestAgeParameter.Hidden = true;
63      AverageAgeParameter.Hidden = true;
64      YoungestAgeParameter.Hidden = true;
65    }
66
67    public override IOperation Apply() {
68      var ages = AgeParameter.ActualValue;
69
70      if (ages.Length > 0) {
71        double min = double.MaxValue, max = double.MinValue, sum = 0;
72        for (int i = 0; i < ages.Length; i++) {
73          if (ages[i].Value < min) min = ages[i].Value;
74          if (ages[i].Value > max) max = ages[i].Value;
75          sum += ages[i].Value;
76        }
77
78        var oldest = OldestAgeParameter.ActualValue;
79        if (oldest == null) OldestAgeParameter.ActualValue = new DoubleValue(max);
80        else oldest.Value = max;
81        var average = AverageAgeParameter.ActualValue;
82        if (average == null) AverageAgeParameter.ActualValue = new DoubleValue(sum / ages.Length);
83        else average.Value = sum / ages.Length;
84        var youngest = YoungestAgeParameter.ActualValue;
85        if (youngest == null) YoungestAgeParameter.ActualValue = new DoubleValue(min);
86        else youngest.Value = min;
87      }
88      return base.Apply();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.