1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Threading;
|
---|
4 | using HeuristicLab.Algorithms.MemPR.Permutation;
|
---|
5 | using HeuristicLab.Analysis.FitnessLandscape;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
8 | using HeuristicLab.Problems.QuadraticAssignment;
|
---|
9 | using HeuristicLab.SequentialEngine;
|
---|
10 |
|
---|
11 | namespace ProblemInstanceIdentifier {
|
---|
12 | public abstract class InstanceExplorer {
|
---|
13 |
|
---|
14 | public abstract string Name { get; }
|
---|
15 | public abstract int Effort { get; }
|
---|
16 | protected InstanceExplorer() { }
|
---|
17 |
|
---|
18 | public abstract InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null);
|
---|
19 | }
|
---|
20 |
|
---|
21 | public class PathRelinkingExplorer : InstanceExplorer {
|
---|
22 | public int Paths { get; set; }
|
---|
23 | public bool LocalOptima { get; set; }
|
---|
24 |
|
---|
25 | public override string Name { get { return "Path-Relinking Explorer"; } }
|
---|
26 | public override int Effort { get { return Paths; } }
|
---|
27 |
|
---|
28 | public PathRelinkingExplorer() {
|
---|
29 |
|
---|
30 | }
|
---|
31 |
|
---|
32 | public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
|
---|
33 | var walk = new QAPDirectedWalk {
|
---|
34 | Problem = problem,
|
---|
35 | BestImprovement = true,
|
---|
36 | Paths = Paths,
|
---|
37 | Seed = seed,
|
---|
38 | LocalOptima = LocalOptima
|
---|
39 | };
|
---|
40 | var features = walk.Calculate().ToDictionary(x => x.Name, x => x.Value);
|
---|
41 |
|
---|
42 | return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
|
---|
43 | features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray());
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | public class RandomWalkExplorer : InstanceExplorer {
|
---|
49 | public int Iterations { get; set; }
|
---|
50 |
|
---|
51 | public override string Name { get { return "Random-Walk Explorer"; } }
|
---|
52 | public override int Effort { get { return Iterations; } }
|
---|
53 |
|
---|
54 | public RandomWalkExplorer() {
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
|
---|
59 | var walk = new RandomWalk() {
|
---|
60 | SeedParameter = { Value = { Value = seed ?? 0 } },
|
---|
61 | SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } },
|
---|
62 | MaximumIterationsParameter = { Value = { Value = Iterations } },
|
---|
63 | RepetitionsParameter = { Value = { Value = 1 } }
|
---|
64 | };
|
---|
65 | walk.Problem = problem;
|
---|
66 | walk.Engine = new SequentialEngine();
|
---|
67 | walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator);
|
---|
68 | var calculator = new RandomWalkCalculator(walk) { Problem = problem };
|
---|
69 | var features = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value);
|
---|
70 |
|
---|
71 | return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
|
---|
72 | features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray());
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public class AdaptiveWalkExplorer : InstanceExplorer {
|
---|
77 | public int Iterations { get; set; }
|
---|
78 |
|
---|
79 | public override string Name { get { return "Adaptive-Walk Explorer"; } }
|
---|
80 | public override int Effort { get { return Iterations; } }
|
---|
81 |
|
---|
82 | public AdaptiveWalkExplorer() {
|
---|
83 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
|
---|
87 | var walk = new AdaptiveWalk() {
|
---|
88 | SeedParameter = { Value = { Value = seed ?? 0 } },
|
---|
89 | SetSeedRandomlyParameter = { Value = { Value = seed.HasValue } },
|
---|
90 | MaximumIterationsParameter = { Value = { Value = Iterations } },
|
---|
91 | RepetitionsParameter = { Value = { Value = 1 } }
|
---|
92 | };
|
---|
93 | walk.Problem = problem;
|
---|
94 | walk.Engine = new SequentialEngine();
|
---|
95 | walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator);
|
---|
96 | var calculator = new AdaptiveWalkCalculator(walk) { Problem = problem };
|
---|
97 | var features = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value);
|
---|
98 |
|
---|
99 | return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
|
---|
100 | features.Keys.ToArray(), features.Values.Select(x => ((DoubleValue)x).Value).ToArray());
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public class MemPRExplorer : InstanceExplorer {
|
---|
105 | public int Seconds { get; set; }
|
---|
106 |
|
---|
107 | public bool IncludeLocalSearch { get; set; }
|
---|
108 |
|
---|
109 | public override string Name { get { return "MemPR Explorer"; } }
|
---|
110 | public override int Effort { get { return Seconds; } }
|
---|
111 |
|
---|
112 | public MemPRExplorer() {
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
|
---|
117 | var memPr = new PermutationMemPR();
|
---|
118 | memPr.Problem = problem;
|
---|
119 | memPr.Prepare(true);
|
---|
120 | memPr.MaximumExecutionTime = TimeSpan.FromSeconds(Seconds);
|
---|
121 | memPr.SetSeedRandomly = !seed.HasValue;
|
---|
122 | memPr.Seed = seed ?? 0;
|
---|
123 | memPr.StartSync();
|
---|
124 | if (memPr.Context.RelinkedPaths.IsEmpty
|
---|
125 | || IncludeLocalSearch && memPr.Context.LocalSearchPaths.IsEmpty) {
|
---|
126 | Console.WriteLine("{0} not all paths present!", problem.Name);
|
---|
127 | return null;
|
---|
128 | };
|
---|
129 |
|
---|
130 | var features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.RelinkedPaths.Paths.ToList());
|
---|
131 | var result = features.GetValues();
|
---|
132 | var resultNames = features.GetNames();
|
---|
133 | if (IncludeLocalSearch) {
|
---|
134 | features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.LocalSearchPaths.Paths.ToList());
|
---|
135 | result = result.Concat(features.GetValues()).ToArray();
|
---|
136 | resultNames = resultNames.Concat(features.GetNames()).ToArray();
|
---|
137 | }
|
---|
138 | return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, resultNames, result);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|