[5994] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16728] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5994] | 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.Optimization;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[16728] | 28 | using HEAL.Attic;
|
---|
[15720] | 29 | using System.Collections.Generic;
|
---|
| 30 | using System.Linq;
|
---|
[5994] | 31 |
|
---|
| 32 | namespace HeuristicLab.Analysis.QualityAnalysis {
|
---|
[6013] | 33 | [Item("QualityDistributionAnalyzer", "Analyzes the distribution of the quality values in that it adds a Histogram of them into the result collection.")]
|
---|
[16728] | 34 | [StorableType("9B339DF1-A9D3-4C9C-B78C-84B764715EB8")]
|
---|
[11970] | 35 | public class QualityDistributionAnalyzer : SingleSuccessorOperator, IAnalyzer, IIterationBasedOperator, ISingleObjectiveOperator {
|
---|
[15720] | 36 | private const string TableDescription = "Shows the quality distributions in the current population.";
|
---|
[5994] | 37 |
|
---|
| 38 | #region Parameter properties
|
---|
| 39 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 40 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 41 | }
|
---|
| 42 | public IValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 43 | get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 44 | }
|
---|
[6013] | 45 | private ValueParameter<StringValue> HistogramNameParameter {
|
---|
| 46 | get { return (ValueParameter<StringValue>)Parameters["HistogramName"]; }
|
---|
[5994] | 47 | }
|
---|
[6013] | 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 | }
|
---|
[5994] | 57 | #endregion
|
---|
| 58 |
|
---|
[7172] | 59 | public virtual bool EnabledByDefault {
|
---|
| 60 | get { return true; }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[6013] | 63 | public string HistogramName {
|
---|
| 64 | get { return HistogramNameParameter.Value.Value; }
|
---|
| 65 | set { HistogramNameParameter.Value.Value = value; }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public bool StoreHistory {
|
---|
| 69 | get { return StoreHistoryParameter.Value.Value; }
|
---|
| 70 | set { StoreHistoryParameter.Value.Value = value; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[5994] | 73 | [StorableConstructor]
|
---|
[16728] | 74 | protected QualityDistributionAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
[5994] | 75 | protected QualityDistributionAnalyzer(QualityDistributionAnalyzer original, Cloner cloner)
|
---|
| 76 | : base(original, cloner) {
|
---|
| 77 | }
|
---|
| 78 | public QualityDistributionAnalyzer()
|
---|
| 79 | : base() {
|
---|
| 80 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 81 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection where the analysis values should be stored."));
|
---|
[6013] | 82 | Parameters.Add(new FixedValueParameter<StringValue>("HistogramName", "The name of the histogram that gets injected in to the results collection.", new StringValue("Quality 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 | QualityParameter.Hidden = true;
|
---|
| 88 | ResultsParameter.Hidden = true;
|
---|
| 89 | IterationsParameter.Hidden = true;
|
---|
| 90 | MaximumIterationsParameter.Hidden = true;
|
---|
[5994] | 91 | }
|
---|
| 92 |
|
---|
| 93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 94 | return new QualityDistributionAnalyzer(this, cloner);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[15720] | 97 | public static DataTable PrepareTable(string qualityName = "Quality") {
|
---|
| 98 | var result = new DataTable("Population Quality Distribution", TableDescription);
|
---|
| 99 | result.VisualProperties.XAxisTitle = qualityName;
|
---|
| 100 | result.VisualProperties.YAxisTitle = "Frequency";
|
---|
| 101 |
|
---|
| 102 | var row = new DataRow("QualityDistribution");
|
---|
| 103 | row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
|
---|
| 104 | result.Rows.Add(row);
|
---|
| 105 |
|
---|
| 106 | return result;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public static void UpdateTable(DataTable table, IEnumerable<double> qualities) {
|
---|
| 110 | var row = table.Rows["QualityDistribution"];
|
---|
| 111 | row.Values.Replace(qualities);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[5994] | 114 | public override IOperation Apply() {
|
---|
[6013] | 115 | DataTable qualityDistribution = null;
|
---|
[15720] | 116 | var results = ResultsParameter.ActualValue;
|
---|
[6013] | 117 | if (results.ContainsKey(HistogramName)) {
|
---|
[15720] | 118 | qualityDistribution = (DataTable)results[HistogramName].Value;
|
---|
[5994] | 119 | } else {
|
---|
[15720] | 120 | qualityDistribution = PrepareTable(QualityParameter.ActualName);
|
---|
| 121 | results.Add(new Result(HistogramName, TableDescription, qualityDistribution));
|
---|
[5994] | 122 | }
|
---|
| 123 |
|
---|
[15720] | 124 | UpdateTable(qualityDistribution, QualityParameter.ActualValue.Select(x => x.Value));
|
---|
| 125 |
|
---|
[6013] | 126 | if (StoreHistory) {
|
---|
[15720] | 127 | var historyResultName = HistogramName + " History";
|
---|
[6013] | 128 | DataTableHistory qdHistory = null;
|
---|
| 129 | if (results.ContainsKey(historyResultName)) {
|
---|
[15720] | 130 | qdHistory = (DataTableHistory)results[historyResultName].Value;
|
---|
[6013] | 131 | } else {
|
---|
| 132 | qdHistory = new DataTableHistory();
|
---|
| 133 | results.Add(new Result(historyResultName, qdHistory));
|
---|
| 134 | }
|
---|
[15720] | 135 | var table = (DataTable)qualityDistribution.Clone();
|
---|
| 136 | var iteration = IterationsParameter.ActualValue;
|
---|
[6013] | 137 | if (iteration != null) {
|
---|
[15720] | 138 | var iterationName = IterationsParameter.ActualName;
|
---|
[6013] | 139 | if (iterationName.EndsWith("s")) iterationName = iterationName.Remove(iterationName.Length - 1);
|
---|
[15720] | 140 | var appendix = " at " + iterationName + " " + iteration.Value.ToString();
|
---|
[6013] | 141 | table.Name += appendix;
|
---|
[6628] | 142 | table.Rows["QualityDistribution"].VisualProperties.DisplayName += appendix;
|
---|
[6013] | 143 | }
|
---|
| 144 | qdHistory.Add(table);
|
---|
| 145 | }
|
---|
[5994] | 146 | return base.Apply();
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|