#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.QuadraticAssignment;
using HeuristicLab.Random;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("Directed Walk (QAP-specific)", "")]
[StorableClass]
public class QAPDirectedWalk : CharacteristicCalculator {
public IFixedValueParameter PathsParameter {
get { return (IFixedValueParameter)Parameters["Paths"]; }
}
public IFixedValueParameter BestImprovementParameter {
get { return (IFixedValueParameter)Parameters["BestImprovement"]; }
}
public IValueParameter SeedParameter {
get { return (IValueParameter)Parameters["Seed"]; }
}
public IFixedValueParameter LocalOptimaParameter {
get { return (IFixedValueParameter)Parameters["LocalOptima"]; }
}
public int Paths {
get { return PathsParameter.Value.Value; }
set { PathsParameter.Value.Value = value; }
}
public bool BestImprovement {
get { return BestImprovementParameter.Value.Value; }
set { BestImprovementParameter.Value.Value = value; }
}
public int? Seed {
get { return SeedParameter.Value != null ? SeedParameter.Value.Value : (int?)null; }
set { SeedParameter.Value = value.HasValue ? new IntValue(value.Value) : null; }
}
public bool LocalOptima {
get { return LocalOptimaParameter.Value.Value; }
set { LocalOptimaParameter.Value.Value = value; }
}
[StorableConstructor]
private QAPDirectedWalk(bool deserializing) : base(deserializing) { }
private QAPDirectedWalk(QAPDirectedWalk original, Cloner cloner) : base(original, cloner) { }
public QAPDirectedWalk() {
characteristics.AddRange(new[] { "Swap2.Sharpness", "Swap2.Bumpiness", "Swap2.Flatness", "Swap2.Steadiness" }
.Select(x => new StringValue(x)).ToList());
Parameters.Add(new FixedValueParameter("Paths", "The number of paths to explore (a path is a set of solutions that connect two randomly chosen solutions).", new IntValue(50)));
Parameters.Add(new FixedValueParameter("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)));
Parameters.Add(new OptionalValueParameter("Seed", "The seed for the random number generator."));
Parameters.Add(new FixedValueParameter("LocalOptima", "Whether to perform walks between local optima.", new BoolValue(false)));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new QAPDirectedWalk(this, cloner);
}
public override bool CanCalculate() {
return Problem is QuadraticAssignmentProblem;
}
public override IEnumerable Calculate() {
IRandom random = Seed.HasValue ? new MersenneTwister((uint)Seed.Value) : new MersenneTwister();
var qap = (QuadraticAssignmentProblem)Problem;
var pathCount = Paths;
var perm = new Permutation(PermutationTypes.Absolute, qap.Weights.Rows, random);
if (LocalOptima) {
var fit = new DoubleValue(QAPEvaluator.Apply(perm, qap.Weights, qap.Distances));
QAPExhaustiveSwap2LocalImprovement.ImproveFast(perm, qap.Weights, qap.Distances, fit, new IntValue(0), new IntValue(0), qap.Maximization.Value, int.MaxValue, CancellationToken.None);
}
var permutations = new List { perm };
while (permutations.Count < pathCount + 1) {
perm = (Permutation)permutations.Last().Clone();
BiasedShuffle(perm, random);
if (LocalOptima) {
var fit = new DoubleValue(QAPEvaluator.Apply(perm, qap.Weights, qap.Distances));
QAPExhaustiveSwap2LocalImprovement.ImproveFast(perm, qap.Weights, qap.Distances, fit, new IntValue(0), new IntValue(0), qap.Maximization.Value, int.MaxValue, CancellationToken.None);
}
if (HammingSimilarityCalculator.CalculateSimilarity(permutations.Last(), perm) < 0.75)
permutations.Add(perm);
}
var trajectories = Run(random, (QuadraticAssignmentProblem)Problem, permutations, BestImprovement).ToList();
var firstDerivatives = trajectories.Select(path => ApproximateDerivative(path).ToList()).ToList();
var secondDerivatives = firstDerivatives.Select(d1 => ApproximateDerivative(d1).ToList()).ToList();
var props = GetCharacteristics(trajectories, firstDerivatives, secondDerivatives).ToDictionary(x => x.Item1, x => x.Item2);
foreach (var chara in characteristics.CheckedItems.Select(x => x.Value.Value)) {
if (chara == "Swap2.Sharpness") yield return new Result("Swap2.Sharpness", new DoubleValue(props["Sharpness"]));
if (chara == "Swap2.Bumpiness") yield return new Result("Swap2.Bumpiness", new DoubleValue(props["Bumpiness"]));
if (chara == "Swap2.Flatness") yield return new Result("Swap2.Flatness", new DoubleValue(props["Flatness"]));
if (chara == "Swap2.Steadiness") yield return new Result("Swap2.Steadiness", new DoubleValue(props["Steadiness"]));
}
}
public static IEnumerable Calculate(List>> trajectories) {
var firstDerivatives = trajectories.Select(path => ApproximateDerivative(path).ToList()).ToList();
var secondDerivatives = firstDerivatives.Select(d1 => ApproximateDerivative(d1).ToList()).ToList();
var props = GetCharacteristics(trajectories, firstDerivatives, secondDerivatives).ToDictionary(x => x.Item1, x => x.Item2);
yield return new Result("Swap2.Sharpness", new DoubleValue(props["Sharpness"]));
yield return new Result("Swap2.Bumpiness", new DoubleValue(props["Bumpiness"]));
yield return new Result("Swap2.Flatness", new DoubleValue(props["Flatness"]));
yield return new Result("Swap2.Steadiness", new DoubleValue(props["Steadiness"]));
}
public static IEnumerable>> Run(IRandom random, QuadraticAssignmentProblem qap, IEnumerable permutations, bool bestImprovement = true) {
var iter = permutations.GetEnumerator();
if (!iter.MoveNext()) yield break;
var min = qap.LowerBound.Value;
var max = qap.AverageQuality.Value;
var start = iter.Current;
while (iter.MoveNext()) {
var end = iter.Current;
var walk = (bestImprovement ? BestDirectedWalk(qap, start, end) : FirstDirectedWalk(random, qap, start, end)).ToList();
yield return walk.Select(x => Tuple.Create(x.Item1, (x.Item2 - min) / (max - min))).ToList();
start = end;
} // end paths
}
private static IEnumerable> GetCharacteristics(List>> f, List>> f1, List>> f2) {
var sharpness = f2.Average(x => Area(x));
var bumpiness = 0.0;
var flatness = 0.0;
var downPointing = f1.Where(x => x.Min(y => y.Item2) < 0).ToList();
var steadiness = 0.0;
foreach (var path in downPointing) {
steadiness += ComBelowZero(path);
}
if (downPointing.Count > 0) steadiness /= downPointing.Count;
for (var p = 0; p < f2.Count; p++) {
if (f2[p].Count <= 2) continue;
var bump = 0;
var flat = 0;
for (var i = 0; i < f2[p].Count - 1; i++) {
if ((f2[p][i].Item2 > 0 && f2[p][i + 1].Item2 < 0) || (f2[p][i].Item2 < 0 && f2[p][i + 1].Item2 > 0)) {
bump++;
} else if (f2[p][i].Item2 == 0) {
flat++;
}
}
bumpiness += bump / (f2[p].Count - 1.0);
flatness += flat / (f2[p].Count - 1.0);
}
bumpiness /= f2.Count;
flatness /= f2.Count;
return new[] {
Tuple.Create("Sharpness", sharpness),
Tuple.Create("Bumpiness", bumpiness),
Tuple.Create("Flatness", flatness),
Tuple.Create("Steadiness", steadiness)
};
}
public static IEnumerable> BestDirectedWalk(QuadraticAssignmentProblem qap, Permutation start, Permutation end) {
var N = qap.Weights.Rows;
var sol = start;
var fitness = QAPEvaluator.Apply(sol, qap.Weights, qap.Distances);
yield return Tuple.Create(sol, fitness);
var invSol = GetInverse(sol);
// we require at most N-1 steps to move from one permutation to another
for (var step = 0; step < N - 1; step++) {
var bestFitness = double.MaxValue;
var bestIndex = -1;
sol = (Permutation)sol.Clone();
for (var index = 0; index < N; index++) {
if (sol[index] == end[index]) continue;
var fit = QAPSwap2MoveEvaluator.Apply(sol, new Swap2Move(index, invSol[end[index]]), qap.Weights, qap.Distances);
if (fit < bestFitness) { // QAP is minimization
bestFitness = fit;
bestIndex = index;
}
}
if (bestIndex >= 0) {
var prev = sol[bestIndex];
Swap2Manipulator.Apply(sol, bestIndex, invSol[end[bestIndex]]);
fitness += bestFitness;
yield return Tuple.Create(sol, fitness);
invSol[prev] = invSol[end[bestIndex]];
invSol[sol[bestIndex]] = bestIndex;
} else break;
}
}
public static IEnumerable> FirstDirectedWalk(IRandom random, QuadraticAssignmentProblem qap, Permutation start, Permutation end) {
var N = qap.Weights.Rows;
var sol = start;
var fitness = QAPEvaluator.Apply(sol, qap.Weights, qap.Distances);
yield return Tuple.Create(sol, fitness);
var invSol = GetInverse(sol);
// randomize the order in which improvements are tried
var order = Enumerable.Range(0, N).Shuffle(random).ToArray();
// we require at most N-1 steps to move from one permutation to another
for (var step = 0; step < N - 1; step++) {
var bestFitness = double.MaxValue;
var bestIndex = -1;
sol = (Permutation)sol.Clone();
for (var i = 0; i < N; i++) {
var index = order[i];
if (sol[index] == end[index]) continue;
var fit = QAPSwap2MoveEvaluator.Apply(sol, new Swap2Move(index, invSol[end[index]]), qap.Weights, qap.Distances);
if (fit < bestFitness) { // QAP is minimization
bestFitness = fit;
bestIndex = index;
if (bestFitness < 0) break;
}
}
if (bestIndex >= 0) {
var prev = sol[bestIndex];
Swap2Manipulator.Apply(sol, bestIndex, invSol[end[bestIndex]]);
fitness += bestFitness;
yield return Tuple.Create(sol, fitness);
invSol[prev] = invSol[end[bestIndex]];
invSol[sol[bestIndex]] = bestIndex;
} else break;
}
}
private static double Area(IEnumerable> path) {
var iter = path.GetEnumerator();
if (!iter.MoveNext()) return 0.0;
var area = 0.0;
var prev = iter.Current;
while (iter.MoveNext()) {
area += TrapezoidArea(prev, iter.Current);
prev = iter.Current;
}
return area;
}
private static double TrapezoidArea(Tuple a, Tuple b) {
var area = 0.0;
var dist = Dist(a.Item1, b.Item1);
if ((a.Item2 <= 0 && b.Item2 <= 0) || (a.Item2 >= 0 && b.Item2 >= 0))
area += dist * (Math.Abs(a.Item2) + Math.Abs(b.Item2)) / 2.0;
else {
var k = (b.Item2 - a.Item2) / dist;
var d = a.Item2;
var x = -d / k;
area += Math.Abs(x * a.Item2 / 2.0);
area += Math.Abs((dist - x) * b.Item2 / 2.0);
}
return area;
}
// Center-of-Mass
private static double ComBelowZero(IEnumerable> path) {
var area = 0.0;
var com = 0.0;
var nwalkDist = 0.0;
Tuple prev = null;
var iter = path.GetEnumerator();
while (iter.MoveNext()) {
var c = iter.Current;
if (prev != null) {
var ndist = Dist(prev.Item1, c.Item1) / (double)c.Item1.Length;
nwalkDist += ndist;
if (prev.Item2 < 0 || c.Item2 < 0) {
var a = TrapezoidArea(prev, c) / (double)c.Item1.Length;
area += a;
com += (nwalkDist - (ndist / 2.0)) * a;
}
}
prev = c;
}
return com / area;
}
private static IEnumerable> ApproximateDerivative(IEnumerable> data) {
Tuple prev = null, prev2 = null;
foreach (var d in data) {
if (prev == null) {
prev = d;
continue;
}
if (prev2 == null) {
prev2 = prev;
prev = d;
continue;
}
var dist = Dist(prev2.Item1, d.Item1);
yield return Tuple.Create(prev.Item1, (d.Item2 - prev2.Item2) / (double)dist);
prev2 = prev;
prev = d;
}
}
private static double Dist(Permutation a, Permutation b) {
var dist = 0;
for (var i = 0; i < a.Length; i++)
if (a[i] != b[i]) dist++;
return dist;
}
private static int[] GetInverse(Permutation p) {
var inv = new int[p.Length];
for (var i = 0; i < p.Length; i++) {
inv[p[i]] = i;
}
return inv;
}
// permutation must be strictly different in every position
private static void BiasedShuffle(Permutation p, IRandom random) {
for (var i = p.Length - 1; i > 0; i--) {
// Swap element "i" with a random earlier element (excluding itself)
var swapIndex = random.Next(i);
var h = p[swapIndex];
p[swapIndex] = p[i];
p[i] = h;
}
}
}
}