[12046] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12046] | 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
| 30 | [Item("OldestAverageYoungestAgeCalculator", "An operator which calculates the oldest, average and youngest age of solutions in the scope tree.")]
|
---|
| 31 | [StorableClass]
|
---|
[12570] | 32 | public sealed class OldestAverageYoungestAgeCalculator : SingleSuccessorOperator {
|
---|
[13046] | 33 | public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
|
---|
| 34 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
|
---|
[12046] | 35 | }
|
---|
[13046] | 36 | public IValueLookupParameter<DoubleValue> OldestAgeParameter {
|
---|
| 37 | get { return (IValueLookupParameter<DoubleValue>)Parameters["OldestAge"]; }
|
---|
[12046] | 38 | }
|
---|
[13046] | 39 | public IValueLookupParameter<DoubleValue> AverageAgeParameter {
|
---|
| 40 | get { return (IValueLookupParameter<DoubleValue>)Parameters["AverageAge"]; }
|
---|
[12046] | 41 | }
|
---|
[13046] | 42 | public IValueLookupParameter<DoubleValue> YoungestAgeParameter {
|
---|
| 43 | get { return (IValueLookupParameter<DoubleValue>)Parameters["YoungestAge"]; }
|
---|
[12046] | 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() {
|
---|
[13046] | 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."));
|
---|
[12046] | 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) {
|
---|
[13046] | 71 | double min = double.MaxValue, max = double.MinValue, sum = 0;
|
---|
[12046] | 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;
|
---|
[13046] | 79 | if (oldest == null) OldestAgeParameter.ActualValue = new DoubleValue(max);
|
---|
[12046] | 80 | else oldest.Value = max;
|
---|
| 81 | var average = AverageAgeParameter.ActualValue;
|
---|
[13046] | 82 | if (average == null) AverageAgeParameter.ActualValue = new DoubleValue(sum / ages.Length);
|
---|
[12046] | 83 | else average.Value = sum / ages.Length;
|
---|
| 84 | var youngest = YoungestAgeParameter.ActualValue;
|
---|
[13046] | 85 | if (youngest == null) YoungestAgeParameter.ActualValue = new DoubleValue(min);
|
---|
[12046] | 86 | else youngest.Value = min;
|
---|
| 87 | }
|
---|
| 88 | return base.Apply();
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | } |
---|