Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.Algorithms.ALPS/3.3/Analyzers/AgeDistributionAnalyzer.cs @ 16321

Last change on this file since 16321 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.ALPS {
33  // Based on QualityDistributionAnalyzer
34  [Item("AgeDistributionAnalyzer", "Analyzes the distribution of the ages in that it adds a Histogram of them into the result collection.")]
35  [StorableClass]
36  public sealed class AgeDistributionAnalyzer : SingleSuccessorOperator, IAnalyzer, IIterationBasedOperator {
37
38    #region Parameter Properties
39    public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
40      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
41    }
42    public IValueLookupParameter<ResultCollection> ResultsParameter {
43      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
44    }
45    private IValueParameter<StringValue> HistogramNameParameter {
46      get { return (IValueParameter<StringValue>)Parameters["HistogramName"]; }
47    }
48    private IValueParameter<BoolValue> StoreHistoryParameter {
49      get { return (IValueParameter<BoolValue>)Parameters["StoreHistory"]; }
50    }
51    public ILookupParameter<IntValue> IterationsParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
53    }
54    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
55      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
56    }
57    #endregion
58
59    public bool EnabledByDefault { get { return false; } }
60
61    public string HistogramName {
62      get { return HistogramNameParameter.Value.Value; }
63      set { HistogramNameParameter.Value.Value = value; }
64    }
65    public bool StoreHistory {
66      get { return StoreHistoryParameter.Value.Value; }
67      set { StoreHistoryParameter.Value.Value = value; }
68    }
69
70    [StorableConstructor]
71    private AgeDistributionAnalyzer(bool deserializing)
72      : base(deserializing) { }
73    private AgeDistributionAnalyzer(AgeDistributionAnalyzer original, Cloner cloner)
74      : base(original, cloner) { }
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new AgeDistributionAnalyzer(this, cloner);
77    }
78    public AgeDistributionAnalyzer()
79      : base() {
80      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The value which represents the age of a solution."));
81      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
82      Parameters.Add(new FixedValueParameter<StringValue>("HistogramName", "The name of the histogram that gets injected in to the results collection.", new StringValue("Age Distribution")));
83      Parameters.Add(new FixedValueParameter<BoolValue>("StoreHistory", "True if the history should be stored in addition to the current distribution", new BoolValue(false)));
84      Parameters.Add(new LookupParameter<IntValue>("Iterations", "Optional: A value indicating the current iteration."));
85      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Unused", new IntValue(-1)));
86
87      AgeParameter.Hidden = true;
88      ResultsParameter.Hidden = true;
89      IterationsParameter.Hidden = true;
90      MaximumIterationsParameter.Hidden = true;
91    }
92
93    public override IOperation Apply() {
94      DataTable ageDistribution = null;
95      ResultCollection results = ResultsParameter.ActualValue;
96      string description = "Shows the age distributions in the current population.";
97      if (results.ContainsKey(HistogramName)) {
98        ageDistribution = results[HistogramName].Value as DataTable;
99      } else {
100        ageDistribution = new DataTable("Population Age Distribution", description);
101        ageDistribution.VisualProperties.XAxisTitle = AgeParameter.ActualName;
102        ageDistribution.VisualProperties.YAxisTitle = "Frequency";
103        results.Add(new Result(HistogramName, description, ageDistribution));
104      }
105      DataRow row;
106      if (!ageDistribution.Rows.TryGetValue("AgeDistribution", out row)) {
107        row = new DataRow("AgeDistribution");
108        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
109        ageDistribution.Rows.Add(row);
110      }
111      var ages = AgeParameter.ActualValue;
112      row.Values.Replace(ages.Select(x => x.Value));
113
114      if (StoreHistory) {
115        string historyResultName = HistogramName + " History";
116        DataTableHistory adHistory = null;
117        if (results.ContainsKey(historyResultName)) {
118          adHistory = results[historyResultName].Value as DataTableHistory;
119        } else {
120          adHistory = new DataTableHistory();
121          results.Add(new Result(historyResultName, adHistory));
122        }
123        DataTable table = (DataTable)ageDistribution.Clone();
124        IntValue iteration = IterationsParameter.ActualValue;
125        if (iteration != null) {
126          string iterationName = IterationsParameter.ActualName;
127          if (iterationName.EndsWith("s")) iterationName = iterationName.Remove(iterationName.Length - 1);
128          string appendix = " at " + iterationName + " " + iteration.Value.ToString();
129          table.Name += appendix;
130          table.Rows["AgeDistribution"].VisualProperties.DisplayName += appendix;
131        }
132        adHistory.Add(table);
133      }
134      return base.Apply();
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.