Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/PopDist/PopulationDistributionAnalyzer.cs @ 9940

Last change on this file since 9940 was 8172, checked in by gkronber, 12 years ago

#1797: adapted return type of ConstrainedValueParameter properties in FLA branch.

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.IO;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Analysis.FitnessLandscape {
35
36  [Item("PopulationDistributionAnalyzer", "An operator that analyzes the distribution of fitness values")]
37  [StorableClass]
38  public class PopulationDistributionAnalyzer : AlgorithmOperator, IAnalyzer {
39    public bool EnabledByDefault {
40      get { return false; }
41    }
42
43    #region Parameters
44    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
45      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47    public ValueLookupParameter<DataTable> FitnessQuantilesParameter {
48      get { return (ValueLookupParameter<DataTable>)Parameters["Fitness Quantiles"]; }
49    }
50    public ValueLookupParameter<DataTable> PopulationDispersionParameter {
51      get { return (ValueLookupParameter<DataTable>)Parameters["Population Dispersion"]; }
52    }
53    public ValueLookupParameter<DataTable> HigherPopulationMomentsParameter {
54      get { return (ValueLookupParameter<DataTable>)Parameters["Higher Population Moments"]; }
55    }
56    public ValueLookupParameter<DataTable> PopulationNormalityParameter {
57      get { return (ValueLookupParameter<DataTable>)Parameters["Population Normality"]; }
58    }
59    public ValueLookupParameter<VariableCollection> ResultsParameter {
60      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
61    }
62    public ValueLookupParameter<ResultCollection> PopulationDistributionResultsParameter {
63      get { return (ValueLookupParameter<ResultCollection>)Parameters["Population Distribution Results"]; }
64    }
65    public OptionalValueParameter<StringValue> PopulationLogFileNameParameter {
66      get { return (OptionalValueParameter<StringValue>)Parameters["Population Log File Name"]; }
67    }
68    public IConstrainedValueParameter<IntValue> NQuantilesParameter {
69      get { return (IConstrainedValueParameter<IntValue>)Parameters["NQuantiles"]; }
70    }
71    #endregion
72
73    [StorableConstructor]
74    protected PopulationDistributionAnalyzer(bool deserializing) : base(deserializing) { }
75    protected PopulationDistributionAnalyzer(PopulationDistributionAnalyzer original, Cloner cloner) : base(original, cloner) { }
76
77    public PopulationDistributionAnalyzer() {
78      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the solution"));
79      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The collection of all results of this algorithm"));
80      Parameters.Add(new ValueLookupParameter<ResultCollection>("Population Distribution Results", "All results from population distribution analysis"));
81      Parameters.Add(new ValueLookupParameter<DataTable>("Fitness Quantiles", "Data table with quantiles of the fitness distribution"));
82      Parameters.Add(new ValueLookupParameter<DataTable>("Population Dispersion", "Data table dispersion statistics"));
83      Parameters.Add(new ValueLookupParameter<DataTable>("Higher Population Moments", "Data table skewness and kurtosis of population's fitness distribution"));
84      Parameters.Add(new ValueLookupParameter<DataTable>("Population Normality", "Jarque-Bera Normality Test p-value and 0.05 threshold"));
85      Parameters.Add(new OptionalValueParameter<StringValue>("Population Log File Name", "File name of a log file where all population fittness values are logged to"));
86      Parameters.Add(new ConstrainedValueParameter<IntValue>("NQuantiles", "Number of quantiles to plot", new ItemSet<IntValue>(Enumerable.Range(1, 50).Select(v => new IntValue(v)))));
87
88      NQuantilesParameter.Value = NQuantilesParameter.ValidValues.Single(v => v.Value == 10);
89
90      var resultsCollector = new ResultsCollector();
91      resultsCollector.ResultsParameter.ActualName = PopulationDistributionResultsParameter.Name;
92      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(FitnessQuantilesParameter.Name));
93      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(PopulationDispersionParameter.Name));
94      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(HigherPopulationMomentsParameter.Name));
95      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(PopulationNormalityParameter.Name));
96
97      var globalResultsCollector = new ResultsCollector();
98      globalResultsCollector.CollectedValues.Add(new ValueLookupParameter<ResultCollection>(PopulationDistributionResultsParameter.Name));
99
100      OperatorGraph.InitialOperator = resultsCollector;
101      resultsCollector.Successor = globalResultsCollector;
102      globalResultsCollector.Successor = null;
103    }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new PopulationDistributionAnalyzer(this, cloner);
107    }
108
109    public override IOperation Apply() {
110      if (PopulationDistributionResultsParameter.ActualValue == null)
111        PopulationDistributionResultsParameter.ActualValue = new ResultCollection();
112      var qualities = QualityParameter.ActualValue.Select(v => v.Value).ToArray();
113      CalculateQuantiles(qualities);
114      CalculateDistributionParameters(qualities);
115      LogPopulationFitnessValues(qualities);
116      return base.Apply();
117    }
118
119    private void CalculateQuantiles(double[] qualities) {
120      DataTable quantiles = FitnessQuantilesParameter.ActualValue;
121      if (quantiles == null) {
122        quantiles = new DataTable("Fitness Quantiles");
123        quantiles.Description = "The population's fitness quantiles";
124        FitnessQuantilesParameter.ActualValue = quantiles;
125        for (int i = 0; i <= NQuantilesParameter.Value.Value; i++)
126          quantiles.Rows.Add(new DataRow((i * 10).ToString()));
127      }
128      int n_quantiles = quantiles.Rows.Count;
129      for (int i = 0; i < n_quantiles; i++) {
130        double v = 0;
131        alglib.basestat.samplepercentile(qualities, qualities.Length, 1.0 * i / n_quantiles, ref v);
132        quantiles.Rows[(i * 10).ToString()].Values.Add(v);
133      }
134    }
135
136    private void CalculateDistributionParameters(double[] qualities) {
137      DataTable populationDispersion = PopulationDispersionParameter.ActualValue;
138      if (populationDispersion == null) {
139        populationDispersion = new DataTable("Population Dispersion");
140        PopulationDispersionParameter.ActualValue = populationDispersion;
141        populationDispersion.Rows.Add(new DataRow("Std. Deviation"));
142        populationDispersion.Rows.Add(new DataRow("Mean Difference"));
143      }
144      DataTable higherPopulationMoments = HigherPopulationMomentsParameter.ActualValue;
145      if (higherPopulationMoments == null) {
146        higherPopulationMoments = new DataTable("Higher Population Moments");
147        HigherPopulationMomentsParameter.ActualValue = higherPopulationMoments;
148        higherPopulationMoments.Rows.Add(new DataRow("Skewness"));
149        higherPopulationMoments.Rows.Add(new DataRow("Kurtosis"));
150      }
151      DataTable populationNormality = PopulationNormalityParameter.ActualValue;
152      if (populationNormality == null) {
153        populationNormality = new DataTable("Population Normality");
154        PopulationNormalityParameter.ActualValue = populationNormality;
155        populationNormality.Rows.Add(new DataRow("Jarque-Bera P-Value"));
156        populationNormality.Rows.Add(new DataRow("0.05"));
157      }
158
159      double mean, variance, skewness, kurtosis, p_value;
160      mean = variance = skewness = kurtosis = p_value = 0;
161      alglib.basestat.samplemoments(qualities, qualities.Length, ref mean, ref variance, ref skewness, ref kurtosis);
162      alglib.jarquebera.jarqueberatest(qualities, qualities.Length, ref p_value);
163      double mean_difference =
164        (from i in Enumerable.Range(0, qualities.Length)
165         from j in Enumerable.Range(0, i)
166         select Math.Abs(qualities[i] - qualities[j])).Sum()
167           * 2 / qualities.Length / (qualities.Length - 1);
168
169      populationDispersion.Rows["Std. Deviation"].Values.Add(Math.Sqrt(variance));
170      populationDispersion.Rows["Mean Difference"].Values.Add(mean_difference);
171      higherPopulationMoments.Rows["Skewness"].Values.Add(skewness);
172      higherPopulationMoments.Rows["Kurtosis"].Values.Add(kurtosis);
173      populationNormality.Rows["Jarque-Bera P-Value"].Values.Add(p_value);
174      populationNormality.Rows["0.05"].Values.Add(0.05);
175    }
176
177    private void LogPopulationFitnessValues(double[] qualities) {
178      if (PopulationLogFileNameParameter.ActualValue == null)
179        return;
180      using (var writer = new StreamWriter(PopulationLogFileNameParameter.Value.Value, true)) {
181        foreach (var q in qualities) {
182          writer.Write(q);
183          writer.Write(";");
184        }
185        writer.WriteLine();
186        writer.Close();
187      }
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.