Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/ProblemInstanceIdentifier/InstanceExplorer.cs @ 16752

Last change on this file since 16752 was 16129, checked in by abeham, 6 years ago

#2457: as calculated

File size: 8.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Analysis.FitnessLandscape;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.PermutationEncoding;
7using HeuristicLab.Problems.QuadraticAssignment;
8using HeuristicLab.SequentialEngine;
9
10namespace ProblemInstanceIdentifier {
11  public abstract class InstanceExplorer {
12
13    public abstract string Name { get; }
14    public abstract int Effort { get; }
15    protected InstanceExplorer() { }
16
17    public abstract InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null);
18  }
19
20  public class PathRelinkingExplorer : InstanceExplorer {
21    public int Paths { get; set; }
22    public QAPDirectedWalk.WalkType Type { get; set; }
23
24    public override string Name { get { return "Path-Relinking Explorer"; } }
25    public override int Effort { get { return Paths; } }
26
27    public ISet<string> Features { get; set; }
28
29    public bool BestImprovement { get; set; }
30
31    public PathRelinkingExplorer() {
32     
33    }
34
35    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
36      var calculator = new QAPDirectedWalk {
37        Problem = problem,
38        BestImprovement = BestImprovement,
39        Paths = Paths,
40        Seed = seed,
41        Type = Type
42      };
43      if (Features != null && Features.Count > 0) {
44        foreach (var c in calculator.Characteristics.ToList()) {
45          calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value));
46        }
47      }
48      var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value);
49      var evaluations = ((IntValue)result["EvaluatedSolutions"]).Value;
50      result.Remove("EvaluatedSolutions");
51      if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results");
52
53      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
54        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray()) { DescriptionEffort = evaluations };
55    }
56  }
57
58  public class RandomWalkExplorer : InstanceExplorer {
59    public int Iterations { get; set; }
60
61    public override string Name { get { return "Random-Walk Explorer"; } }
62    public override int Effort { get { return Iterations; } }
63
64    public ISet<string> Features { get; set; }
65
66    public RandomWalkExplorer() {
67
68    }
69
70    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
71      var walk = new RandomWalk() {
72        SeedParameter = { Value = { Value = seed ?? 0 } },
73        SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } },
74        MaximumIterationsParameter = { Value = { Value = Iterations } },
75        RepetitionsParameter = { Value = { Value = 1 } }
76      };
77      walk.Problem = problem;
78      walk.Engine = new SequentialEngine();
79      walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator);
80      var calculator = new RandomWalkCalculator(walk) { Problem = problem };
81      foreach (var c in calculator.Characteristics.ToList()) {
82        calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value));
83      }
84      var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value);
85      if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results");
86
87      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
88        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray());
89    }
90  }
91
92  public class AdaptiveWalkExplorer : InstanceExplorer {
93    public int Iterations { get; set; }
94    public int SampleSize { get; set; }
95
96    public override string Name { get { return "Adaptive-Walk Explorer"; } }
97    private int _effort;
98    public override int Effort { get { return _effort; } }
99
100    public ISet<string> Features { get; set; }
101
102    public AdaptiveWalkExplorer() {
103      _effort = 0;
104    }
105
106    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
107      var walk = new AdaptiveWalk() {
108        SeedParameter = { Value = { Value = seed ?? 0 } },
109        SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } },
110        MaximumIterationsParameter = { Value = { Value = Iterations } },
111        RepetitionsParameter = { Value = { Value = 1 } },
112        SampleSizeParameter = { Value = { Value = SampleSize } }
113      };
114      walk.Problem = problem;
115      walk.Engine = new SequentialEngine();
116      walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator);
117      var calculator = new AdaptiveWalkCalculator(walk) { Problem = problem };
118      foreach (var c in calculator.Characteristics.ToList()) {
119        calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value));
120      }
121      var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value);
122      if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results");
123
124      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
125        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray());
126    }
127  }
128
129  public class UpDownWalkExplorer : InstanceExplorer {
130    public int Iterations { get; set; }
131    public int SampleSize { get; set; }
132
133    public override string Name { get { return "Up/Down-Walk Explorer"; } }
134    public override int Effort { get { return Iterations * SampleSize; } }
135
136    public ISet<string> Features { get; set; }
137
138    public UpDownWalkExplorer() {
139
140    }
141
142    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
143      var walk = new UpDownWalk() {
144        SeedParameter = { Value = { Value = seed ?? 0 } },
145        SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } },
146        MaximumIterationsParameter = { Value = { Value = Iterations } },
147        RepetitionsParameter = { Value = { Value = 1 } },
148        SampleSizeParameter = { Value = { Value = SampleSize } }
149      };
150      walk.Problem = problem;
151      walk.Engine = new SequentialEngine();
152      walk.MutatorParameter.Value = walk.MutatorParameter.ValidValues.First(x => x is Swap2Manipulator);
153      var calculator = new UpDownWalkCalculator(walk) {  Problem = problem };
154      foreach (var c in calculator.Characteristics.ToList()) {
155        calculator.Characteristics.SetItemCheckedState(c, Features.Contains(c.Value));
156      }
157      var result = calculator.Calculate().ToDictionary(x => x.Name, x => x.Value);
158      if (Features != null && result.Count != Features.Count) throw new InvalidOperationException("Not all features in results");
159
160      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
161        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray());
162    }
163  }
164  /*
165  public class MemPRExplorer : InstanceExplorer {
166    public int Seconds { get; set; }
167
168    public bool IncludeLocalSearch { get; set; }
169
170    public override string Name { get { return "MemPR Explorer"; } }
171    public override int Effort { get { return Seconds; } }
172
173    public MemPRExplorer() {
174
175    }
176
177    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
178      var memPr = new PermutationMemPR();
179      memPr.Problem = problem;
180      memPr.Prepare(true);
181      memPr.MaximumExecutionTime = TimeSpan.FromSeconds(Seconds);
182      memPr.SetSeedRandomly = !seed.HasValue;
183      memPr.Seed = seed ?? 0;
184      memPr.StartSync();
185      if (memPr.Context.RelinkedPaths.IsEmpty
186        || IncludeLocalSearch && memPr.Context.LocalSearchPaths.IsEmpty) {
187        Console.WriteLine("{0} not all paths present!", problem.Name);
188        return null;
189      };
190
191      var features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.RelinkedPaths.Paths.ToList());
192      var result = features.GetValues();
193      var resultNames = features.GetNames();
194      if (IncludeLocalSearch) {
195        features = PermutationPathAnalysis.GetCharacteristics(memPr.Context.LocalSearchPaths.Paths.ToList());
196        result = result.Concat(features.GetValues()).ToArray();
197        resultNames = resultNames.Concat(features.GetNames()).ToArray();
198      }
199      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows, resultNames, result);
200    }
201  }*/
202}
Note: See TracBrowser for help on using the repository browser.