[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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
[13583] | 26 | using HeuristicLab.Optimization;
|
---|
[7128] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[13583] | 29 | using System.Linq;
|
---|
[7128] | 30 |
|
---|
[13583] | 31 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
| 32 | [Item("Up/Down Walk Analyzer", "Analyzes the quality trail produced from an up/down walk.")]
|
---|
[7128] | 33 | [StorableClass]
|
---|
[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]
|
---|
| 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"));
|
---|
[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;
|
---|
| 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);
|
---|
[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;
|
---|
| 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);
|
---|
[7128] | 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | return base.Apply();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[13583] | 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;
|
---|
[7128] | 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|