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