Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestTunedSettings.cs @ 13067

Last change on this file since 13067 was 12099, checked in by gkronber, 10 years ago

#2283: name for all problems (for output), new unit test, and added testsettings file

File size: 19.2 KB
RevLine 
[12014]1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using HeuristicLab.Algorithms.Bandits;
8using HeuristicLab.Algorithms.Bandits.BanditPolicies;
9using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
10using HeuristicLab.Algorithms.Bandits.Models;
11using HeuristicLab.Algorithms.GrammaticalOptimization;
12using HeuristicLab.Common;
[12025]13using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
[12014]14using Microsoft.VisualStudio.TestTools.UnitTesting;
15using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
16
17namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
18  [TestClass]
19  public class TestTunedSettings {
20    private const int randSeed = 31415;
21    internal class Configuration {
22      public IProblem Problem;
23      public int MaxSize;
24      public int RandSeed;
25
26      public override string ToString() {
27        return string.Format("{0} {1} {2}", RandSeed, Problem, MaxSize);
28      }
29    }
30    [TestMethod]
[12025]31    [Timeout(1000 * 60 * 60 * 12)] // 12 hours
32    public void TestAllPoliciesArtificialAnt() {
[12014]33      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
34
35      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
36      {
37        (randSeed) => (ISymbolicExpressionTreeProblem)new SantaFeAntProblem(),
38      };
39
40      var policyFactories = new Func<IBanditPolicy>[]
41        {
42         () => new RandomPolicy(),
[12025]43         () => new ActiveLearningPolicy(), 
[12014]44         () => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"),
45         () => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"),
46         () => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"),
47         () => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"),
48         //() => new GaussianThompsonSamplingPolicy(),
49         () => new GaussianThompsonSamplingPolicy(true),
50         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),
51         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),
52         //() => new BernoulliThompsonSamplingPolicy(),
53         () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),
54         () => new EpsGreedyPolicy(0.01),
55         () => new EpsGreedyPolicy(0.05),
56         () => new EpsGreedyPolicy(0.1),
57         () => new EpsGreedyPolicy(0.2),
58         () => new EpsGreedyPolicy(0.5),
59         () => new UCTPolicy(0.01),
60         () => new UCTPolicy(0.05),
61         () => new UCTPolicy(0.1),
62         () => new UCTPolicy(0.5),
63         () => new UCTPolicy(1),
64         () => new UCTPolicy(2),
65         () => new UCTPolicy( 5),
66         () => new UCTPolicy( 10),
67         () => new ModifiedUCTPolicy(0.01),
68         () => new ModifiedUCTPolicy(0.05),
69         () => new ModifiedUCTPolicy(0.1),
70         () => new ModifiedUCTPolicy(0.5),
71         () => new ModifiedUCTPolicy(1),
72         () => new ModifiedUCTPolicy(2),
73         () => new ModifiedUCTPolicy( 5),
74         () => new ModifiedUCTPolicy( 10),
75         () => new UCB1Policy(),
76         () => new UCB1TunedPolicy(),
77         () => new UCBNormalPolicy(),
78         () => new BoltzmannExplorationPolicy(1),
79         () => new BoltzmannExplorationPolicy(10),
80         () => new BoltzmannExplorationPolicy(20),
81         () => new BoltzmannExplorationPolicy(100),
82         () => new BoltzmannExplorationPolicy(200),
83         () => new BoltzmannExplorationPolicy(500),
[12025]84         () => new ChernoffIntervalEstimationPolicy( 0.01),
85         () => new ChernoffIntervalEstimationPolicy( 0.05),
86         () => new ChernoffIntervalEstimationPolicy( 0.1),
87         () => new ChernoffIntervalEstimationPolicy( 0.2),
[12014]88         () => new ThresholdAscentPolicy(5, 0.01),
89         () => new ThresholdAscentPolicy(5, 0.05),
90         () => new ThresholdAscentPolicy(5, 0.1),
91         () => new ThresholdAscentPolicy(5, 0.2),
92         () => new ThresholdAscentPolicy(10, 0.01),
93         () => new ThresholdAscentPolicy(10, 0.05),
94         () => new ThresholdAscentPolicy(10, 0.1),
95         () => new ThresholdAscentPolicy(10, 0.2),
96         () => new ThresholdAscentPolicy(50, 0.01),
97         () => new ThresholdAscentPolicy(50, 0.05),
98         () => new ThresholdAscentPolicy(50, 0.1),
99         () => new ThresholdAscentPolicy(50, 0.2),
100         () => new ThresholdAscentPolicy(100, 0.01),
101         () => new ThresholdAscentPolicy(100, 0.05),
102         () => new ThresholdAscentPolicy(100, 0.1),
103         () => new ThresholdAscentPolicy(100, 0.2),
104         () => new ThresholdAscentPolicy(500, 0.01),
105         () => new ThresholdAscentPolicy(500, 0.05),
106         () => new ThresholdAscentPolicy(500, 0.1),
107         () => new ThresholdAscentPolicy(500, 0.2),
108         () => new ThresholdAscentPolicy(5000, 0.01),
109         () => new ThresholdAscentPolicy(10000, 0.01),
110        };
111      var maxSizes = new int[] { 17 };  // necessary size for ant programm
112      int nReps = 20;
[12025]113      int maxIterations = 100000;
[12014]114      foreach (var instanceFactory in instanceFactories) {
115        var sumBestQ = 0.0;
116        var sumItersToBest = 0;
117        double fractionSolved = 0.0;
118        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
119          foreach (var policy in policyFactories) {
120            var prob = conf.Problem;
121            var maxLen = conf.MaxSize;
122            var rand = new Random(conf.RandSeed);
123
124            var solver = new SequentialSearch(prob, maxLen, rand, 0, new GenericGrammarPolicy(prob, policy(), true));
125            var problemName = prob.GetType().Name;
126            var policyName = policy().ToString();
127            double bestQ; int itersToBest;
128            RunSolver(solver, problemName, policyName, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
129            sumBestQ += bestQ;
130            sumItersToBest += itersToBest;
131            if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
132          }
133        }
134        // Assert.AreEqual(0.85, fractionSolved, 1E-6);
135        // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
136        // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
137      }
[12025]138    }
[12014]139
[12099]140    [TestMethod]
141    [Timeout(1000 * 60 * 60 * 30)] // 30 hours
142    public void TestAllPoliciesPoly10() {
143      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
[12025]144
[12099]145      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
146      {
147        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionPoly10Problem(),
148      };
149
150      var policyFactories = new Func<IBanditPolicy>[]
151        {
152         () => new RandomPolicy(),
153         () => new ActiveLearningPolicy(), 
154         () => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"),
155         () => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"),
156         () => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"),
157         () => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"),
158         //() => new GaussianThompsonSamplingPolicy(),
159         () => new GaussianThompsonSamplingPolicy(true),
160         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),
161         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),
162         //() => new BernoulliThompsonSamplingPolicy(),
163         () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),
164         () => new EpsGreedyPolicy(0.01),
165         () => new EpsGreedyPolicy(0.05),
166         () => new EpsGreedyPolicy(0.1),
167         () => new EpsGreedyPolicy(0.2),
168         () => new EpsGreedyPolicy(0.5),
169         () => new UCTPolicy(0.01),
170         () => new UCTPolicy(0.05),
171         () => new UCTPolicy(0.1),
172         () => new UCTPolicy(0.5),
173         () => new UCTPolicy(1),
174         () => new UCTPolicy(2),
175         () => new UCTPolicy( 5),
176         () => new UCTPolicy( 10),
177         () => new ModifiedUCTPolicy(0.01),
178         () => new ModifiedUCTPolicy(0.05),
179         () => new ModifiedUCTPolicy(0.1),
180         () => new ModifiedUCTPolicy(0.5),
181         () => new ModifiedUCTPolicy(1),
182         () => new ModifiedUCTPolicy(2),
183         () => new ModifiedUCTPolicy( 5),
184         () => new ModifiedUCTPolicy( 10),
185         () => new UCB1Policy(),
186         () => new UCB1TunedPolicy(),
187         () => new UCBNormalPolicy(),
188         () => new BoltzmannExplorationPolicy(1),
189         () => new BoltzmannExplorationPolicy(10),
190         () => new BoltzmannExplorationPolicy(20),
191         () => new BoltzmannExplorationPolicy(100),
192         () => new BoltzmannExplorationPolicy(200),
193         () => new BoltzmannExplorationPolicy(500),
194         () => new ChernoffIntervalEstimationPolicy( 0.01),
195         () => new ChernoffIntervalEstimationPolicy( 0.05),
196         () => new ChernoffIntervalEstimationPolicy( 0.1),
197         () => new ChernoffIntervalEstimationPolicy( 0.2),
198         () => new ThresholdAscentPolicy(5, 0.01),
199         () => new ThresholdAscentPolicy(5, 0.05),
200         () => new ThresholdAscentPolicy(5, 0.1),
201         () => new ThresholdAscentPolicy(5, 0.2),
202         () => new ThresholdAscentPolicy(10, 0.01),
203         () => new ThresholdAscentPolicy(10, 0.05),
204         () => new ThresholdAscentPolicy(10, 0.1),
205         () => new ThresholdAscentPolicy(10, 0.2),
206         () => new ThresholdAscentPolicy(50, 0.01),
207         () => new ThresholdAscentPolicy(50, 0.05),
208         () => new ThresholdAscentPolicy(50, 0.1),
209         () => new ThresholdAscentPolicy(50, 0.2),
210         () => new ThresholdAscentPolicy(100, 0.01),
211         () => new ThresholdAscentPolicy(100, 0.05),
212         () => new ThresholdAscentPolicy(100, 0.1),
213         () => new ThresholdAscentPolicy(100, 0.2),
214         () => new ThresholdAscentPolicy(500, 0.01),
215         () => new ThresholdAscentPolicy(500, 0.05),
216         () => new ThresholdAscentPolicy(500, 0.1),
217         () => new ThresholdAscentPolicy(500, 0.2),
218         () => new ThresholdAscentPolicy(5000, 0.01),
219         () => new ThresholdAscentPolicy(10000, 0.01),
220        };
221      var maxSizes = new int[] { 23 };  // necessary size symb reg poly 10
222      int nReps = 20;
223      int maxIterations = 100000;
224      foreach (var instanceFactory in instanceFactories) {
225        var sumBestQ = 0.0;
226        var sumItersToBest = 0;
227        double fractionSolved = 0.0;
228        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
229          foreach (var policy in policyFactories) {
230            var prob = conf.Problem;
231            var maxLen = conf.MaxSize;
232            var rand = new Random(conf.RandSeed);
233
234            var solver = new SequentialSearch(prob, maxLen, rand, 0, new GenericGrammarPolicy(prob, policy(), true));
235            var problemName = prob.GetType().Name;
236            var policyName = policy().ToString();
237            double bestQ; int itersToBest;
238            RunSolver(solver, problemName, policyName, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
239            sumBestQ += bestQ;
240            sumItersToBest += itersToBest;
241            if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
242          }
243        }
244        // Assert.AreEqual(0.85, fractionSolved, 1E-6);
245        // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
246        // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
247      }
248    }
249
[12025]250    [TestMethod]
[12099]251    [Timeout(1000 * 60 * 60 * 30)] // 30 hours
252    public void TestAllSymbolicRegression() {
253      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
254
255      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
256      {
257        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Nguyen F7", true),
258        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Keijzer 6", true),
259        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Vladislavleva-4", true),
260        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Spatial", true),
261        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Friedman - II", true),
262        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Tower", true),
263      };
264
265      var policyFactories = new Func<IBanditPolicy>[]
266        {
267         () => new UCTPolicy(0.05),
268         () => new UCTPolicy(0.1),
269         () => new ModifiedUCTPolicy(0.01),
270         () => new ModifiedUCTPolicy(0.05),
271         () => new UCB1Policy(),
272         () => new UCB1TunedPolicy(),
273        };
274      var maxSizes = new int[] { 20 };  // default limit for all problems
275      int nReps = 20;
276      int maxIterations = 10000;
277      foreach (var instanceFactory in instanceFactories) {
278        var sumBestQ = 0.0;
279        var sumItersToBest = 0;
280        double fractionSolved = 0.0;
281        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
282          foreach (var policy in policyFactories) {
283            var prob = conf.Problem;
284            var maxLen = conf.MaxSize;
285            var rand = new Random(conf.RandSeed);
286
287            var solver = new SequentialSearch(prob, maxLen, rand, 0, new GenericGrammarPolicy(prob, policy(), true));
288            var problemName = prob.Name;
289            var policyName = policy().ToString();
290            double bestQ; int itersToBest;
291            RunSolver(solver, problemName, policyName, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
292            sumBestQ += bestQ;
293            sumItersToBest += itersToBest;
294            if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
295          }
296        }
297        // Assert.AreEqual(0.85, fractionSolved, 1E-6);
298        // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
299        // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
300      }
301    }
302
303    [TestMethod]
[12025]304    [Timeout(1000 * 60 * 60 * 12)] // 12 hours
305    // this configuration worked especially well in the experiments
306    public void TestPoly10WithOutConstantOpt() {
307      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
308
309      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
310      {
311        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionPoly10Problem(),
312      };
313
[12099]314      var maxSizes = new int[] { 23 };
[12025]315      int nReps = 20;
316      int maxIterations = 100000;
317      foreach (var instanceFactory in instanceFactories) {
318        var sumBestQ = 0.0;
319        var sumItersToBest = 0;
320        double fractionSolved = 0.0;
321        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
322          var prob = conf.Problem;
323          var maxLen = conf.MaxSize;
324          var rand = new Random(conf.RandSeed);
325
326          var solver = new SequentialSearch(prob, maxLen, rand, 0,
327            new GenericFunctionApproximationGrammarPolicy(prob, true));
328
329          var problemName = prob.GetType().Name;
330          double bestQ; int itersToBest;
331          RunSolver(solver, problemName, string.Empty, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
332          sumBestQ += bestQ;
333          sumItersToBest += itersToBest;
334          if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
335        }
336        // Assert.AreEqual(0.85, fractionSolved, 1E-6);
337        // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
338        // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
339      }
[12014]340    }
341
[12025]342    [TestMethod]
343    [Timeout(1000 * 60 * 60 * 12)] // 12 hours
344    // this configuration worked especially well in the experiments
345    public void TestPoly10WithConstantOpt() {
346      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
347
348      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
349      {
350        (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Poly-10", true ),
351      };
352
[12099]353      var maxSizes = new int[] { 23 };
[12025]354      int nReps = 20;
355      int maxIterations = 100000;
356      foreach (var instanceFactory in instanceFactories) {
357        var sumBestQ = 0.0;
358        var sumItersToBest = 0;
359        double fractionSolved = 0.0;
360        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
361          var prob = conf.Problem;
362          var maxLen = conf.MaxSize;
363          var rand = new Random(conf.RandSeed);
364
365          var solver = new SequentialSearch(prob, maxLen, rand, 0,
366            new GenericFunctionApproximationGrammarPolicy(prob, true));
367
368          var problemName = prob.GetType().Name;
369          double bestQ; int itersToBest;
370          RunSolver(solver, problemName, string.Empty, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
371          sumBestQ += bestQ;
372          sumItersToBest += itersToBest;
373          if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
374        }
375        // Assert.AreEqual(0.85, fractionSolved, 1E-6);
376        // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
377        // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
378      }
379    }
380
[12014]381    private IEnumerable<Configuration> GenerateConfigurations(Func<int, ISymbolicExpressionTreeProblem> problemFactory,
382     int nReps,
383     IEnumerable<int> maxSizes
384     ) {
385      var seedRand = new Random(randSeed);
386      // the problem seed is the same for all configuratons
387      // this guarantees that we solve the _same_ problem each time
388      // with different solvers and multiple repetitions
389      var problemSeed = randSeed;
390      for (int i = 0; i < nReps; i++) {
391        // in each repetition use the same random seed for all solver configuratons
392        // do nReps with different seeds for each configuration
393        var solverSeed = seedRand.Next();
394        foreach (var maxSize in maxSizes) {
395          yield return new Configuration {
396            MaxSize = maxSize,
397            Problem = problemFactory(problemSeed),
398            RandSeed = solverSeed
399          };
400        }
401      }
402    }
403
404
405
406
407    private static void RunSolver(ISolver solver, string problemName, string policyName, double bestKnownQuality, int maxIters, int maxSize,
408      out double bestQ, out int itersToBest) {
409      int iterations = 0;
410      var globalStatistics = new SentenceSetStatistics(bestKnownQuality);
411      var solverName = solver.GetType().Name;
412      double bestQuality = double.NegativeInfinity;
413      int iterationsToBest = -1;
414      solver.SolutionEvaluated += (sentence, quality) => {
415        iterations++;
416        globalStatistics.AddSentence(sentence, quality);
417        if (quality > bestQuality) {
418          bestQuality = quality;
419          iterationsToBest = iterations;
420        }
421        if (iterations % 1000 == 0) {
422          Console.WriteLine("\"{0,25}\" \"{1,25}\" {2} \"{3,25}\" {4}", solverName, policyName, maxSize, problemName, globalStatistics);
423        }
424      };
425
426
427      solver.Run(maxIters);
428
429      bestQ = bestQuality;
430      itersToBest = iterationsToBest;
431    }
432  }
433}
Note: See TracBrowser for help on using the repository browser.