Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/Analysis/UpDownWalkAnalyzer.cs @ 13583

Last change on this file since 13583 was 13583, checked in by abeham, 9 years ago

#2457:

  • Added stripped-down version of FLA branch
  • Added appropriate calculators
  • Fixed detecting maximization in RLD view
File size: 7.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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System.Linq;
30
31namespace HeuristicLab.Analysis.FitnessLandscape {
32  [Item("Up/Down Walk Analyzer", "Analyzes the quality trail produced from an up/down walk.")]
33  [StorableClass]
34  public class UpDownWalkAnalyzer : SingleSuccessorOperator, IQualityTrailAnalyzer {
35    public bool EnabledByDefault {
36      get { return false; }
37    }
38
39    #region Parameters
40    public LookupParameter<DataTable> QualityTrailParameter {
41      get { return (LookupParameter<DataTable>)Parameters["Quality Trail"]; }
42    }
43    public LookupParameter<ResultCollection> ResultsParameter {
44      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
45    }
46    public LookupParameter<DoubleValue> UpWalkLengthParameter {
47      get { return (LookupParameter<DoubleValue>)Parameters["UpWalkLength"]; }
48    }
49    public LookupParameter<DoubleValue> DownWalkLengthParameter {
50      get { return (LookupParameter<DoubleValue>)Parameters["DownWalkLength"]; }
51    }
52    public LookupParameter<DoubleValue> UpWalkLenVarParameter {
53      get { return (LookupParameter<DoubleValue>)Parameters["UpWalkLenVar"]; }
54    }
55    public LookupParameter<DoubleValue> DownWalkLenVarParameter {
56      get { return (LookupParameter<DoubleValue>)Parameters["DownWalkLenVar"]; }
57    }
58    public LookupParameter<DoubleValue> UpperVarianceParameter {
59      get { return (LookupParameter<DoubleValue>)Parameters["UpperVariance"]; }
60    }
61    public LookupParameter<DoubleValue> LowerVarianceParameter {
62      get { return (LookupParameter<DoubleValue>)Parameters["LowerVariance"]; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    protected UpDownWalkAnalyzer(bool deserializing) : base(deserializing) { }
68    protected UpDownWalkAnalyzer(UpDownWalkAnalyzer original, Cloner cloner) : base(original, cloner) { }
69    public UpDownWalkAnalyzer() {
70      Parameters.Add(new LookupParameter<DataTable>("Quality Trail", "The qualities of the solutions"));
71      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection of all results of this algorithm"));
72      Parameters.Add(new LookupParameter<DoubleValue>("DownWalkLength", "Average downward walk length."));
73      Parameters.Add(new LookupParameter<DoubleValue>("UpWalkLength", "Average updward walk length."));
74      Parameters.Add(new LookupParameter<DoubleValue>("UpWalkLenVar", "Upward walk length variance."));
75      Parameters.Add(new LookupParameter<DoubleValue>("DownWalkLenVar", "Downward walk length variance."));
76      Parameters.Add(new LookupParameter<DoubleValue>("LowerVariance", "Lower level variance."));
77      Parameters.Add(new LookupParameter<DoubleValue>("UpperVariance", "Upper level variance."));
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new UpDownWalkAnalyzer(this, cloner);
82    }
83
84    public override IOperation Apply() {
85      var qualityTrail = QualityTrailParameter.ActualValue;
86      if (qualityTrail != null && qualityTrail.Rows.Count > 0) {
87        var qualities = qualityTrail.Rows.First().Values.ToList();
88        if (qualities.Count > 2) {
89          var results = ResultsParameter.ActualValue;
90          var extremes = qualities
91            .Delta((a, b) => new { a, b, diff = b - a })
92            .Select((p, i) => new { p.a, p.b, p.diff, i = i + 1 })
93            .Delta((s1, s2) => new {
94              s1.i,
95              value = s2.a,
96              top = s1.diff >= 0 && s2.diff < 0,
97              bottom = s1.diff <= 0 && s2.diff > 0
98            })
99            .Where(x => x.top || x.bottom)
100            .GroupConsecutive(x => x.bottom)
101            .Select(g => g.Count() == 1
102                           ? g.First()
103                           : (g.First().bottom
104                                ? g.OrderBy(x => x.value).First()
105                                : g.OrderByDescending(x => x.value).First())).ToList();
106          var maxima = extremes.Where(x => x.top).ToList();
107          var minima = extremes.Where(x => x.bottom).ToList();
108          var tops = Enumerable.Repeat(new { length = 0, value = 0.0 }, 0).ToList();
109          var bottoms = tops;
110          if (maxima.Count > 0 && minima.Count > 0) {
111            if (maxima.First().i < minima.First().i) {
112              bottoms = maxima.Zip(minima, (t, b) => new { length = b.i - t.i, b.value }).ToList();
113              minima.Insert(0, new { i = -1, value = 0.0, top = false, bottom = false });
114              tops = maxima.Zip(minima, (t, b) => new { length = t.i - b.i, t.value }).ToList();
115            } else {
116              tops = maxima.Zip(minima, (t, b) => new { length = t.i - b.i, t.value }).ToList();
117              maxima.Insert(0, new { i = -1, value = 0.0, top = false, bottom = false });
118              bottoms = maxima.Zip(minima, (t, b) => new { length = b.i - t.i, b.value }).ToList();
119            }
120            if (tops.Count > 0) {
121              var topLengths = tops.Select(t => (double)t.length).ToList();
122              var topVals = tops.Select(t => t.value).ToList();
123              var uv = new DoubleValue(topVals.Variance());
124              UpperVarianceParameter.ActualValue = uv;
125              AddOrUpdateResult(results, UpperVarianceParameter.Name, uv);
126              var ul = new DoubleValue(topLengths.Average());
127              UpWalkLengthParameter.ActualValue = ul;
128              AddOrUpdateResult(results, UpWalkLengthParameter.Name, ul);
129              var ulv = new DoubleValue(topLengths.Variance());
130              UpWalkLenVarParameter.ActualValue = ulv;
131              AddOrUpdateResult(results, UpWalkLenVarParameter.Name, ulv);
132            }
133            if (bottoms.Count > 0) {
134              var bottomLengths = bottoms.Select(b => (double)b.length).ToList();
135              var bottomVals = bottoms.Select(b => b.value).ToList();
136              var lv = new DoubleValue(bottomVals.Variance());
137              LowerVarianceParameter.ActualValue = lv;
138              AddOrUpdateResult(results, LowerVarianceParameter.Name, lv);
139              var dl = new DoubleValue(bottomLengths.Average());
140              DownWalkLengthParameter.ActualValue = dl;
141              AddOrUpdateResult(results, DownWalkLengthParameter.Name, dl);
142              var dlv = new DoubleValue(bottomLengths.Variance());
143              DownWalkLenVarParameter.ActualValue = dlv;
144              AddOrUpdateResult(results, DownWalkLenVarParameter.Name, dlv);
145            }
146          }
147        }
148      }
149      return base.Apply();
150    }
151
152    private static void AddOrUpdateResult(ResultCollection results, string name, IItem item, bool clone = false) {
153      IResult r;
154      if (!results.TryGetValue(name, out r)) {
155        results.Add(new Result(name, clone ? (IItem)item.Clone() : item));
156      } else r.Value = clone ? (IItem)item.Clone() : item;
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.