1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022016 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 


35  namespace HeuristicLab.Problems.CharacteristicAnalysis.QAP {


36  [Item("Directed Walk (QAPspecific)", "")]


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  var pathCount = Paths;


88  var random = Seed.HasValue ? new MersenneTwister((uint)Seed.Value) : new MersenneTwister();


89  var bestImprovement = BestImprovement;


90  var qap = (QuadraticAssignmentProblem)Problem;


91  var N = qap.Weights.Rows;


92  var start = new Permutation(PermutationTypes.Absolute, N, random);


93  var end = start;


94  List<double> ips = new List<double>(), ups = new List<double>(), sd2 = new List<double>();


95  var pathsD1 = new List<List<Tuple<double, double>>>();


96  for (var i = 0; i < pathCount; i++) {


97  var trajD1 = new List<Tuple<double, double>>();


98  pathsD1.Add(trajD1);


99 


100  if ((i + 1) % N == 0) {


101  start = new Permutation(PermutationTypes.Absolute, N, random);


102  end = start;


103  }


104  end = (Permutation)end.Clone();


105  Rot1(end);


106 


107  var hist = new Tuple<Permutation, double>[5];


108  var walkDist = 0.0;


109  var prevVal = double.NaN;


110  var sumD2 = 0.0;


111  var inflectionPoints = 0;


112  var undulationPoints = 0;


113  var countPoints = 0;


114  var counter = 0;


115  var path = bestImprovement ? BestImprovementWalk(qap, start, QAPEvaluator.Apply(start, qap.Weights, qap.Distances), end)


116  : FirstImprovementWalk(qap, start, QAPEvaluator.Apply(start, qap.Weights, qap.Distances), end, random);


117  foreach (var next in path) {


118  if (hist[0] != null) {


119  var dist = Dist(next.Item1, hist[0].Item1);


120  walkDist += dist;


121  }


122  // from the past 5 values we can calculate the 2nd derivative


123  // first derivative in point 2 as differential between points 1 and 3


124  // first derivative in point 4 as differential between points 3 and 5


125  // second derivative in point 3 as differential between the first derivatives in points 2 and 4


126  hist[4] = hist[3];


127  hist[3] = hist[2];


128  hist[2] = hist[1];


129  hist[1] = hist[0];


130  hist[0] = next;


131  counter++;


132 


133  if (counter < 3) continue;


134  var grad1 = (hist[0].Item2  hist[2].Item2) / Dist(hist[0].Item1, hist[2].Item1);


135 


136  if (!double.IsNaN(grad1) && !double.IsInfinity(grad1))


137  trajD1.Add(Tuple.Create(walkDist, grad1));


138 


139  if (counter < 5) continue;


140  countPoints++;


141  var grad2 = (hist[2].Item2  hist[4].Item2) / Dist(hist[2].Item1, hist[4].Item1);


142  var dgrad = (grad1  grad2) / Dist(hist[1].Item1, hist[3].Item1);


143 


144  if (double.IsNaN(dgrad)  double.IsInfinity(dgrad)) continue;


145  if (!double.IsNaN(prevVal)) {


146  if (prevVal < 0 && dgrad > 0  prevVal > 0 && dgrad < 0)


147  inflectionPoints++;


148  else if (prevVal.IsAlmost(0) && dgrad.IsAlmost(0)


149   prevVal.IsAlmost(0) && !dgrad.IsAlmost(0)


150   !prevVal.IsAlmost(0) && dgrad.IsAlmost(0))


151  undulationPoints++;


152  }


153  sumD2 += Math.Abs(grad1  grad2);


154  prevVal = dgrad;


155  }


156  start = end;


157  ips.Add(inflectionPoints / (double)countPoints);


158  ups.Add(undulationPoints / (double)countPoints);


159  sd2.Add(sumD2 / walkDist);


160  } // end paths


161  var avgZero = pathsD1.Select(path => path.SkipWhile(v => v.Item2 < 0).First().Item1 / path.Last().Item1).Median();


162 


163  foreach (var chara in characteristics.CheckedItems.Select(x => x.Value.Value)) {


164  if (chara == "Swap2.Sharpness") yield return new Result("Swap2.Sharpness", new DoubleValue(sd2.Average()));


165  if (chara == "Swap2.Bumpiness") yield return new Result("Swap2.Bumpiness", new DoubleValue(ips.Average()));


166  if (chara == "Swap2.Flatness") yield return new Result("Swap2.Flatness", new DoubleValue(ups.Average()));


167  if (chara == "Swap2.Steadiness") yield return new Result("Swap2.Steadiness", new DoubleValue(avgZero));


168  }


169  }


170 


171  public IEnumerable<Tuple<Permutation, double>> BestImprovementWalk(QuadraticAssignmentProblem qap, Permutation start, double fitness, Permutation end) {


172  var N = qap.Weights.Rows;


173  var sol = start;


174  var invSol = GetInverse(sol);


175  // we require at most N1 steps to move from one permutation to another


176  for (var step = 0; step < N  1; step++) {


177  var bestFitness = double.MaxValue;


178  var bestIndex = 1;


179  sol = (Permutation)sol.Clone();


180  for (var index = 0; index < N; index++) {


181  if (sol[index] == end[index]) continue;


182  var fit = QAPSwap2MoveEvaluator.Apply(sol, new Swap2Move(index, invSol[end[index]]), qap.Weights, qap.Distances);


183  if (fit < bestFitness) { // QAP is minimization


184  bestFitness = fit;


185  bestIndex = index;


186  }


187  }


188  if (bestIndex >= 0) {


189  var prev = sol[bestIndex];


190  Swap2Manipulator.Apply(sol, bestIndex, invSol[end[bestIndex]]);


191  fitness += bestFitness;


192  yield return Tuple.Create(sol, fitness);


193  invSol[prev] = invSol[end[bestIndex]];


194  invSol[sol[bestIndex]] = bestIndex;


195  } else break;


196  }


197  }


198 


199  public IEnumerable<Tuple<Permutation, double>> FirstImprovementWalk(QuadraticAssignmentProblem qap, Permutation start, double fitness, Permutation end, IRandom random) {


200  var N = qap.Weights.Rows;


201  var sol = start;


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 N1 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 Dist(Permutation a, Permutation b) {


232  return a.Where((t, i) => t != b[i]).Count();


233  }


234 


235  private static int[] GetInverse(Permutation p) {


236  var inv = new int[p.Length];


237  for (var i = 0; i < p.Length; i++) inv[p[i]] = i;


238  return inv;


239  }


240 


241  private static void Rot1(Permutation p) {


242  var first = p[0];


243  for (var i = 0; i < p.Length  1; i++) p[i] = p[i + 1];


244  p[p.Length  1] = first;


245  }


246  }


247  }

