Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2457: as calculated

File size: 8.7 KB
RevLine 
[14691]1using System;
[16096]2using System.Collections.Generic;
[14691]3using System.Linq;
[14690]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; }
[16096]22    public QAPDirectedWalk.WalkType Type { get; set; }
[14690]23
24    public override string Name { get { return "Path-Relinking Explorer"; } }
25    public override int Effort { get { return Paths; } }
26
[16096]27    public ISet<string> Features { get; set; }
28
[16129]29    public bool BestImprovement { get; set; }
30
[14690]31    public PathRelinkingExplorer() {
32     
33    }
34
35    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
[16096]36      var calculator = new QAPDirectedWalk {
[14690]37        Problem = problem,
[16129]38        BestImprovement = BestImprovement,
[14690]39        Paths = Paths,
40        Seed = seed,
[16096]41        Type = Type
[14690]42      };
[16096]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        }
[15031]47      }
[16096]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");
[15031]52
53      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
[16096]54        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray()) { DescriptionEffort = evaluations };
[15031]55    }
56  }
57
[14690]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
[16096]64    public ISet<string> Features { get; set; }
65
[14690]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 } },
[14691]73        SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } },
[14690]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 };
[16096]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
[14690]87      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
[16096]88        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray());
[14690]89    }
90  }
[14691]91
[14776]92  public class AdaptiveWalkExplorer : InstanceExplorer {
93    public int Iterations { get; set; }
[15031]94    public int SampleSize { get; set; }
[14776]95
96    public override string Name { get { return "Adaptive-Walk Explorer"; } }
[16096]97    private int _effort;
98    public override int Effort { get { return _effort; } }
[14776]99
[16096]100    public ISet<string> Features { get; set; }
101
[14776]102    public AdaptiveWalkExplorer() {
[16096]103      _effort = 0;
[14776]104    }
105
106    public override InstanceDescriptor Explore(QuadraticAssignmentProblem problem, int? seed = null) {
107      var walk = new AdaptiveWalk() {
108        SeedParameter = { Value = { Value = seed ?? 0 } },
[15031]109        SetSeedRandomlyParameter = { Value = { Value = !seed.HasValue } },
[14776]110        MaximumIterationsParameter = { Value = { Value = Iterations } },
[15031]111        RepetitionsParameter = { Value = { Value = 1 } },
112        SampleSizeParameter = { Value = { Value = SampleSize } }
[14776]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 };
[16096]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");
[14776]123
124      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
[16096]125        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray());
[14776]126    }
127  }
128
[15031]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
[16096]136    public ISet<string> Features { get; set; }
137
[15031]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 };
[16096]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");
[15031]159
160      return new InstanceDescriptor(problem.Name, InstanceDescriptor.GetClass(problem.Name), problem.Weights.Rows,
[16096]161        result.Keys.ToArray(), result.Values.Select(x => ((DoubleValue)x).Value).ToArray());
[15031]162    }
163  }
[16096]164  /*
[14691]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) {
[14696]187        Console.WriteLine("{0} not all paths present!", problem.Name);
[14691]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    }
[16096]201  }*/
[14690]202}
Note: See TracBrowser for help on using the repository browser.