Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/ProblemInstanceIdentifier/InstanceExplorer.cs @ 14691

Last change on this file since 14691 was 14691, checked in by abeham, 7 years ago

#2457: working on identification of problem instances

File size: 4.3 KB
Line 
1using System;
2using System.Linq;
3using System.Threading;
4using HeuristicLab.Algorithms.MemPR.Permutation;
5using HeuristicLab.Analysis.FitnessLandscape;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.PermutationEncoding;
8using HeuristicLab.Problems.QuadraticAssignment;
9using HeuristicLab.SequentialEngine;
10
11namespace 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 MemPRExplorer : InstanceExplorer {
77    public int Seconds { get; set; }
78
79    public bool IncludeLocalSearch { get; set; }
80
81    public override string Name { get { return "MemPR Explorer"; } }
82    public override int Effort { get { return Seconds; } }
83
84    public MemPRExplorer() {
85
86    }
87
88    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
89      var memPr = new PermutationMemPR();
90      memPr.Problem = problem;
91      memPr.Prepare(true);
92      memPr.MaximumExecutionTime = TimeSpan.FromSeconds(Seconds);
93      memPr.SetSeedRandomly = !seed.HasValue;
94      memPr.Seed = seed ?? 0;
95      memPr.StartSync();
96      if (memPr.Context.RelinkedPaths.IsEmpty
97        || IncludeLocalSearch && memPr.Context.LocalSearchPaths.IsEmpty) {
98        Console.Write("{0} not all paths present!", problem.Name);
99        return null;
100      };
101
102      var features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.RelinkedPaths.Paths.ToList());
103      var result = features.GetValues();
104      var resultNames = features.GetNames();
105      if (IncludeLocalSearch) {
106        features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.LocalSearchPaths.Paths.ToList());
107        result = result.Concat(features.GetValues()).ToArray();
108        resultNames = resultNames.Concat(features.GetNames()).ToArray();
109      }
110      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, resultNames, result);
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.