using System.Linq; using System.Threading; 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()); } } }