Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemCharacteristicAnalysis/QAP/QAPDirectedWalk.cs @ 14690

Last change on this file since 14690 was 14690, checked in by abeham, 7 years ago

#2457: working on identification of problem instances

File size: 15.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Encodings.PermutationEncoding;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.QuadraticAssignment;
30using HeuristicLab.Random;
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Threading;
35
36namespace HeuristicLab.Analysis.FitnessLandscape {
37  [Item("Directed Walk (QAP-specific)", "")]
38  [StorableClass]
39  public class QAPDirectedWalk : CharacteristicCalculator {
40   
41    public IFixedValueParameter<IntValue> PathsParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters["Paths"]; }
43    }
44
45    public IFixedValueParameter<BoolValue> BestImprovementParameter {
46      get { return (IFixedValueParameter<BoolValue>)Parameters["BestImprovement"]; }
47    }
48
49    public IValueParameter<IntValue> SeedParameter {
50      get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
51    }
52
53    public IFixedValueParameter<BoolValue> LocalOptimaParameter {
54      get { return (IFixedValueParameter<BoolValue>)Parameters["LocalOptima"]; }
55    }
56
57    public int Paths {
58      get { return PathsParameter.Value.Value; }
59      set { PathsParameter.Value.Value = value; }
60    }
61
62    public bool BestImprovement {
63      get { return BestImprovementParameter.Value.Value; }
64      set { BestImprovementParameter.Value.Value = value; }
65    }
66
67    public int? Seed {
68      get { return SeedParameter.Value != null ? SeedParameter.Value.Value : (int?)null; }
69      set { SeedParameter.Value = value.HasValue ? new IntValue(value.Value) : null; }
70    }
71
72    public bool LocalOptima {
73      get { return LocalOptimaParameter.Value.Value; }
74      set { LocalOptimaParameter.Value.Value = value; }
75    }
76
77    [StorableConstructor]
78    private QAPDirectedWalk(bool deserializing) : base(deserializing) { }
79    private QAPDirectedWalk(QAPDirectedWalk original, Cloner cloner) : base(original, cloner) { }
80    public QAPDirectedWalk() {
81      characteristics.AddRange(new[] { "Swap2.Sharpness", "Swap2.Bumpiness", "Swap2.Flatness", "Swap2.Steadiness" }
82        .Select(x => new StringValue(x)).ToList());
83      Parameters.Add(new FixedValueParameter<IntValue>("Paths", "The number of paths to explore (a path is a set of solutions that connect two randomly chosen solutions).", new IntValue(50)));
84      Parameters.Add(new FixedValueParameter<BoolValue>("BestImprovement", "Whether the best of all alternatives should be chosen for each step in the path or just the first improving (least degrading) move should be made.", new BoolValue(true)));
85      Parameters.Add(new OptionalValueParameter<IntValue>("Seed", "The seed for the random number generator."));
86      Parameters.Add(new FixedValueParameter<BoolValue>("LocalOptima", "Whether to perform walks between local optima.", new BoolValue(false)));
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new QAPDirectedWalk(this, cloner);
91    }
92
93    public override bool CanCalculate() {
94      return Problem is QuadraticAssignmentProblem;
95    }
96
97    public override IEnumerable<IResult> Calculate() {
98      IRandom random = Seed.HasValue ? new MersenneTwister((uint)Seed.Value) : new MersenneTwister();
99      var qap = (QuadraticAssignmentProblem)Problem;
100      var pathCount = Paths;
101
102      var perm = new Permutation(PermutationTypes.Absolute, qap.Weights.Rows, random);
103      if (LocalOptima) {
104        var fit = new DoubleValue(QAPEvaluator.Apply(perm, qap.Weights, qap.Distances));
105        QAPExhaustiveSwap2LocalImprovement.ImproveFast(perm, qap.Weights, qap.Distances, fit, new IntValue(0), new IntValue(0), qap.Maximization.Value, int.MaxValue, CancellationToken.None);
106      }
107      var permutations = new List<Permutation> { perm };
108      while (permutations.Count < pathCount + 1) {
109        perm = (Permutation)permutations.Last().Clone();
110        BiasedShuffle(perm, random);
111        if (LocalOptima) {
112          var fit = new DoubleValue(QAPEvaluator.Apply(perm, qap.Weights, qap.Distances));
113          QAPExhaustiveSwap2LocalImprovement.ImproveFast(perm, qap.Weights, qap.Distances, fit, new IntValue(0), new IntValue(0), qap.Maximization.Value, int.MaxValue, CancellationToken.None);
114        }
115        if (HammingSimilarityCalculator.CalculateSimilarity(permutations.Last(), perm) < 0.75)
116          permutations.Add(perm);
117      }
118
119      var trajectories = Run(random, (QuadraticAssignmentProblem)Problem, permutations, BestImprovement).ToList();
120      var firstDerivatives = trajectories.Select(path => ApproximateDerivative(path).ToList()).ToList();
121      var secondDerivatives = firstDerivatives.Select(d1 => ApproximateDerivative(d1).ToList()).ToList();
122     
123      var props = GetCharacteristics(trajectories, firstDerivatives, secondDerivatives).ToDictionary(x => x.Item1, x => x.Item2);
124      foreach (var chara in characteristics.CheckedItems.Select(x => x.Value.Value)) {
125        if (chara == "Swap2.Sharpness") yield return new Result("Swap2.Sharpness", new DoubleValue(props["Sharpness"]));
126        if (chara == "Swap2.Bumpiness") yield return new Result("Swap2.Bumpiness", new DoubleValue(props["Bumpiness"]));
127        if (chara == "Swap2.Flatness") yield return new Result("Swap2.Flatness", new DoubleValue(props["Flatness"]));
128        if (chara == "Swap2.Steadiness") yield return new Result("Swap2.Steadiness", new DoubleValue(props["Steadiness"]));
129      }
130    }
131
132    public static IEnumerable<IResult> Calculate(List<List<Tuple<Permutation, double>>> trajectories) {
133      var firstDerivatives = trajectories.Select(path => ApproximateDerivative(path).ToList()).ToList();
134      var secondDerivatives = firstDerivatives.Select(d1 => ApproximateDerivative(d1).ToList()).ToList();
135
136      var props = GetCharacteristics(trajectories, firstDerivatives, secondDerivatives).ToDictionary(x => x.Item1, x => x.Item2);
137      yield return new Result("Swap2.Sharpness", new DoubleValue(props["Sharpness"]));
138      yield return new Result("Swap2.Bumpiness", new DoubleValue(props["Bumpiness"]));
139      yield return new Result("Swap2.Flatness", new DoubleValue(props["Flatness"]));
140      yield return new Result("Swap2.Steadiness", new DoubleValue(props["Steadiness"]));
141    }
142
143    public static IEnumerable<List<Tuple<Permutation, double>>> Run(IRandom random, QuadraticAssignmentProblem qap, IEnumerable<Permutation> permutations, bool bestImprovement = true) {
144      var iter = permutations.GetEnumerator();
145      if (!iter.MoveNext()) yield break;
146
147      var min = qap.LowerBound.Value;
148      var max = qap.AverageQuality.Value;
149
150      var start = iter.Current;
151      while (iter.MoveNext()) {
152        var end = iter.Current;
153
154        var walk = (bestImprovement ? BestDirectedWalk(qap, start, end) : FirstDirectedWalk(random, qap, start, end)).ToList();
155        yield return walk.Select(x => Tuple.Create(x.Item1, (x.Item2 - min) / (max - min))).ToList();
156        start = end;
157      } // end paths
158    }
159
160    private static IEnumerable<Tuple<string, double>> GetCharacteristics(List<List<Tuple<Permutation, double>>> f, List<List<Tuple<Permutation, double>>> f1, List<List<Tuple<Permutation, double>>> f2) {
161      var sharpness = f2.Average(x => Area(x));
162      var bumpiness = 0.0;
163      var flatness = 0.0;
164      var downPointing = f1.Where(x => x.Min(y => y.Item2) < 0).ToList();
165
166      var steadiness = 0.0;
167      foreach (var path in downPointing) {
168        steadiness += ComBelowZero(path);
169      }
170      if (downPointing.Count > 0) steadiness /= downPointing.Count;
171
172      for (var p = 0; p < f2.Count; p++) {
173        if (f2[p].Count <= 2) continue;
174        var bump = 0;
175        var flat = 0;
176        for (var i = 0; i < f2[p].Count - 1; i++) {
177          if ((f2[p][i].Item2 > 0 && f2[p][i + 1].Item2 < 0) || (f2[p][i].Item2 < 0 && f2[p][i + 1].Item2 > 0)) {
178            bump++;
179          } else if (f2[p][i].Item2 == 0) {
180            flat++;
181          }
182        }
183        bumpiness += bump / (f2[p].Count - 1.0);
184        flatness += flat / (f2[p].Count - 1.0);
185      }
186      bumpiness /= f2.Count;
187      flatness /= f2.Count;
188      return new[] {
189      Tuple.Create("Sharpness", sharpness),
190      Tuple.Create("Bumpiness", bumpiness),
191      Tuple.Create("Flatness", flatness),
192      Tuple.Create("Steadiness", steadiness)
193    };
194    }
195
196    public static IEnumerable<Tuple<Permutation, double>> BestDirectedWalk(QuadraticAssignmentProblem qap, Permutation start, Permutation end) {
197      var N = qap.Weights.Rows;
198      var sol = start;
199      var fitness = QAPEvaluator.Apply(sol, qap.Weights, qap.Distances);
200      yield return Tuple.Create(sol, fitness);
201
202      var invSol = GetInverse(sol);
203      // we require at most N-1 steps to move from one permutation to another
204      for (var step = 0; step < N - 1; step++) {
205        var bestFitness = double.MaxValue;
206        var bestIndex = -1;
207        sol = (Permutation)sol.Clone();
208        for (var index = 0; index < N; index++) {
209          if (sol[index] == end[index]) continue;
210          var fit = QAPSwap2MoveEvaluator.Apply(sol, new Swap2Move(index, invSol[end[index]]), qap.Weights, qap.Distances);
211          if (fit < bestFitness) { // QAP is minimization
212            bestFitness = fit;
213            bestIndex = index;
214          }
215        }
216        if (bestIndex >= 0) {
217          var prev = sol[bestIndex];
218          Swap2Manipulator.Apply(sol, bestIndex, invSol[end[bestIndex]]);
219          fitness += bestFitness;
220          yield return Tuple.Create(sol, fitness);
221          invSol[prev] = invSol[end[bestIndex]];
222          invSol[sol[bestIndex]] = bestIndex;
223        } else break;
224      }
225    }
226
227    public static IEnumerable<Tuple<Permutation, double>> FirstDirectedWalk(IRandom random, QuadraticAssignmentProblem qap, Permutation start, Permutation end) {
228      var N = qap.Weights.Rows;
229      var sol = start;
230      var fitness = QAPEvaluator.Apply(sol, qap.Weights, qap.Distances);
231      yield return Tuple.Create(sol, fitness);
232
233      var invSol = GetInverse(sol);
234      // randomize the order in which improvements are tried
235      var order = Enumerable.Range(0, N).Shuffle(random).ToArray();
236      // we require at most N-1 steps to move from one permutation to another
237      for (var step = 0; step < N - 1; step++) {
238        var bestFitness = double.MaxValue;
239        var bestIndex = -1;
240        sol = (Permutation)sol.Clone();
241        for (var i = 0; i < N; i++) {
242          var index = order[i];
243          if (sol[index] == end[index]) continue;
244          var fit = QAPSwap2MoveEvaluator.Apply(sol, new Swap2Move(index, invSol[end[index]]), qap.Weights, qap.Distances);
245          if (fit < bestFitness) { // QAP is minimization
246            bestFitness = fit;
247            bestIndex = index;
248            if (bestFitness < 0) break;
249          }
250        }
251        if (bestIndex >= 0) {
252          var prev = sol[bestIndex];
253          Swap2Manipulator.Apply(sol, bestIndex, invSol[end[bestIndex]]);
254          fitness += bestFitness;
255          yield return Tuple.Create(sol, fitness);
256          invSol[prev] = invSol[end[bestIndex]];
257          invSol[sol[bestIndex]] = bestIndex;
258        } else break;
259      }
260    }
261
262    private static double Area(IEnumerable<Tuple<Permutation, double>> path) {
263      var iter = path.GetEnumerator();
264      if (!iter.MoveNext()) return 0.0;
265      var area = 0.0;
266      var prev = iter.Current;
267      while (iter.MoveNext()) {
268        area += TrapezoidArea(prev, iter.Current);
269        prev = iter.Current;
270      }
271      return area;
272    }
273
274    private static double TrapezoidArea(Tuple<Permutation, double> a, Tuple<Permutation, double> b) {
275      var area = 0.0;
276      var dist = Dist(a.Item1, b.Item1);
277      if ((a.Item2 <= 0 && b.Item2 <= 0) || (a.Item2 >= 0 && b.Item2 >= 0))
278        area += dist * (Math.Abs(a.Item2) + Math.Abs(b.Item2)) / 2.0;
279      else {
280        var k = (b.Item2 - a.Item2) / dist;
281        var d = a.Item2;
282        var x = -d / k;
283        area += Math.Abs(x * a.Item2 / 2.0);
284        area += Math.Abs((dist - x) * b.Item2 / 2.0);
285      }
286      return area;
287    }
288
289    // Center-of-Mass
290    private static double ComBelowZero(IEnumerable<Tuple<Permutation, double>> path) {
291      var area = 0.0;
292      var com = 0.0;
293      var nwalkDist = 0.0;
294      Tuple<Permutation, double> prev = null;
295      var iter = path.GetEnumerator();
296      while (iter.MoveNext()) {
297        var c = iter.Current;
298        if (prev != null) {
299          var ndist = Dist(prev.Item1, c.Item1) / (double)c.Item1.Length;
300          nwalkDist += ndist;
301          if (prev.Item2 < 0 || c.Item2 < 0) {
302            var a = TrapezoidArea(prev, c) / (double)c.Item1.Length;
303            area += a;
304            com += (nwalkDist - (ndist / 2.0)) * a;
305          }
306        }
307        prev = c;
308      }
309      return com / area;
310    }
311
312    private static IEnumerable<Tuple<Permutation, double>> ApproximateDerivative(IEnumerable<Tuple<Permutation, double>> data) {
313      Tuple<Permutation, double> prev = null, prev2 = null;
314      foreach (var d in data) {
315        if (prev == null) {
316          prev = d;
317          continue;
318        }
319        if (prev2 == null) {
320          prev2 = prev;
321          prev = d;
322          continue;
323        }
324        var dist = Dist(prev2.Item1, d.Item1);
325        yield return Tuple.Create(prev.Item1, (d.Item2 - prev2.Item2) / (double)dist);
326        prev2 = prev;
327        prev = d;
328      }
329    }
330
331    private static double Dist(Permutation a, Permutation b) {
332      var dist = 0;
333      for (var i = 0; i < a.Length; i++)
334        if (a[i] != b[i]) dist++;
335      return dist;
336    }
337
338    private static int[] GetInverse(Permutation p) {
339      var inv = new int[p.Length];
340      for (var i = 0; i < p.Length; i++) {
341        inv[p[i]] = i;
342      }
343      return inv;
344    }
345
346    // permutation must be strictly different in every position
347    private static void BiasedShuffle(Permutation p, IRandom random) {
348      for (var i = p.Length - 1; i > 0; i--) {
349        // Swap element "i" with a random earlier element (excluding itself)
350        var swapIndex = random.Next(i);
351        var h = p[swapIndex];
352        p[swapIndex] = p[i];
353        p[i] = h;
354      }
355    }
356  }
357}
Note: See TracBrowser for help on using the repository browser.