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