Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ALPS/3.3/Analyzers/AgeDistributionAnalyzer.cs @ 17226

Last change on this file since 17226 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("714CE0E4-A88F-44A1-A2D0-92F4D8252C0C")]
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(StorableConstructorFlag _) : base(_) { }
72    private AgeDistributionAnalyzer(AgeDistributionAnalyzer original, Cloner cloner)
73      : base(original, cloner) { }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new AgeDistributionAnalyzer(this, cloner);
76    }
77    public AgeDistributionAnalyzer()
78      : base() {
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The value which represents the age of a solution."));
80      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
81      Parameters.Add(new FixedValueParameter<StringValue>("HistogramName", "The name of the histogram that gets injected in to the results collection.", new StringValue("Age Distribution")));
82      Parameters.Add(new FixedValueParameter<BoolValue>("StoreHistory", "True if the history should be stored in addition to the current distribution", new BoolValue(false)));
83      Parameters.Add(new LookupParameter<IntValue>("Iterations", "Optional: A value indicating the current iteration."));
84      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Unused", new IntValue(-1)));
85
86      AgeParameter.Hidden = true;
87      ResultsParameter.Hidden = true;
88      IterationsParameter.Hidden = true;
89      MaximumIterationsParameter.Hidden = true;
90    }
91
92    public override IOperation Apply() {
93      DataTable ageDistribution = null;
94      ResultCollection results = ResultsParameter.ActualValue;
95      string description = "Shows the age distributions in the current population.";
96      if (results.ContainsKey(HistogramName)) {
97        ageDistribution = results[HistogramName].Value as DataTable;
98      } else {
99        ageDistribution = new DataTable("Population Age Distribution", description);
100        ageDistribution.VisualProperties.XAxisTitle = AgeParameter.ActualName;
101        ageDistribution.VisualProperties.YAxisTitle = "Frequency";
102        results.Add(new Result(HistogramName, description, ageDistribution));
103      }
104      DataRow row;
105      if (!ageDistribution.Rows.TryGetValue("AgeDistribution", out row)) {
106        row = new DataRow("AgeDistribution");
107        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
108        ageDistribution.Rows.Add(row);
109      }
110      var ages = AgeParameter.ActualValue;
111      row.Values.Replace(ages.Select(x => x.Value));
112
113      if (StoreHistory) {
114        string historyResultName = HistogramName + " History";
115        DataTableHistory adHistory = null;
116        if (results.ContainsKey(historyResultName)) {
117          adHistory = results[historyResultName].Value as DataTableHistory;
118        } else {
119          adHistory = new DataTableHistory();
120          results.Add(new Result(historyResultName, adHistory));
121        }
122        DataTable table = (DataTable)ageDistribution.Clone();
123        IntValue iteration = IterationsParameter.ActualValue;
124        if (iteration != null) {
125          string iterationName = IterationsParameter.ActualName;
126          if (iterationName.EndsWith("s")) iterationName = iterationName.Remove(iterationName.Length - 1);
127          string appendix = " at " + iterationName + " " + iteration.Value.ToString();
128          table.Name += appendix;
129          table.Rows["AgeDistribution"].VisualProperties.DisplayName += appendix;
130        }
131        adHistory.Add(table);
132      }
133      return base.Apply();
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.