[12289] | 1 | using System;
|
---|
| 2 | using System.Collections;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[12354] | 4 | using System.Globalization;
|
---|
| 5 | using HeuristicLab.Algorithms.Bandits;
|
---|
[12289] | 6 | using HeuristicLab.Algorithms.Bandits.BanditPolicies;
|
---|
| 7 | using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
|
---|
[12354] | 8 | using HeuristicLab.Algorithms.Bandits.Models;
|
---|
[12289] | 9 | using HeuristicLab.Algorithms.GeneticProgramming;
|
---|
| 10 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
| 11 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 12 | using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
|
---|
| 13 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[12354] | 14 | using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
|
---|
[12289] | 15 |
|
---|
| 16 | namespace 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;
|
---|
[12354] | 24 | public IBanditPolicy Policy;
|
---|
[12289] | 25 | public int MaxSize;
|
---|
| 26 | public int RandSeed;
|
---|
| 27 |
|
---|
| 28 | public override string ToString() {
|
---|
[12354] | 29 | return string.Format("{0} {1} {2} {3}", RandSeed, Problem, Policy, MaxSize);
|
---|
[12289] | 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[12354] | 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 | };
|
---|
[12289] | 98 |
|
---|
| 99 | #region artificial ant
|
---|
| 100 | [TestMethod]
|
---|
[12354] | 101 | [Timeout(1000 * 60 * 60 * 72)] // 72 hours
|
---|
[12370] | 102 | public void RunMctsArtificialAntProblem() {
|
---|
[12354] | 103 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
| 104 |
|
---|
[12289] | 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) {
|
---|
[12354] | 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 | }
|
---|
[12289] | 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | #endregion
|
---|
| 123 |
|
---|
| 124 | #region symb-reg-poly-10
|
---|
| 125 | [TestMethod]
|
---|
[12370] | 126 | [Timeout(1000 * 60 * 60 * 120)] // 120 hours
|
---|
| 127 | public void RunMctsPoly10Problem() {
|
---|
[12354] | 128 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
| 129 |
|
---|
[12289] | 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;
|
---|
[12354] | 137 | int maxIterations = 100000; // sequentialsearch should find the optimum within 100000 evals
|
---|
[12289] | 138 | foreach (var instanceFactory in instanceFactories) {
|
---|
[12354] | 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 | }
|
---|
[12289] | 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
| 149 | #region helpers
|
---|
[12354] | 150 | private IEnumerable<Configuration> GenerateConfigurations(
|
---|
| 151 | Func<int, ISymbolicExpressionTreeProblem> problemFactory,
|
---|
| 152 | Func<IBanditPolicy> policyFactory,
|
---|
[12289] | 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),
|
---|
[12354] | 169 | Policy = policyFactory(),
|
---|
[12289] | 170 | RandSeed = solverSeed
|
---|
| 171 | };
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | private static void RunMctsForProblem(
|
---|
| 177 | int randSeed,
|
---|
| 178 | IProblem problem,
|
---|
[12354] | 179 | IBanditPolicy policy,
|
---|
[12289] | 180 | int maxIters,
|
---|
| 181 | int maxSize
|
---|
| 182 | ) {
|
---|
| 183 | var solver = new SequentialSearch(problem, maxSize, new Random(randSeed), 0,
|
---|
[12354] | 184 | new GenericGrammarPolicy(problem, policy, false));
|
---|
[12289] | 185 | var problemName = problem.GetType().Name;
|
---|
[12370] | 186 | RunSolver(solver, problemName, policy.ToString(), maxIters, maxSize);
|
---|
[12289] | 187 | }
|
---|
| 188 |
|
---|
[12370] | 189 | private static void RunSolver(ISolver solver, string problemName, string policyName, int maxIters, int maxSize) {
|
---|
[12289] | 190 | int iterations = 0;
|
---|
[12370] | 191 | var globalStatistics = new SentenceSetStatistics(1.0);
|
---|
| 192 | var solverName = solver.GetType().Name;
|
---|
[12289] | 193 | solver.SolutionEvaluated += (sentence, quality) => {
|
---|
| 194 | iterations++;
|
---|
| 195 | globalStatistics.AddSentence(sentence, quality);
|
---|
| 196 |
|
---|
| 197 | if (iterations % 1000 == 0) {
|
---|
[12370] | 198 | Console.WriteLine("\"{0,25}\" {1} \"{2,25}\" \"{3}\" {4}", solverName, maxSize, problemName, policyName, globalStatistics);
|
---|
[12289] | 199 | }
|
---|
| 200 | };
|
---|
| 201 |
|
---|
| 202 | solver.Run(maxIters);
|
---|
| 203 | }
|
---|
| 204 | #endregion
|
---|
| 205 | }
|
---|
| 206 | }
|
---|