Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/Analyzers/AgeDistributionAnalyzer.cs @ 12083

Last change on this file since 12083 was 12046, checked in by pfleck, 10 years ago

#2269 Added an OldestAverageYoungestAgeAnalyzer.

File size: 6.3 KB
RevLine 
[11617]1#region License Information
2/* HeuristicLab
[12018]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11617]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 {
[11620]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.")]
[11617]35  [StorableClass]
[11620]36  public sealed class AgeDistributionAnalyzer : SingleSuccessorOperator, IAnalyzer, IIterationBasedOperator {
[11617]37
38    #region Parameter Properties
39    public IScopeTreeLookupParameter<IntValue> AgeParameter {
40      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Age"]; }
41    }
42    public ValueLookupParameter<ResultCollection> ResultsParameter {
43      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
44    }
45    private ValueParameter<StringValue> HistogramNameParameter {
46      get { return (ValueParameter<StringValue>)Parameters["HistogramName"]; }
47    }
48    private ValueParameter<BoolValue> StoreHistoryParameter {
49      get { return (ValueParameter<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]
[11620]71    private AgeDistributionAnalyzer(bool deserializing)
[11617]72      : base(deserializing) { }
[11620]73    private AgeDistributionAnalyzer(AgeDistributionAnalyzer original, Cloner cloner)
[11617]74      : base(original, cloner) { }
75    public override IDeepCloneable Clone(Cloner cloner) {
[11620]76      return new AgeDistributionAnalyzer(this, cloner);
[11617]77    }
[11620]78    public AgeDistributionAnalyzer()
[11617]79      : base() {
80      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("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 => (double)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.