[11617] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Analysis;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace 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
|
---|
[13046] | 39 | public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
|
---|
| 40 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
|
---|
[11617] | 41 | }
|
---|
[13127] | 42 | public IValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 43 | get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
[11617] | 44 | }
|
---|
[13127] | 45 | private IValueParameter<StringValue> HistogramNameParameter {
|
---|
| 46 | get { return (IValueParameter<StringValue>)Parameters["HistogramName"]; }
|
---|
[11617] | 47 | }
|
---|
[13127] | 48 | private IValueParameter<BoolValue> StoreHistoryParameter {
|
---|
| 49 | get { return (IValueParameter<BoolValue>)Parameters["StoreHistory"]; }
|
---|
[11617] | 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() {
|
---|
[13046] | 80 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The value which represents the age of a solution."));
|
---|
[11617] | 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;
|
---|
[13046] | 112 | row.Values.Replace(ages.Select(x => x.Value));
|
---|
[11617] | 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 | } |
---|