Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/UpDownWalkAnalyzer.cs @ 10151

Last change on this file since 10151 was 7176, checked in by gkronber, 13 years ago

#1696 adapted analyzers to compile with changes of r7172 (#1584)

File size: 10.4 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Analysis.FitnessLandscape.Analysis {
35
36  [StorableClass]
37  public class UpDownWalkAnalyzer : AlgorithmOperator, IQualityTrailAnalyzer {
38    public bool EnabledByDefault {
39      get { return false; }
40    }
41
42    #region Parameters
43    public LookupParameter<DataTable> QualityTrailParameter {
44      get { return (LookupParameter<DataTable>)Parameters["Quality Trail"]; }
45    }
46    public LookupParameter<VariableCollection> ResultsParameter {
47      get { return (LookupParameter<VariableCollection>)Parameters["Results"]; }
48    }
49    public LookupParameter<DataTable> UpDownStepsParameter {
50      get { return (LookupParameter<DataTable>)Parameters["UpDownSteps"]; }
51    }
52    public LookupParameter<DataTable> UpDownLevelsParameter {
53      get { return (LookupParameter<DataTable>)Parameters["UpDownLevels"]; }
54    }
55    public LookupParameter<DoubleValue> UpWalkLengthParameter {
56      get { return (LookupParameter<DoubleValue>)Parameters["UpWalkLength"]; }
57    }
58    public LookupParameter<DoubleValue> DownWalkLengthParameter {
59      get { return (LookupParameter<DoubleValue>)Parameters["DownWalkLength"]; }
60    }
61    public LookupParameter<DoubleValue> UpWalkLenVarParameter {
62      get { return (LookupParameter<DoubleValue>)Parameters["UpWalkLenVar"]; }
63    }
64    public LookupParameter<DoubleValue> DownWalkLenVarParameter {
65      get { return (LookupParameter<DoubleValue>)Parameters["DownWalkLenVar"]; }
66    }
67    public LookupParameter<DoubleValue> UpperLevelParameter {
68      get { return (LookupParameter<DoubleValue>)Parameters["UpperLevel"]; }
69    }
70    public LookupParameter<DoubleValue> LowerLevelParameter {
71      get { return (LookupParameter<DoubleValue>)Parameters["LowerLevel"]; }
72    }
73    public LookupParameter<DoubleValue> UpperVarianceParameter {
74      get { return (LookupParameter<DoubleValue>)Parameters["UpperVariance"]; }
75    }
76    public LookupParameter<DoubleValue> LowerVarianceParameter {
77      get { return (LookupParameter<DoubleValue>)Parameters["LowerVariance"]; }
78    }
79    #endregion
80
81    [StorableConstructor]
82    protected UpDownWalkAnalyzer(bool deserializing) : base(deserializing) { }
83    protected UpDownWalkAnalyzer(UpDownWalkAnalyzer original, Cloner cloner) : base(original, cloner) { }
84
85    public UpDownWalkAnalyzer() {
86      Parameters.Add(new LookupParameter<DataTable>("Quality Trail", "The qualities of the solutions"));
87      Parameters.Add(new LookupParameter<VariableCollection>("Results", "The collection of all results of this algorithm"));
88      Parameters.Add(new LookupParameter<DataTable>("UpDownSteps", "Distribution of upward and downward steps between extremes."));
89      Parameters.Add(new LookupParameter<DataTable>("UpDownLevels", "Distribution of upper and lower levels in up-down walks."));
90      Parameters.Add(new LookupParameter<DoubleValue>("DownWalkLength", "Average downward walk length."));
91      Parameters.Add(new LookupParameter<DoubleValue>("UpWalkLength", "Average updward walk length."));
92      Parameters.Add(new LookupParameter<DoubleValue>("UpWalkLenVar", "Upward walk length variance."));
93      Parameters.Add(new LookupParameter<DoubleValue>("DownWalkLenVar", "Downward walk length variance."));
94      Parameters.Add(new LookupParameter<DoubleValue>("UpperLevel", "Average maximum fitness value."));
95      Parameters.Add(new LookupParameter<DoubleValue>("LowerLevel", "Average minimum fitness value."));
96      Parameters.Add(new LookupParameter<DoubleValue>("LowerVariance", "Lower level variance."));
97      Parameters.Add(new LookupParameter<DoubleValue>("UpperVariance", "Upper level variance."));
98   
99
100      var resultsCollector = new ResultsCollector();
101      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(UpDownStepsParameter.Name));
102      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(UpDownLevelsParameter.Name));
103      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(DownWalkLengthParameter.Name));
104      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(UpWalkLengthParameter.Name));
105      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(UpWalkLenVarParameter.Name));
106      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(DownWalkLenVarParameter.Name));
107      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(UpperLevelParameter.Name));
108      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(LowerLevelParameter.Name));
109      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(UpperVarianceParameter.Name));
110      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(LowerVarianceParameter.Name));
111
112      OperatorGraph.InitialOperator = resultsCollector;
113      resultsCollector.Successor = null;
114    }
115
116    public override IDeepCloneable Clone(Cloner cloner) {
117      return new UpDownWalkAnalyzer(this, cloner);
118    }
119
120    public override IOperation Apply() {
121      var stepsTable = GetOrCreateTable(UpDownStepsParameter, "Up", "Down");
122      var levelsTable = GetOrCreateTable(UpDownLevelsParameter, "Top", "Bottom");
123      DataTable qualityTrail = QualityTrailParameter.ActualValue;
124      if (qualityTrail != null && qualityTrail.Rows.Count > 0) {
125        var qualities = qualityTrail.Rows.First().Values.ToList();
126        if (qualities.Count > 2) {
127          var extremes = qualities
128            .Delta((a, b) => new {a, b, diff = b - a})
129            .Select((p, i) => new {p.a, p.b, p.diff, i = i+1})
130            .Delta((s1, s2) => new {
131              s1.i,
132              value = s2.a,
133              top = s1.diff >= 0 && s2.diff < 0,
134              bottom = s1.diff <= 0 && s2.diff > 0
135            })
136            .Where(x => x.top || x.bottom)
137            .GroupConsecutive(x => x.bottom)
138            .Select(g => g.Count() == 1
139                           ? g.First()
140                           : (g.First().bottom
141                                ? g.OrderBy(x => x.value).First()
142                                : g.OrderByDescending(x => x.value).First())).ToList();
143          var maxima = extremes.Where(x => x.top).ToList();
144          var minima = extremes.Where(x => x.bottom).ToList();
145          var tops = Enumerable.Repeat(new {length = 0, value = 0.0}, 0).ToList();
146          var bottoms = tops;
147          if (maxima.Count > 0 && minima.Count > 0) {
148            if (maxima.First().i < minima.First().i) {
149              bottoms = maxima.Zip(minima, (t, b) => new {length = b.i - t.i, b.value}).ToList();
150              minima.Insert(0, new {i = -1, value = 0.0, top = false, bottom = false});
151              tops = maxima.Zip(minima, (t, b) => new {length = t.i - b.i, t.value}).ToList();
152            } else {
153              tops = maxima.Zip(minima, (t, b) => new {length = t.i - b.i, t.value}).ToList();
154              maxima.Insert(0, new {i = -1, value = 0.0, top = false, bottom = false});
155              bottoms = maxima.Zip(minima, (t, b) => new {length = b.i - t.i, b.value}).ToList();
156            }
157            if (tops.Count > 0) {
158              var topLengths = tops.Select(t => (double) t.length).ToList();
159              var topVals = tops.Select(t => t.value).ToList();
160              ReplaceHistogram(stepsTable.Rows["Up"], topLengths);
161              ReplaceHistogram(levelsTable.Rows["Top"], topVals);
162              UpperLevelParameter.ActualValue = new DoubleValue(topVals.Average());
163              UpperVarianceParameter.ActualValue = new DoubleValue(topVals.Variance());
164              UpWalkLengthParameter.ActualValue = new DoubleValue(topLengths.Average());
165              UpWalkLenVarParameter.ActualValue = new DoubleValue(topLengths.Variance());
166            }
167            if (bottoms.Count > 0) {
168              var bottomLengths = bottoms.Select(b => (double) b.length).ToList();
169              var bottomVals = bottoms.Select(b => b.value).ToList();
170              ReplaceHistogram(stepsTable.Rows["Down"], bottomLengths);
171              ReplaceHistogram(levelsTable.Rows["Bottom"], bottomVals);
172              LowerLevelParameter.ActualValue = new DoubleValue(bottomVals.Average());
173              LowerVarianceParameter.ActualValue = new DoubleValue(bottomVals.Variance());
174              DownWalkLengthParameter.ActualValue = new DoubleValue(bottomLengths.Average());
175              DownWalkLenVarParameter.ActualValue = new DoubleValue(bottomLengths.Variance());
176            }
177          }
178        }
179      }
180      return base.Apply();
181    }
182
183    private void ReplaceHistogram(DataRow row, IEnumerable<double> values) {
184      row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
185      row.VisualProperties.Bins = 25;
186      row.Values.Clear();
187      row.Values.AddRange(values);
188    }
189
190    private DataTable GetOrCreateTable(LookupParameter<DataTable> tableParameter, params string[] rowNames) {
191      DataTable table = tableParameter.ActualValue;
192      if (table == null) {
193        table = new DataTable(tableParameter.Name, tableParameter.Description);
194        tableParameter.ActualValue = table;
195        foreach (var name in rowNames) {
196          table.Rows.Add(new DataRow(name));
197        }
198      }
199      return table;
200    }
201  }
202}
Note: See TracBrowser for help on using the repository browser.