#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" } .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; List permutations = CalculateRelinkingPoints(random, qap, Paths, LocalOptima); var trajectories = Run(random, (QuadraticAssignmentProblem)Problem, permutations, BestImprovement).ToList(); var result = PermutationPathAnalysis.GetCharacteristics(trajectories); foreach (var chara in characteristics.CheckedItems.Select(x => x.Value.Value)) { if (chara == "Swap2.Sharpness") yield return new Result("Swap2.Sharpness", new DoubleValue(result.Sharpness)); if (chara == "Swap2.Bumpiness") yield return new Result("Swap2.Bumpiness", new DoubleValue(result.Bumpiness)); if (chara == "Swap2.Flatness") yield return new Result("Swap2.Flatness", new DoubleValue(result.Flatness)); } } public static List CalculateRelinkingPoints(IRandom random, QuadraticAssignmentProblem qap, int pathCount, bool localOptima) { 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); } return permutations; } 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> 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; } } private 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 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; } } } }