source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/ProblemCharacteristicAnalysis/QAP/QAPDirectedWalk.cs @ 14429

Last change on this file since 14429 was 14429, checked in by abeham, 4 years ago

#2701, #2708: Made a new branch from ProblemRefactoring and removed ScopedBasicAlgorithm branch (which becomes MemPR branch)

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