[13861] | 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Problems.QuadraticAssignment;
|
---|
| 30 | using HeuristicLab.Random;
|
---|
| 31 | using System;
|
---|
| 32 | using System.Collections.Generic;
|
---|
| 33 | using System.Linq;
|
---|
| 34 |
|
---|
[14678] | 35 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
[13861] | 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() {
|
---|
[14429] | 87 | IRandom random = Seed.HasValue ? new MersenneTwister((uint)Seed.Value) : new MersenneTwister();
|
---|
| 88 | var qap = (QuadraticAssignmentProblem)Problem;
|
---|
[13861] | 89 | var pathCount = Paths;
|
---|
| 90 |
|
---|
[14429] | 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 | }
|
---|
[13861] | 98 |
|
---|
[14429] | 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 | }
|
---|
[13861] | 111 |
|
---|
[14678] | 112 | public static IEnumerable<IResult> Calculate(List<List<Tuple<Permutation, double>>> trajectories) {
|
---|
| 113 | var firstDerivatives = trajectories.Select(path => ApproximateDerivative(path).ToList()).ToList();
|
---|
| 114 | var secondDerivatives = firstDerivatives.Select(d1 => ApproximateDerivative(d1).ToList()).ToList();
|
---|
| 115 |
|
---|
| 116 | var props = GetCharacteristics(trajectories, firstDerivatives, secondDerivatives).ToDictionary(x => x.Item1, x => x.Item2);
|
---|
| 117 | yield return new Result("Swap2.Sharpness", new DoubleValue(props["Sharpness"]));
|
---|
| 118 | yield return new Result("Swap2.Bumpiness", new DoubleValue(props["Bumpiness"]));
|
---|
| 119 | yield return new Result("Swap2.Flatness", new DoubleValue(props["Flatness"]));
|
---|
| 120 | yield return new Result("Swap2.Steadiness", new DoubleValue(props["Steadiness"]));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[14429] | 123 | public static IEnumerable<List<Tuple<Permutation, double>>> Run(IRandom random, QuadraticAssignmentProblem qap, IEnumerable<Permutation> permutations, bool bestImprovement = true) {
|
---|
| 124 | var iter = permutations.GetEnumerator();
|
---|
| 125 | if (!iter.MoveNext()) yield break;
|
---|
[13861] | 126 |
|
---|
[14678] | 127 | var min = qap.LowerBound.Value;
|
---|
| 128 | var max = qap.AverageQuality.Value;
|
---|
| 129 |
|
---|
[14429] | 130 | var start = iter.Current;
|
---|
| 131 | while (iter.MoveNext()) {
|
---|
| 132 | var end = iter.Current;
|
---|
[13861] | 133 |
|
---|
[14429] | 134 | var walk = (bestImprovement ? BestDirectedWalk(qap, start, end) : FirstDirectedWalk(random, qap, start, end)).ToList();
|
---|
[14678] | 135 | yield return walk.Select(x => Tuple.Create(x.Item1, (x.Item2 - min) / (max - min))).ToList();
|
---|
[14429] | 136 | start = end;
|
---|
| 137 | } // end paths
|
---|
| 138 | }
|
---|
[13861] | 139 |
|
---|
[14678] | 140 | 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) {
|
---|
[14429] | 141 | var sharpness = f2.Average(x => Area(x));
|
---|
| 142 | var bumpiness = 0.0;
|
---|
| 143 | var flatness = 0.0;
|
---|
| 144 | var downPointing = f1.Where(x => x.Min(y => y.Item2) < 0).ToList();
|
---|
| 145 |
|
---|
| 146 | var steadiness = 0.0;
|
---|
| 147 | foreach (var path in downPointing) {
|
---|
| 148 | steadiness += ComBelowZero(path);
|
---|
| 149 | }
|
---|
| 150 | if (downPointing.Count > 0) steadiness /= downPointing.Count;
|
---|
| 151 |
|
---|
| 152 | for (var p = 0; p < f2.Count; p++) {
|
---|
| 153 | var bump = 0;
|
---|
| 154 | var flat = 0;
|
---|
| 155 | for (var i = 0; i < f2[p].Count - 1; i++) {
|
---|
| 156 | if ((f2[p][i].Item2 > 0 && f2[p][i + 1].Item2 < 0) || (f2[p][i].Item2 < 0 && f2[p][i + 1].Item2 > 0)) {
|
---|
| 157 | bump++;
|
---|
| 158 | } else if (f2[p][i].Item2 == 0) {
|
---|
| 159 | flat++;
|
---|
[13861] | 160 | }
|
---|
| 161 | }
|
---|
[14429] | 162 | bumpiness += bump / (f2[p].Count - 1.0);
|
---|
| 163 | flatness += flat / (f2[p].Count - 1.0);
|
---|
[13861] | 164 | }
|
---|
[14429] | 165 | bumpiness /= f2.Count;
|
---|
| 166 | flatness /= f2.Count;
|
---|
| 167 | return new[] {
|
---|
| 168 | Tuple.Create("Sharpness", sharpness),
|
---|
| 169 | Tuple.Create("Bumpiness", bumpiness),
|
---|
| 170 | Tuple.Create("Flatness", flatness),
|
---|
| 171 | Tuple.Create("Steadiness", steadiness)
|
---|
| 172 | };
|
---|
[13861] | 173 | }
|
---|
| 174 |
|
---|
[14429] | 175 | public static IEnumerable<Tuple<Permutation, double>> BestDirectedWalk(QuadraticAssignmentProblem qap, Permutation start, Permutation end) {
|
---|
[13861] | 176 | var N = qap.Weights.Rows;
|
---|
| 177 | var sol = start;
|
---|
[14429] | 178 | var fitness = QAPEvaluator.Apply(sol, qap.Weights, qap.Distances);
|
---|
| 179 | yield return Tuple.Create(sol, fitness);
|
---|
| 180 |
|
---|
[13861] | 181 | var invSol = GetInverse(sol);
|
---|
| 182 | // we require at most N-1 steps to move from one permutation to another
|
---|
| 183 | for (var step = 0; step < N - 1; step++) {
|
---|
| 184 | var bestFitness = double.MaxValue;
|
---|
| 185 | var bestIndex = -1;
|
---|
| 186 | sol = (Permutation)sol.Clone();
|
---|
| 187 | for (var index = 0; index < N; index++) {
|
---|
| 188 | if (sol[index] == end[index]) continue;
|
---|
| 189 | var fit = QAPSwap2MoveEvaluator.Apply(sol, new Swap2Move(index, invSol[end[index]]), qap.Weights, qap.Distances);
|
---|
| 190 | if (fit < bestFitness) { // QAP is minimization
|
---|
| 191 | bestFitness = fit;
|
---|
| 192 | bestIndex = index;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | if (bestIndex >= 0) {
|
---|
| 196 | var prev = sol[bestIndex];
|
---|
| 197 | Swap2Manipulator.Apply(sol, bestIndex, invSol[end[bestIndex]]);
|
---|
| 198 | fitness += bestFitness;
|
---|
| 199 | yield return Tuple.Create(sol, fitness);
|
---|
| 200 | invSol[prev] = invSol[end[bestIndex]];
|
---|
| 201 | invSol[sol[bestIndex]] = bestIndex;
|
---|
| 202 | } else break;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[14429] | 206 | public static IEnumerable<Tuple<Permutation, double>> FirstDirectedWalk(IRandom random, QuadraticAssignmentProblem qap, Permutation start, Permutation end) {
|
---|
[13861] | 207 | var N = qap.Weights.Rows;
|
---|
| 208 | var sol = start;
|
---|
[14429] | 209 | var fitness = QAPEvaluator.Apply(sol, qap.Weights, qap.Distances);
|
---|
| 210 | yield return Tuple.Create(sol, fitness);
|
---|
| 211 |
|
---|
[13861] | 212 | var invSol = GetInverse(sol);
|
---|
| 213 | // randomize the order in which improvements are tried
|
---|
| 214 | var order = Enumerable.Range(0, N).Shuffle(random).ToArray();
|
---|
| 215 | // we require at most N-1 steps to move from one permutation to another
|
---|
| 216 | for (var step = 0; step < N - 1; step++) {
|
---|
| 217 | var bestFitness = double.MaxValue;
|
---|
| 218 | var bestIndex = -1;
|
---|
| 219 | sol = (Permutation)sol.Clone();
|
---|
| 220 | for (var i = 0; i < N; i++) {
|
---|
| 221 | var index = order[i];
|
---|
| 222 | if (sol[index] == end[index]) continue;
|
---|
| 223 | var fit = QAPSwap2MoveEvaluator.Apply(sol, new Swap2Move(index, invSol[end[index]]), qap.Weights, qap.Distances);
|
---|
| 224 | if (fit < bestFitness) { // QAP is minimization
|
---|
| 225 | bestFitness = fit;
|
---|
| 226 | bestIndex = index;
|
---|
| 227 | if (bestFitness < 0) break;
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 | if (bestIndex >= 0) {
|
---|
| 231 | var prev = sol[bestIndex];
|
---|
| 232 | Swap2Manipulator.Apply(sol, bestIndex, invSol[end[bestIndex]]);
|
---|
| 233 | fitness += bestFitness;
|
---|
| 234 | yield return Tuple.Create(sol, fitness);
|
---|
| 235 | invSol[prev] = invSol[end[bestIndex]];
|
---|
| 236 | invSol[sol[bestIndex]] = bestIndex;
|
---|
| 237 | } else break;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[14429] | 241 | private static double Area(IEnumerable<Tuple<Permutation, double>> path) {
|
---|
| 242 | var iter = path.GetEnumerator();
|
---|
| 243 | if (!iter.MoveNext()) return 0.0;
|
---|
| 244 | var area = 0.0;
|
---|
| 245 | var prev = iter.Current;
|
---|
| 246 | while (iter.MoveNext()) {
|
---|
| 247 | area += TrapezoidArea(prev, iter.Current);
|
---|
| 248 | prev = iter.Current;
|
---|
| 249 | }
|
---|
| 250 | return area;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | private static double TrapezoidArea(Tuple<Permutation, double> a, Tuple<Permutation, double> b) {
|
---|
| 254 | var area = 0.0;
|
---|
| 255 | var dist = Dist(a.Item1, b.Item1);
|
---|
| 256 | if ((a.Item2 <= 0 && b.Item2 <= 0) || (a.Item2 >= 0 && b.Item2 >= 0))
|
---|
| 257 | area += dist * (Math.Abs(a.Item2) + Math.Abs(b.Item2)) / 2.0;
|
---|
| 258 | else {
|
---|
| 259 | var k = (b.Item2 - a.Item2) / dist;
|
---|
| 260 | var d = a.Item2;
|
---|
| 261 | var x = -d / k;
|
---|
| 262 | area += Math.Abs(x * a.Item2 / 2.0);
|
---|
| 263 | area += Math.Abs((dist - x) * b.Item2 / 2.0);
|
---|
| 264 | }
|
---|
| 265 | return area;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | private static double ComBelowZero(IEnumerable<Tuple<Permutation, double>> path) {
|
---|
| 269 | var area = 0.0;
|
---|
| 270 | var com = 0.0;
|
---|
| 271 | var nwalkDist = 0.0;
|
---|
| 272 | Tuple<Permutation, double> prev = null;
|
---|
| 273 | var iter = path.GetEnumerator();
|
---|
| 274 | while (iter.MoveNext()) {
|
---|
| 275 | var c = iter.Current;
|
---|
| 276 | if (prev != null) {
|
---|
| 277 | var ndist = Dist(prev.Item1, c.Item1) / (double)c.Item1.Length;
|
---|
| 278 | nwalkDist += ndist;
|
---|
| 279 | if (prev.Item2 < 0 || c.Item2 < 0) {
|
---|
| 280 | var a = TrapezoidArea(prev, c) / (double)c.Item1.Length;
|
---|
| 281 | area += a;
|
---|
| 282 | com += (nwalkDist - (ndist / 2.0)) * a;
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | prev = c;
|
---|
| 286 | }
|
---|
| 287 | return com / area;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | private static IEnumerable<Tuple<Permutation, double>> ApproximateDerivative(IEnumerable<Tuple<Permutation, double>> data) {
|
---|
| 291 | Tuple<Permutation, double> prev = null, prev2 = null;
|
---|
| 292 | foreach (var d in data) {
|
---|
| 293 | if (prev == null) {
|
---|
| 294 | prev = d;
|
---|
| 295 | continue;
|
---|
| 296 | }
|
---|
| 297 | if (prev2 == null) {
|
---|
| 298 | prev2 = prev;
|
---|
| 299 | prev = d;
|
---|
| 300 | continue;
|
---|
| 301 | }
|
---|
| 302 | var dist = Dist(prev2.Item1, d.Item1);
|
---|
| 303 | yield return Tuple.Create(prev.Item1, (d.Item2 - prev2.Item2) / (double)dist);
|
---|
| 304 | prev2 = prev;
|
---|
| 305 | prev = d;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[13861] | 309 | private static double Dist(Permutation a, Permutation b) {
|
---|
[14429] | 310 | var dist = 0;
|
---|
| 311 | for (var i = 0; i < a.Length; i++)
|
---|
| 312 | if (a[i] != b[i]) dist++;
|
---|
| 313 | return dist;
|
---|
[13861] | 314 | }
|
---|
| 315 |
|
---|
| 316 | private static int[] GetInverse(Permutation p) {
|
---|
| 317 | var inv = new int[p.Length];
|
---|
[14429] | 318 | for (var i = 0; i < p.Length; i++) {
|
---|
| 319 | inv[p[i]] = i;
|
---|
| 320 | }
|
---|
[13861] | 321 | return inv;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[14429] | 324 | // permutation must be strictly different in every position
|
---|
| 325 | private static void BiasedShuffle(Permutation p, IRandom random) {
|
---|
| 326 | for (var i = p.Length - 1; i > 0; i--) {
|
---|
| 327 | // Swap element "i" with a random earlier element (excluding itself)
|
---|
| 328 | var swapIndex = random.Next(i);
|
---|
| 329 | var h = p[swapIndex];
|
---|
| 330 | p[swapIndex] = p[i];
|
---|
| 331 | p[i] = h;
|
---|
| 332 | }
|
---|
[13861] | 333 | }
|
---|
| 334 | }
|
---|
| 335 | }
|
---|