using System; using System.Collections.Generic; using System.Linq; 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 QAPDirectedWalk.WalkType Type { get; set; } public override string Name { get { return "Path-Relinking Explorer"; } } public override int Effort { get { return Paths; } } public ISet Features { get; set; } public bool BestImprovement { get; set; } public PathRelinkingExplorer() { } public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) { var calculator = new QAPDirectedWalk { Problem = problem, BestImprovement = BestImprovement, Paths = Paths, Seed = seed, Type = Type }; if (Features != null && Features.Count > 0) { foreach (var c in calculator.Characteristics.ToList()) { calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value)); } } var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value); var evaluations = ((IntValue)result["EvaluatedSolutions"]).Value; result.Remove("EvaluatedSolutions"); if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results"); return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray()) { DescriptionEffort = evaluations }; } } 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 ISet Features { get; set; } 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 }; foreach (var c in calculator.Characteristics.ToList()) { calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value)); } var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value); if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results"); return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray()); } } public class AdaptiveWalkExplorer : InstanceExplorer { public int Iterations { get; set; } public int SampleSize { get; set; } public override string Name { get { return "Adaptive-Walk Explorer"; } } private int _effort; public override int Effort { get { return _effort; } } public ISet Features { get; set; } public AdaptiveWalkExplorer() { _effort = 0; } 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 } }, SampleSizeParameter = { Value = { Value = SampleSize } } }; 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 }; foreach (var c in calculator.Characteristics.ToList()) { calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value)); } var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value); if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results"); return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray()); } } public class UpDownWalkExplorer : InstanceExplorer { public int Iterations { get; set; } public int SampleSize { get; set; } public override string Name { get { return "Up/Down-Walk Explorer"; } } public override int Effort { get { return Iterations * SampleSize; } } public ISet Features { get; set; } public UpDownWalkExplorer() { } public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) { var walk = new UpDownWalk() { SeedParameter = { Value = { Value = seed ?? 0 } }, SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } }, MaximumIterationsParameter = { Value = { Value = Iterations } }, RepetitionsParameter = { Value = { Value = 1 } }, SampleSizeParameter = { Value = { Value = SampleSize } } }; walk.Problem = problem; walk.Engine = new SequentialEngine(); walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator); var calculator = new UpDownWalkCalculator(walk) { Problem = problem }; foreach (var c in calculator.Characteristics.ToList()) { calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value)); } var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value); if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results"); return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, result.Keys.ToArray(), result.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); } }*/ }