1 | using System.Linq;
|
---|
2 | using System.Threading;
|
---|
3 | using HeuristicLab.Analysis.FitnessLandscape;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
6 | using HeuristicLab.Problems.QuadraticAssignment;
|
---|
7 | using HeuristicLab.SequentialEngine;
|
---|
8 |
|
---|
9 | namespace ProblemInstanceIdentifier {
|
---|
10 | public abstract class InstanceExplorer {
|
---|
11 |
|
---|
12 | public abstract string Name { get; }
|
---|
13 | public abstract int Effort { get; }
|
---|
14 | protected InstanceExplorer() { }
|
---|
15 |
|
---|
16 | public abstract InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public class PathRelinkingExplorer : InstanceExplorer {
|
---|
20 | public int Paths { get; set; }
|
---|
21 | public bool LocalOptima { get; set; }
|
---|
22 |
|
---|
23 | public override string Name { get { return "Path-Relinking Explorer"; } }
|
---|
24 | public override int Effort { get { return Paths; } }
|
---|
25 |
|
---|
26 | public PathRelinkingExplorer() {
|
---|
27 |
|
---|
28 | }
|
---|
29 |
|
---|
30 | public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
|
---|
31 | var walk = new QAPDirectedWalk {
|
---|
32 | Problem = problem,
|
---|
33 | BestImprovement = true,
|
---|
34 | Paths = Paths,
|
---|
35 | Seed = seed,
|
---|
36 | LocalOptima = LocalOptima
|
---|
37 | };
|
---|
38 | var features = walk.Calculate().ToDictionary(x => x.Name, x => x.Value);
|
---|
39 |
|
---|
40 | return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
|
---|
41 | features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray());
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | public class RandomWalkExplorer : InstanceExplorer {
|
---|
47 | public int Iterations { get; set; }
|
---|
48 |
|
---|
49 | public override string Name { get { return "Random-Walk Explorer"; } }
|
---|
50 | public override int Effort { get { return Iterations; } }
|
---|
51 |
|
---|
52 | public RandomWalkExplorer() {
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
|
---|
57 | var walk = new RandomWalk() {
|
---|
58 | SeedParameter = { Value = { Value = seed ?? 0 } },
|
---|
59 | SetSeedRandomlyParameter = { Value = { Value = seed.HasValue } },
|
---|
60 | MaximumIterationsParameter = { Value = { Value = Iterations } },
|
---|
61 | RepetitionsParameter = { Value = { Value = 1 } }
|
---|
62 | };
|
---|
63 | walk.Problem = problem;
|
---|
64 | walk.Engine = new SequentialEngine();
|
---|
65 | walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator);
|
---|
66 | var calculator = new RandomWalkCalculator(walk) { Problem = problem };
|
---|
67 | var features = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value);
|
---|
68 |
|
---|
69 | return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
|
---|
70 | features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray());
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|