Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/Test/RunBaselineExperiments.cs @ 12724

Last change on this file since 12724 was 11981, checked in by gkronber, 10 years ago

#2283: cleanup and included HeuristicLab.dlls to create a self-contained branch

File size: 12.5 KB
RevLine 
[11981]1using System;
2using System.Collections;
3using System.Collections.Generic;
4using HeuristicLab.Algorithms.Bandits.BanditPolicies;
5using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
6using HeuristicLab.Algorithms.GeneticProgramming;
7using HeuristicLab.Algorithms.GrammaticalOptimization;
8using HeuristicLab.Problems.GrammaticalOptimization;
9using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
12
13namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
14
15
16  [TestClass]
17  public class RunBaselineExperiments {
18    private readonly static int randSeed = 31415;
19
20    internal class Configuration {
21      public ISymbolicExpressionTreeProblem Problem;
22      public int MaxSize;
23      public int RandSeed;
24
25      public override string ToString() {
26        return string.Format("{0} {1} {2}", RandSeed, Problem, MaxSize);
27      }
28    }
29
30    #region permutation
31    [TestMethod]
32    public void RunPureRandomSearchPermutationProblem() {
33      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
34      {
35        (randSeed) => (ISymbolicExpressionTreeProblem)new PermutationProblem(),
36      };
37
38      var maxSizes = new int[] { 32 };
39      int nReps = 20;
40      int maxIterations = 50000;
41      foreach (var instanceFactory in instanceFactories) {
42        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
43          RunPureRandomSearchForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
44        }
45      }
46    }
47    [TestMethod]
48    public void RunRandomSearchNoResamplingPermutationProblem() {
49      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
50      {
51        (randSeed) => (ISymbolicExpressionTreeProblem)new PermutationProblem(),
52      };
53
54      var maxSizes = new int[] { 32 };
55      int nReps = 20;
56      int maxIterations = 50000;
57      foreach (var instanceFactory in instanceFactories) {
58        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
59          RunRandomSearchNoResamplingForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
60        }
61      }
62    }
63    #endregion
64
65    #region royalpair
66    [TestMethod]
67    public void RunPureRandomSearchRoyalPairProblem() {
68      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
69      {
70        (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalPairProblem(),
71      };
72
73      var maxSizes = new int[] { 32, 64, 128, 256 };
74      int nReps = 20;
75      int maxIterations = 50000;
76      foreach (var instanceFactory in instanceFactories) {
77        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
78          RunPureRandomSearchForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
79        }
80      }
81    }
82    [TestMethod]
83    public void RunRandomSearchNoResamplingRoyalPairProblem() {
84      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
85      {
86        (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalPairProblem(),
87      };
88
89      var maxSizes = new int[] { 32, 64, 128, 256 };
90      int nReps = 20;
91      int maxIterations = 50000;
92      foreach (var instanceFactory in instanceFactories) {
93        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
94          RunRandomSearchNoResamplingForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
95        }
96      }
97    }
98    #endregion
99
100    #region royalsymbol
101    [TestMethod]
102    public void RunPureRandomSearchRoyalSymbolProblem() {
103      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
104      {
105        (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalSymbolProblem(),
106      };
107
108      var maxSizes = new int[] { 32, 64, 128, 256 };
109      int nReps = 20;
110      int maxIterations = 50000;
111      foreach (var instanceFactory in instanceFactories) {
112        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
113          RunPureRandomSearchForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
114        }
115      }
116    }
117    [TestMethod]
118    public void RunRandomSearchNoResamplingRoyalSymbolProblem() {
119      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
120      {
121        (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalSymbolProblem(),
122      };
123
124      var maxSizes = new int[] { 32, 64, 128, 256 };
125      int nReps = 20;
126      int maxIterations = 50000;
127      foreach (var instanceFactory in instanceFactories) {
128        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
129          RunRandomSearchNoResamplingForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
130        }
131      }
132    }
133    #endregion
134
135    #region findphrases
136    [TestMethod]
137    public void RunPureRandomSearchFindPhrasesProblem() {
138      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
139      {
140        (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, true),
141        (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, false),
142        (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 50, 1, 0.8, false),
143      };
144
145      var maxSizes = new int[] { 15 * 3 }; // * 3 for non-terminals
146      int nReps = 20;
147      int maxIterations = 50000;
148      foreach (var instanceFactory in instanceFactories) {
149        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
150          RunPureRandomSearchForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
151        }
152      }
153    }
154    [TestMethod]
155    public void RunRandomSearchNoResamplingFindPhrasesProblem() {
156      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
157      {
158        (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, true),
159        (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, false),
160        (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 50, 1, 0.8, false),
161      };
162
163      var maxSizes = new int[] { 15 * 3 }; // * 3 for non-terminals
164      int nReps = 20;
165      int maxIterations = 50000;
166      foreach (var instanceFactory in instanceFactories) {
167        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
168          RunRandomSearchNoResamplingForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
169        }
170      }
171    }
172
173    #endregion
174
175    #region artificial ant
176    [TestMethod]
177    public void RunPureRandomSearchArtificialAntProblem() {
178      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
179      {
180        (randSeed) => (ISymbolicExpressionTreeProblem) new SantaFeAntProblem(),
181      };
182
183      var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 17
184      int nReps = 20;
185      int maxIterations = 100000; // randomsearch finds the optimum almost always for 100000 evals
186      foreach (var instanceFactory in instanceFactories) {
187        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
188          RunPureRandomSearchForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
189        }
190      }
191    }
192    [TestMethod]
193    public void RunRandomSearchNoResamplingArtificialAntProblem() {
194      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
195      {
196        (randSeed) => (ISymbolicExpressionTreeProblem) new SantaFeAntProblem(),
197      };
198
199      var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 17
200      int nReps = 20;
201      int maxIterations = 100000; // randomsearch finds the optimum almost always for 100000 evals
202      foreach (var instanceFactory in instanceFactories) {
203        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
204          RunRandomSearchNoResamplingForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
205        }
206      }
207    }
208    #endregion
209
210    #region symb-reg-poly-10
211    [TestMethod]
212    public void RunPureRandomSearchPoly10Problem() {
213      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
214      {
215        (randSeed) => (ISymbolicExpressionTreeProblem) new SymbolicRegressionPoly10Problem(),
216      };
217
218      var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 23
219      int nReps = 20;
220      int maxIterations = 100000; // sequentialsearch should find the optimum within 100000 evals
221      foreach (var instanceFactory in instanceFactories) {
222        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
223          RunPureRandomSearchForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
224        }
225      }
226    }
227    [TestMethod]
228    public void RunRandomSearchNoResamplingPoly10Problem() {
229      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
230      {
231        (randSeed) => (ISymbolicExpressionTreeProblem) new SymbolicRegressionPoly10Problem(),
232      };
233
234      var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 23
235      int nReps = 20;
236      int maxIterations = 100000; // sequentialsearch should find the optimum within 100000 evals
237      foreach (var instanceFactory in instanceFactories) {
238        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
239          RunRandomSearchNoResamplingForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
240        }
241      }
242    }
243    #endregion
244
245    #region helpers
246    private IEnumerable<Configuration> GenerateConfigurations(Func<int, ISymbolicExpressionTreeProblem> problemFactory,
247      int nReps,
248      IEnumerable<int> maxSizes
249      ) {
250      var seedRand = new Random(randSeed);
251      // the problem seed is the same for all configuratons
252      // this guarantees that we solve the _same_ problem each time
253      // with different solvers and multiple repetitions
254      var problemSeed = randSeed;
255      for (int i = 0; i < nReps; i++) {
256        // in each repetition use the same random seed for all solver configuratons
257        // do nReps with different seeds for each configuration
258        var solverSeed = seedRand.Next();
259        foreach (var maxSize in maxSizes) {
260          yield return new Configuration {
261            MaxSize = maxSize,
262            Problem = problemFactory(problemSeed),
263            RandSeed = solverSeed
264          };
265        }
266      }
267    }
268
269    private static void RunPureRandomSearchForProblem(
270      int randSeed,
271      ISymbolicExpressionTreeProblem problem,
272      int maxIters,
273      int maxSize
274      ) {
275      var solver = new RandomSearch(problem, new Random(randSeed), maxSize);
276      var problemName = problem.GetType().Name;
277      var bestKnownQuality = problem.BestKnownQuality(maxSize);
278      RunSolver(solver, problemName, bestKnownQuality, maxIters, maxSize);
279    }
280
281    private static void RunRandomSearchNoResamplingForProblem(
282  int randSeed,
283  ISymbolicExpressionTreeProblem problem,
284  int maxIters,
285  int maxSize
286  ) {
287      var solver = new SequentialSearch(problem, maxSize, new Random(randSeed), 0,
288        new GenericGrammarPolicy(problem, new RandomPolicy(), false));
289
290      var problemName = problem.GetType().Name;
291      var bestKnownQuality = problem.BestKnownQuality(maxSize);
292      RunSolver(solver, problemName, bestKnownQuality, maxIters, maxSize);
293    }
294
295    private static void RunSolver(ISolver solver, string problemName, double bestKnownQuality, int maxIters, int maxSize) {
296      int iterations = 0;
297      var globalStatistics = new SentenceSetStatistics(bestKnownQuality);
298      var gpName = solver.GetType().Name;
299      solver.SolutionEvaluated += (sentence, quality) => {
300        iterations++;
301        globalStatistics.AddSentence(sentence, quality);
302
303        if (iterations % 1000 == 0) {
304          Console.WriteLine("\"{0,25}\" {1} \"{2,25}\" {3}", gpName, maxSize, problemName, globalStatistics);
305        }
306      };
307
308      solver.Run(maxIters);
309    }
310    #endregion
311  }
312}
Note: See TracBrowser for help on using the repository browser.