using System; using System.Linq; using System.Threading; using HeuristicLab.Algorithms.MemPR.Permutation; using HeuristicLab.Analysis.FitnessLandscape; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Problems.QuadraticAssignment; using HeuristicLab.SequentialEngine; namespace ProblemInstanceIdentifier { public abstract class InstanceExplorer { public abstract string Name { get; } public abstract int Effort { get; } protected InstanceExplorer() { } public abstract InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null); } public class PathRelinkingExplorer : InstanceExplorer { public int Paths { get; set; } public bool LocalOptima { get; set; } public override string Name { get { return "Path-Relinking Explorer"; } } public override int Effort { get { return Paths; } } public PathRelinkingExplorer() { } public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) { var walk = new QAPDirectedWalk { Problem = problem, BestImprovement = true, Paths = Paths, Seed = seed, LocalOptima = LocalOptima }; var features = walk.Calculate().ToDictionary(x => x.Name, x => x.Value); return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray()); } } public class RandomWalkExplorer : InstanceExplorer { public int Iterations { get; set; } public override string Name { get { return "Random-Walk Explorer"; } } public override int Effort { get { return Iterations; } } public RandomWalkExplorer() { } public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) { var walk = new RandomWalk() { SeedParameter = { Value = { Value = seed ?? 0 } }, SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } }, MaximumIterationsParameter = { Value = { Value = Iterations } }, RepetitionsParameter = { Value = { Value = 1 } } }; walk.Problem = problem; walk.Engine = new SequentialEngine(); walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator); var calculator = new RandomWalkCalculator(walk) { Problem = problem }; var features = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value); return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray()); } } public class AdaptiveWalkExplorer : InstanceExplorer { public int Iterations { get; set; } public override string Name { get { return "Adaptive-Walk Explorer"; } } public override int Effort { get { return Iterations; } } public AdaptiveWalkExplorer() { } public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) { var walk = new AdaptiveWalk() { SeedParameter = { Value = { Value = seed ?? 0 } }, SetSeedRandomlyParameter = { Value = { Value = seed.HasValue } }, MaximumIterationsParameter = { Value = { Value = Iterations } }, RepetitionsParameter = { Value = { Value = 1 } } }; walk.Problem = problem; walk.Engine = new SequentialEngine(); walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator); var calculator = new AdaptiveWalkCalculator(walk) { Problem = problem }; var features = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value); return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray()); } } public class MemPRExplorer : InstanceExplorer { public int Seconds { get; set; } public bool IncludeLocalSearch { get; set; } public override string Name { get { return "MemPR Explorer"; } } public override int Effort { get { return Seconds; } } public MemPRExplorer() { } public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) { var memPr = new PermutationMemPR(); memPr.Problem = problem; memPr.Prepare(true); memPr.MaximumExecutionTime = TimeSpan.FromSeconds(Seconds); memPr.SetSeedRandomly = !seed.HasValue; memPr.Seed = seed ?? 0; memPr.StartSync(); if (memPr.Context.RelinkedPaths.IsEmpty || IncludeLocalSearch && memPr.Context.LocalSearchPaths.IsEmpty) { Console.WriteLine("{0} not all paths present!", problem.Name); return null; }; var features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.RelinkedPaths.Paths.ToList()); var result = features.GetValues(); var resultNames = features.GetNames(); if (IncludeLocalSearch) { features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.LocalSearchPaths.Paths.ToList()); result = result.Concat(features.GetValues()).ToArray(); resultNames = resultNames.Concat(features.GetNames()).ToArray(); } return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, resultNames, result); } } }