Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/Test/RunMctsExperiments.cs @ 12370

Last change on this file since 12370 was 12370, checked in by gkronber, 9 years ago

#2283: renamed unit test

File size: 8.3 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Globalization;
5using HeuristicLab.Algorithms.Bandits;
6using HeuristicLab.Algorithms.Bandits.BanditPolicies;
7using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
8using HeuristicLab.Algorithms.Bandits.Models;
9using HeuristicLab.Algorithms.GeneticProgramming;
10using HeuristicLab.Algorithms.GrammaticalOptimization;
11using HeuristicLab.Problems.GrammaticalOptimization;
12using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
13using Microsoft.VisualStudio.TestTools.UnitTesting;
14using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
15
16namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
17
18  [TestClass]
19  public class RunMctsExperiments {
20    private readonly static int randSeed = 31415;
21
22    internal class Configuration {
23      public ISymbolicExpressionTreeProblem Problem;
24      public IBanditPolicy Policy;
25      public int MaxSize;
26      public int RandSeed;
27
28      public override string ToString() {
29        return string.Format("{0} {1} {2} {3}", RandSeed, Problem, Policy, MaxSize);
30      }
31    }
32
33    private Func<IBanditPolicy>[] policyFactories = new Func<IBanditPolicy>[]
34    {
35          () => new RandomPolicy(),
36          () => new ActiveLearningPolicy(), 
37         () => new GaussianThompsonSamplingPolicy(true),
38         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),
39         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),
40         () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),
41         () => new EpsGreedyPolicy(0.01),
42         () => new EpsGreedyPolicy(0.05),
43         () => new EpsGreedyPolicy(0.1),
44         () => new EpsGreedyPolicy(0.2),
45         () => new EpsGreedyPolicy(0.5),
46         () => new UCTPolicy(0.01),
47         () => new UCTPolicy(0.05),
48         () => new UCTPolicy(0.1),
49         () => new UCTPolicy(0.5),
50         () => new UCTPolicy(1),
51         () => new UCTPolicy(2),
52         () => new UCTPolicy( 5),
53         () => new UCTPolicy( 10),
54         () => new ModifiedUCTPolicy(0.01),
55         () => new ModifiedUCTPolicy(0.05),
56         () => new ModifiedUCTPolicy(0.1),
57         () => new ModifiedUCTPolicy(0.5),
58         () => new ModifiedUCTPolicy(1),
59         () => new ModifiedUCTPolicy(2),
60         () => new ModifiedUCTPolicy( 5),
61         () => new ModifiedUCTPolicy( 10),
62         () => new UCB1Policy(),
63         () => new UCB1TunedPolicy(),
64         () => new UCBNormalPolicy(),
65         () => new BoltzmannExplorationPolicy(1),
66         () => new BoltzmannExplorationPolicy(10),
67         () => new BoltzmannExplorationPolicy(20),
68         () => new BoltzmannExplorationPolicy(100),
69         () => new BoltzmannExplorationPolicy(200),
70         () => new BoltzmannExplorationPolicy(500),
71          () => new ChernoffIntervalEstimationPolicy( 0.01),
72          () => new ChernoffIntervalEstimationPolicy( 0.05),
73          () => new ChernoffIntervalEstimationPolicy( 0.1),
74          () => new ChernoffIntervalEstimationPolicy( 0.2),
75         () => new ThresholdAscentPolicy(5, 0.01),
76         () => new ThresholdAscentPolicy(5, 0.05),
77         () => new ThresholdAscentPolicy(5, 0.1),
78         () => new ThresholdAscentPolicy(5, 0.2),
79         () => new ThresholdAscentPolicy(10, 0.01),
80         () => new ThresholdAscentPolicy(10, 0.05),
81         () => new ThresholdAscentPolicy(10, 0.1),
82         () => new ThresholdAscentPolicy(10, 0.2),
83         () => new ThresholdAscentPolicy(50, 0.01),
84         () => new ThresholdAscentPolicy(50, 0.05),
85         () => new ThresholdAscentPolicy(50, 0.1),
86         () => new ThresholdAscentPolicy(50, 0.2),
87         () => new ThresholdAscentPolicy(100, 0.01),
88         () => new ThresholdAscentPolicy(100, 0.05),
89         () => new ThresholdAscentPolicy(100, 0.1),
90         () => new ThresholdAscentPolicy(100, 0.2),
91         () => new ThresholdAscentPolicy(500, 0.01),
92         () => new ThresholdAscentPolicy(500, 0.05),
93         () => new ThresholdAscentPolicy(500, 0.1),
94         () => new ThresholdAscentPolicy(500, 0.2),
95         () => new ThresholdAscentPolicy(5000, 0.01),
96         () => new ThresholdAscentPolicy(10000, 0.01), 
97    };
98
99    #region artificial ant
100    [TestMethod]
101    [Timeout(1000 * 60 * 60 * 72)] // 72 hours
102    public void RunMctsArtificialAntProblem() {
103      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
104
105      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
106      {
107        (randSeed) => (ISymbolicExpressionTreeProblem) new SantaFeAntProblem(),
108      };
109
110      var maxSizes = new int[] { 17 }; // size of sequential representation is 17
111      int nReps = 30;
112      int maxIterations = 100000; // randomsearch finds the optimum almost always for 100000 evals
113      foreach (var instanceFactory in instanceFactories) {
114        foreach (var policyFactory in policyFactories) {
115          foreach (var conf in GenerateConfigurations(instanceFactory, policyFactory, nReps, maxSizes)) {
116            RunMctsForProblem(conf.RandSeed, conf.Problem, conf.Policy, maxIterations, conf.MaxSize);
117          }
118        }
119      }
120    }
121
122    #endregion
123
124    #region symb-reg-poly-10
125    [TestMethod]
126    [Timeout(1000 * 60 * 60 * 120)] // 120 hours
127    public void RunMctsPoly10Problem() {
128      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
129
130      var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
131      {
132        (randSeed) => (ISymbolicExpressionTreeProblem) new SymbolicRegressionPoly10Problem(),
133      };
134
135      var maxSizes = new int[] { 23 }; // size of sequential representation is 23
136      int nReps = 30;
137      int maxIterations = 100000; // sequentialsearch should find the optimum within 100000 evals
138      foreach (var instanceFactory in instanceFactories) {
139        foreach (var policyFactory in policyFactories) {
140          foreach (var conf in GenerateConfigurations(instanceFactory, policyFactory, nReps, maxSizes)) {
141            RunMctsForProblem(conf.RandSeed, conf.Problem, conf.Policy, maxIterations, conf.MaxSize);
142          }
143        }
144      }
145    }
146
147    #endregion
148
149    #region helpers
150    private IEnumerable<Configuration> GenerateConfigurations(
151      Func<int, ISymbolicExpressionTreeProblem> problemFactory,
152      Func<IBanditPolicy> policyFactory,
153      int nReps,
154      IEnumerable<int> maxSizes
155      ) {
156      var seedRand = new Random(randSeed);
157      // the problem seed is the same for all configuratons
158      // this guarantees that we solve the _same_ problem each time
159      // with different solvers and multiple repetitions
160      var problemSeed = randSeed;
161      for (int i = 0; i < nReps; i++) {
162        // in each repetition use the same random seed for all solver configuratons
163        // do nReps with different seeds for each configuration
164        var solverSeed = seedRand.Next();
165        foreach (var maxSize in maxSizes) {
166          yield return new Configuration {
167            MaxSize = maxSize,
168            Problem = problemFactory(problemSeed),
169            Policy = policyFactory(),
170            RandSeed = solverSeed
171          };
172        }
173      }
174    }
175
176    private static void RunMctsForProblem(
177      int randSeed,
178      IProblem problem,
179      IBanditPolicy policy,
180      int maxIters,
181      int maxSize
182      ) {
183      var solver = new SequentialSearch(problem, maxSize, new Random(randSeed), 0,
184        new GenericGrammarPolicy(problem, policy, false));
185      var problemName = problem.GetType().Name;
186      RunSolver(solver, problemName, policy.ToString(), maxIters, maxSize);
187    }
188
189    private static void RunSolver(ISolver solver, string problemName, string policyName, int maxIters, int maxSize) {
190      int iterations = 0;
191      var globalStatistics = new SentenceSetStatistics(1.0);
192      var solverName = solver.GetType().Name;
193      solver.SolutionEvaluated += (sentence, quality) => {
194        iterations++;
195        globalStatistics.AddSentence(sentence, quality);
196
197        if (iterations % 1000 == 0) {
198          Console.WriteLine("\"{0,25}\" {1} \"{2,25}\" \"{3}\" {4}", solverName, maxSize, problemName, policyName, globalStatistics);
199        }
200      };
201
202      solver.Run(maxIters);
203    }
204    #endregion
205  }
206}
Note: See TracBrowser for help on using the repository browser.