Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/Analysis/UpDownWalkAnalyzer.cs

Last change on this file was 16958, checked in by abeham, 6 years ago

#2457: adapted to trunk

File size: 7.4 KB
RevLine 
[7128]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
[16096]22using System.Linq;
[16958]23using HEAL.Attic;
[7128]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
[13583]28using HeuristicLab.Optimization;
[7128]29using HeuristicLab.Parameters;
30
[13583]31namespace HeuristicLab.Analysis.FitnessLandscape {
32  [Item("Up/Down Walk Analyzer", "Analyzes the quality trail produced from an up/down walk.")]
[16958]33  [StorableType("0D054A87-A040-4DC5-A722-0952EE96C924")]
[13583]34  public class UpDownWalkAnalyzer : SingleSuccessorOperator, IQualityTrailAnalyzer {
[7176]35    public bool EnabledByDefault {
36      get { return false; }
37    }
[7128]38
39    #region Parameters
40    public LookupParameter<DataTable> QualityTrailParameter {
41      get { return (LookupParameter<DataTable>)Parameters["Quality Trail"]; }
42    }
[13583]43    public LookupParameter<ResultCollection> ResultsParameter {
44      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
[7128]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]
[16958]67    protected UpDownWalkAnalyzer(StorableConstructorFlag _) : base(_) { }
[7128]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"));
[13583]71      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection of all results of this algorithm"));
[7128]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() {
[13583]85      var qualityTrail = QualityTrailParameter.ActualValue;
[7128]86      if (qualityTrail != null && qualityTrail.Rows.Count > 0) {
87        var qualities = qualityTrail.Rows.First().Values.ToList();
88        if (qualities.Count > 2) {
[13583]89          var results = ResultsParameter.ActualValue;
[7128]90          var extremes = qualities
[13583]91            .Delta((a, b) => new { a, b, diff = b - a })
92            .Select((p, i) => new { p.a, p.b, p.diff, i = i + 1 })
[7128]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();
[13583]108          var tops = Enumerable.Repeat(new { length = 0, value = 0.0 }, 0).ToList();
[7128]109          var bottoms = tops;
110          if (maxima.Count > 0 && minima.Count > 0) {
111            if (maxima.First().i < minima.First().i) {
[13583]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();
[7128]115            } else {
[13583]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();
[7128]119            }
120            if (tops.Count > 0) {
[13583]121              var topLengths = tops.Select(t => (double)t.length).ToList();
[7128]122              var topVals = tops.Select(t => t.value).ToList();
[13583]123              var uv = new DoubleValue(topVals.Variance());
124              UpperVarianceParameter.ActualValue = uv;
[16096]125              results.AddOrUpdateResult(UpperVarianceParameter.Name, uv);
[13583]126              var ul = new DoubleValue(topLengths.Average());
127              UpWalkLengthParameter.ActualValue = ul;
[16096]128              results.AddOrUpdateResult(UpWalkLengthParameter.Name, ul);
[13583]129              var ulv = new DoubleValue(topLengths.Variance());
130              UpWalkLenVarParameter.ActualValue = ulv;
[16096]131              results.AddOrUpdateResult(UpWalkLenVarParameter.Name, ulv);
[7128]132            }
133            if (bottoms.Count > 0) {
[13583]134              var bottomLengths = bottoms.Select(b => (double)b.length).ToList();
[7128]135              var bottomVals = bottoms.Select(b => b.value).ToList();
[13583]136              var lv = new DoubleValue(bottomVals.Variance());
137              LowerVarianceParameter.ActualValue = lv;
[16096]138              results.AddOrUpdateResult(LowerVarianceParameter.Name, lv);
[13583]139              var dl = new DoubleValue(bottomLengths.Average());
140              DownWalkLengthParameter.ActualValue = dl;
[16096]141              results.AddOrUpdateResult(DownWalkLengthParameter.Name, dl);
[13583]142              var dlv = new DoubleValue(bottomLengths.Variance());
143              DownWalkLenVarParameter.ActualValue = dlv;
[16096]144              results.AddOrUpdateResult(DownWalkLenVarParameter.Name, dlv);
[7128]145            }
146          }
147        }
148      }
149      return base.Apply();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.