[11795] | 1 | using System;
|
---|
[11659] | 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Diagnostics;
|
---|
[11730] | 4 | using System.Globalization;
|
---|
[11895] | 5 | using System.Runtime.Remoting.Messaging;
|
---|
[11659] | 6 | using System.Text;
|
---|
[11846] | 7 | using System.Threading;
|
---|
[11727] | 8 | using System.Threading.Tasks;
|
---|
| 9 | using HeuristicLab.Algorithms.Bandits;
|
---|
[11742] | 10 | using HeuristicLab.Algorithms.Bandits.BanditPolicies;
|
---|
[11792] | 11 | using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
|
---|
[11730] | 12 | using HeuristicLab.Algorithms.Bandits.Models;
|
---|
[11846] | 13 | using HeuristicLab.Algorithms.GeneticProgramming;
|
---|
[11659] | 14 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
| 15 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
[11895] | 16 | using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
|
---|
[11792] | 17 | using BoltzmannExplorationPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.BoltzmannExplorationPolicy;
|
---|
| 18 | using EpsGreedyPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.EpsGreedyPolicy;
|
---|
[11846] | 19 | using IProblem = HeuristicLab.Problems.GrammaticalOptimization.IProblem;
|
---|
[11792] | 20 | using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
|
---|
| 21 | using UCTPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.UCTPolicy;
|
---|
[11659] | 22 |
|
---|
| 23 | namespace Main {
|
---|
| 24 | class Program {
|
---|
| 25 | static void Main(string[] args) {
|
---|
[11730] | 26 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
| 27 |
|
---|
[11832] | 28 | //RunDemo();
|
---|
[11972] | 29 | //RunGpDemo();
|
---|
| 30 | //RunGridTest();
|
---|
[11973] | 31 | //RunGpGridTest();
|
---|
| 32 | RunFunApproxTest();
|
---|
[11727] | 33 | }
|
---|
| 34 |
|
---|
| 35 | private static void RunGridTest() {
|
---|
[11966] | 36 | int maxIterations = 200000; // for poly-10 with 50000 evaluations no successful try with hl yet
|
---|
[11732] | 37 | //var globalRandom = new Random(31415);
|
---|
[11730] | 38 | var localRandSeed = 31415;
|
---|
[11966] | 39 | var reps = 20;
|
---|
[11730] | 40 |
|
---|
[11799] | 41 | var policyFactories = new Func<IBanditPolicy>[]
|
---|
[11727] | 42 | {
|
---|
[11792] | 43 | () => new RandomPolicy(),
|
---|
| 44 | () => new ActiveLearningPolicy(),
|
---|
[11742] | 45 | () => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
| 46 | () => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
| 47 | () => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
| 48 | () => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
| 49 | //() => new GaussianThompsonSamplingPolicy(),
|
---|
[11732] | 50 | () => new GaussianThompsonSamplingPolicy(true),
|
---|
[11742] | 51 | () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),
|
---|
| 52 | () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),
|
---|
| 53 | //() => new BernoulliThompsonSamplingPolicy(),
|
---|
[11732] | 54 | () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),
|
---|
| 55 | () => new EpsGreedyPolicy(0.01),
|
---|
| 56 | () => new EpsGreedyPolicy(0.05),
|
---|
| 57 | () => new EpsGreedyPolicy(0.1),
|
---|
| 58 | () => new EpsGreedyPolicy(0.2),
|
---|
| 59 | () => new EpsGreedyPolicy(0.5),
|
---|
[11801] | 60 | () => new UCTPolicy(0.01),
|
---|
| 61 | () => new UCTPolicy(0.05),
|
---|
[11732] | 62 | () => new UCTPolicy(0.1),
|
---|
| 63 | () => new UCTPolicy(0.5),
|
---|
| 64 | () => new UCTPolicy(1),
|
---|
| 65 | () => new UCTPolicy(2),
|
---|
| 66 | () => new UCTPolicy( 5),
|
---|
| 67 | () => new UCTPolicy( 10),
|
---|
[11806] | 68 | () => new ModifiedUCTPolicy(0.01),
|
---|
| 69 | () => new ModifiedUCTPolicy(0.05),
|
---|
| 70 | () => new ModifiedUCTPolicy(0.1),
|
---|
| 71 | () => new ModifiedUCTPolicy(0.5),
|
---|
| 72 | () => new ModifiedUCTPolicy(1),
|
---|
| 73 | () => new ModifiedUCTPolicy(2),
|
---|
| 74 | () => new ModifiedUCTPolicy( 5),
|
---|
| 75 | () => new ModifiedUCTPolicy( 10),
|
---|
[11732] | 76 | () => new UCB1Policy(),
|
---|
| 77 | () => new UCB1TunedPolicy(),
|
---|
| 78 | () => new UCBNormalPolicy(),
|
---|
| 79 | () => new BoltzmannExplorationPolicy(1),
|
---|
| 80 | () => new BoltzmannExplorationPolicy(10),
|
---|
| 81 | () => new BoltzmannExplorationPolicy(20),
|
---|
| 82 | () => new BoltzmannExplorationPolicy(100),
|
---|
[11792] | 83 | () => new BoltzmannExplorationPolicy(200),
|
---|
| 84 | () => new BoltzmannExplorationPolicy(500),
|
---|
[11732] | 85 | () => new ChernoffIntervalEstimationPolicy( 0.01),
|
---|
| 86 | () => new ChernoffIntervalEstimationPolicy( 0.05),
|
---|
| 87 | () => new ChernoffIntervalEstimationPolicy( 0.1),
|
---|
| 88 | () => new ChernoffIntervalEstimationPolicy( 0.2),
|
---|
[11799] | 89 | () => new ThresholdAscentPolicy(5, 0.01),
|
---|
| 90 | () => new ThresholdAscentPolicy(5, 0.05),
|
---|
| 91 | () => new ThresholdAscentPolicy(5, 0.1),
|
---|
| 92 | () => new ThresholdAscentPolicy(5, 0.2),
|
---|
[11742] | 93 | () => new ThresholdAscentPolicy(10, 0.01),
|
---|
| 94 | () => new ThresholdAscentPolicy(10, 0.05),
|
---|
| 95 | () => new ThresholdAscentPolicy(10, 0.1),
|
---|
| 96 | () => new ThresholdAscentPolicy(10, 0.2),
|
---|
[11799] | 97 | () => new ThresholdAscentPolicy(50, 0.01),
|
---|
| 98 | () => new ThresholdAscentPolicy(50, 0.05),
|
---|
| 99 | () => new ThresholdAscentPolicy(50, 0.1),
|
---|
| 100 | () => new ThresholdAscentPolicy(50, 0.2),
|
---|
[11742] | 101 | () => new ThresholdAscentPolicy(100, 0.01),
|
---|
| 102 | () => new ThresholdAscentPolicy(100, 0.05),
|
---|
| 103 | () => new ThresholdAscentPolicy(100, 0.1),
|
---|
| 104 | () => new ThresholdAscentPolicy(100, 0.2),
|
---|
[11799] | 105 | () => new ThresholdAscentPolicy(500, 0.01),
|
---|
| 106 | () => new ThresholdAscentPolicy(500, 0.05),
|
---|
| 107 | () => new ThresholdAscentPolicy(500, 0.1),
|
---|
| 108 | () => new ThresholdAscentPolicy(500, 0.2),
|
---|
[11792] | 109 | //() => new ThresholdAscentPolicy(5000, 0.01),
|
---|
| 110 | //() => new ThresholdAscentPolicy(10000, 0.01),
|
---|
[11727] | 111 | };
|
---|
| 112 |
|
---|
[11799] | 113 | var instanceFactories = new Func<Random, Tuple<IProblem, int>>[]
|
---|
| 114 | {
|
---|
[11966] | 115 | (rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17),
|
---|
[11832] | 116 | //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:false ), 15),
|
---|
| 117 | //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:true ), 15),
|
---|
| 118 | //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:false), 15),
|
---|
| 119 | //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:true), 15),
|
---|
| 120 | (rand) => Tuple.Create((IProblem)new SymbolicRegressionPoly10Problem(), 23)
|
---|
[11799] | 121 | };
|
---|
| 122 |
|
---|
| 123 | foreach (var instanceFactory in instanceFactories) {
|
---|
[11966] | 124 | foreach (var useCanonical in new bool[] { true, false }) {
|
---|
| 125 | foreach (var randomTries in new int[] { 0, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) {
|
---|
[11799] | 126 | foreach (var policyFactory in policyFactories) {
|
---|
[11792] | 127 | var myRandomTries = randomTries;
|
---|
| 128 | var localRand = new Random(localRandSeed);
|
---|
| 129 | var options = new ParallelOptions();
|
---|
[11799] | 130 | options.MaxDegreeOfParallelism = 4;
|
---|
[11792] | 131 | Parallel.For(0, reps, options, (i) => {
|
---|
| 132 | Random myLocalRand;
|
---|
| 133 | lock (localRand)
|
---|
| 134 | myLocalRand = new Random(localRand.Next());
|
---|
[11730] | 135 |
|
---|
[11792] | 136 | int iterations = 0;
|
---|
| 137 | var globalStatistics = new SentenceSetStatistics();
|
---|
[11727] | 138 |
|
---|
[11792] | 139 | // var problem = new SymbolicRegressionPoly10Problem();
|
---|
| 140 | // var problem = new SantaFeAntProblem();
|
---|
| 141 | //var problem = new PalindromeProblem();
|
---|
| 142 | //var problem = new HardPalindromeProblem();
|
---|
| 143 | //var problem = new RoyalPairProblem();
|
---|
| 144 | //var problem = new EvenParityProblem();
|
---|
[11799] | 145 | // var alg = new MctsSampler(problem.Item1, problem.Item2, myLocalRand, myRandomTries, policy());
|
---|
| 146 | var instance = instanceFactory(myLocalRand);
|
---|
| 147 | var problem = instance.Item1;
|
---|
| 148 | var maxLen = instance.Item2;
|
---|
[11966] | 149 | var alg = new SequentialSearch(problem, maxLen, myLocalRand, myRandomTries,
|
---|
| 150 | new GenericGrammarPolicy(problem, policyFactory(), useCanonical));
|
---|
| 151 | // var alg = new SequentialSearch(problem, maxLen, myLocalRand,
|
---|
| 152 | // myRandomTries,
|
---|
| 153 | // new GenericFunctionApproximationGrammarPolicy(problem,
|
---|
| 154 | // useCanonical));
|
---|
[11792] | 155 | //var alg = new ExhaustiveBreadthFirstSearch(problem, 25);
|
---|
| 156 | //var alg = new AlternativesContextSampler(problem, 25);
|
---|
[11727] | 157 |
|
---|
[11792] | 158 | alg.SolutionEvaluated += (sentence, quality) => {
|
---|
| 159 | iterations++;
|
---|
| 160 | globalStatistics.AddSentence(sentence, quality);
|
---|
[11832] | 161 | if (iterations % 1000 == 0) {
|
---|
| 162 | Console.WriteLine("{0,3} {1,5} \"{2,25}\" {3} {4} {5}", i, myRandomTries, policyFactory(), useCanonical, problem.ToString(), globalStatistics);
|
---|
[11792] | 163 | }
|
---|
| 164 | };
|
---|
| 165 | alg.FoundNewBestSolution += (sentence, quality) => {
|
---|
[11799] | 166 | //Console.WriteLine("{0,5} {1,25} {2} {3}",
|
---|
| 167 | // myRandomTries, policyFactory(), useCanonical,
|
---|
| 168 | // globalStatistics);
|
---|
[11792] | 169 | };
|
---|
[11727] | 170 |
|
---|
[11792] | 171 | alg.Run(maxIterations);
|
---|
| 172 | });
|
---|
| 173 | }
|
---|
[11732] | 174 | }
|
---|
[11799] | 175 | }
|
---|
| 176 | }
|
---|
[11727] | 177 | }
|
---|
| 178 |
|
---|
| 179 | private static void RunDemo() {
|
---|
[11747] | 180 | // TODO: unify MCTS, TD and ContextMCTS Solvers (stateInfos)
|
---|
[11732] | 181 | // TODO: test with eps-greedy using max instead of average as value (seems to work well for symb-reg! explore further!)
|
---|
| 182 | // TODO: separate value function from policy
|
---|
[11742] | 183 | // TODO: warum funktioniert die alte Implementierung von GaussianThompson besser fÃŒr SantaFe als neue? Siehe Vergleich: alte vs. neue implementierung GaussianThompsonSampling
|
---|
[11730] | 184 | // TODO: why does GaussianThompsonSampling work so well with MCTS for the artificial ant problem?
|
---|
| 185 | // TODO: research thompson sampling for max bandit?
|
---|
[11732] | 186 | // TODO: verify TA implementation using example from the original paper
|
---|
[11730] | 187 | // TODO: implement thompson sampling for gaussian mixture models
|
---|
| 188 | // TODO: gleichzeitige modellierung von transformierter zielvariable (y, 1/y, log(y), exp(y), sqrt(y), ...)
|
---|
| 189 | // TODO: vergleich bei complete-randomly möglichst kurze sÀtze generieren vs. einfach zufÀllig alternativen wÀhlen
|
---|
| 190 | // TODO: reward discounting (fÌr verÀnderliche reward distributions Ìber zeit). speziellen unit-test dafÌr erstellen
|
---|
[11732] | 191 | // TODO: constant optimization
|
---|
[11727] | 192 |
|
---|
[11730] | 193 |
|
---|
[11832] | 194 | int maxIterations = 1000000;
|
---|
[11659] | 195 | int iterations = 0;
|
---|
| 196 | var sw = new Stopwatch();
|
---|
[11770] | 197 |
|
---|
[11727] | 198 | var globalStatistics = new SentenceSetStatistics();
|
---|
[11730] | 199 | var random = new Random();
|
---|
[11659] | 200 |
|
---|
[11799] | 201 |
|
---|
[11806] | 202 | //var problem = new RoyalSequenceProblem(random, 10, 30, 2, 1, 0);
|
---|
[11832] | 203 | // var phraseLen = 3;
|
---|
| 204 | // var numPhrases = 5;
|
---|
| 205 | // var problem = new RoyalPhraseSequenceProblem(random, 10, numPhrases, phraseLen: phraseLen, numCorrectPhrases: 1, correctReward: 1, incorrectReward: 0.0, phrasesAsSets: false);
|
---|
| 206 |
|
---|
[11755] | 207 | //var phraseLen = 3;
|
---|
| 208 | //var numPhrases = 5;
|
---|
[11832] | 209 | //var problem = new FindPhrasesProblem(random, 10, numPhrases, phraseLen, numOptimalPhrases: numPhrases, numDecoyPhrases: 0, correctReward: 1.0, decoyReward: 0, phrasesAsSets: false);
|
---|
[11747] | 210 |
|
---|
[11795] | 211 | // good results for symb-reg
|
---|
| 212 | // prev results: e.g. 10 randomtries and EpsGreedyPolicy(0.2, (aInfo)=>aInfo.MaxReward)
|
---|
| 213 | // 2015 01 19: grid test with canonical states:
|
---|
| 214 | // - EpsGreedyPolicy(0.20,max)
|
---|
| 215 | // - GenericThompsonSamplingPolicy("")
|
---|
[11801] | 216 | // - UCTPolicy(0.10) (5 of 5 runs, 35000 iters avg.), 10 successful runs of 10 with rand-tries 0, bei 40000 iters 9 / 10, bei 30000 1 / 10
|
---|
[11832] | 217 | // 2015 01 22: symb-reg: grid test on find-phrases problem showed good results for UCB1TunedPolicy and SequentialSearch with canonical states
|
---|
| 218 | // - symb-reg: consistent results with UCB1Tuned. finds optimal solution in ~50k iters (new GenericGrammarPolicy(problem, new UCB1TunedPolicy(), true));
|
---|
| 219 | // 2015 01 23: grid test with canonical states:
|
---|
| 220 | // - UCTPolicy(0.10) und UCBNormalPolicy 10/10 optimale Lösungen bei max. 50k iters, etwas schlechter: generic-thompson with variable sigma und bolzmannexploration (100)
|
---|
[11799] | 221 |
|
---|
[11832] | 222 |
|
---|
[11795] | 223 | // good results for artificial ant:
|
---|
| 224 | // prev results:
|
---|
| 225 | // - var alg = new MctsSampler(problem, 17, random, 1, (rand, numActions) => new ThresholdAscentPolicy(numActions, 500, 0.01));
|
---|
| 226 | // - GaussianModelWithUnknownVariance (and Q= 0.99-quantil) also works well for Ant
|
---|
| 227 | // 2015 01 19: grid test with canonical states (non-canonical slightly worse)
|
---|
[11832] | 228 | // - ant: Threshold Ascent (best 100, 0.01; all variants relatively good)
|
---|
| 229 | // - ant: Policies where the variance has a large weight compared to the mean? (Gaussian(compatible), Gaussian with fixed variance, UCT with large c, alle TA)
|
---|
| 230 | // - ant: UCB1Tuned with canonical states also works very well for the artificial ant! constistent solutions in less than 10k iters
|
---|
[11770] | 231 |
|
---|
[11972] | 232 | //var problem = new SymbolicRegressionPoly10Problem();
|
---|
[11832] | 233 | //var problem = new SantaFeAntProblem();
|
---|
[11972] | 234 | var problem = new SymbolicRegressionProblem(random, "Breiman");
|
---|
[11727] | 235 | //var problem = new PalindromeProblem();
|
---|
| 236 | //var problem = new HardPalindromeProblem();
|
---|
| 237 | //var problem = new RoyalPairProblem();
|
---|
| 238 | //var problem = new EvenParityProblem();
|
---|
[11747] | 239 | // symbreg length = 11 q = 0.824522210419616
|
---|
[11755] | 240 | //var alg = new MctsSampler(problem, 23, random, 0, new BoltzmannExplorationPolicy(100));
|
---|
[11770] | 241 | //var alg = new MctsSampler(problem, 23, random, 0, new EpsGreedyPolicy(0.1));
|
---|
[11806] | 242 | //var alg = new SequentialSearch(problem, 23, random, 0,
|
---|
[11832] | 243 | // new HeuristicLab.Algorithms.Bandits.GrammarPolicies.QLearningGrammarPolicy(problem, new BoltzmannExplorationPolicy(10),
|
---|
| 244 | // 1, 1, true));
|
---|
| 245 | //var alg = new SequentialSearch(problem, 23, random, 0,
|
---|
| 246 | // new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericContextualGrammarPolicy(problem, new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)), true));
|
---|
[11972] | 247 | var alg = new SequentialSearch(problem, 30, random, 0,
|
---|
[11832] | 248 | new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericFunctionApproximationGrammarPolicy(problem, true));
|
---|
[11747] | 249 | //var alg = new MctsQLearningSampler(problem, sentenceLen, random, 0, null);
|
---|
| 250 | //var alg = new MctsQLearningSampler(problem, 30, random, 0, new EpsGreedyPolicy(0.2));
|
---|
| 251 | //var alg = new MctsContextualSampler(problem, 23, random, 0); // must visit each canonical solution only once
|
---|
| 252 | //var alg = new TemporalDifferenceTreeSearchSampler(problem, 30, random, 1);
|
---|
| 253 | //var alg = new ExhaustiveBreadthFirstSearch(problem, 7);
|
---|
[11730] | 254 | //var alg = new AlternativesContextSampler(problem, random, 17, 4, (rand, numActions) => new RandomPolicy(rand, numActions));
|
---|
| 255 | //var alg = new ExhaustiveDepthFirstSearch(problem, 17);
|
---|
| 256 | // var alg = new AlternativesSampler(problem, 17);
|
---|
[11732] | 257 | // var alg = new RandomSearch(problem, random, 17);
|
---|
[11747] | 258 | //var alg = new ExhaustiveRandomFirstSearch(problem, random, 17);
|
---|
[11659] | 259 |
|
---|
[11727] | 260 | alg.FoundNewBestSolution += (sentence, quality) => {
|
---|
[11832] | 261 | //Console.WriteLine("{0}", globalStatistics);
|
---|
[11745] | 262 | //Console.ReadLine();
|
---|
[11659] | 263 | };
|
---|
[11727] | 264 | alg.SolutionEvaluated += (sentence, quality) => {
|
---|
[11659] | 265 | iterations++;
|
---|
[11727] | 266 | globalStatistics.AddSentence(sentence, quality);
|
---|
[11832] | 267 |
|
---|
[11972] | 268 | //if (iterations % 100 == 0) {
|
---|
| 269 | // if (iterations % 10000 == 0) Console.Clear();
|
---|
| 270 | // Console.SetCursorPosition(0, 0);
|
---|
| 271 | // alg.PrintStats();
|
---|
| 272 | //}
|
---|
[11832] | 273 |
|
---|
[11747] | 274 | //Console.WriteLine(sentence);
|
---|
[11745] | 275 |
|
---|
[11972] | 276 | if (iterations % 100 == 0) {
|
---|
| 277 | Console.WriteLine("{0}", globalStatistics);
|
---|
| 278 | }
|
---|
[11659] | 279 | };
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | sw.Start();
|
---|
| 283 |
|
---|
[11727] | 284 | alg.Run(maxIterations);
|
---|
[11659] | 285 |
|
---|
| 286 | sw.Stop();
|
---|
| 287 |
|
---|
[11770] | 288 | Console.Clear();
|
---|
| 289 | alg.PrintStats();
|
---|
| 290 | Console.WriteLine(globalStatistics);
|
---|
[11659] | 291 | Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol",
|
---|
| 292 | sw.Elapsed.TotalSeconds,
|
---|
| 293 | maxIterations / (double)sw.Elapsed.TotalSeconds,
|
---|
| 294 | (double)sw.ElapsedMilliseconds * 1000 / maxIterations);
|
---|
| 295 | }
|
---|
[11846] | 296 |
|
---|
| 297 | public static void RunGpDemo() {
|
---|
| 298 | int iterations = 0;
|
---|
[11865] | 299 | const int seed = 31415;
|
---|
[11846] | 300 | const int maxIterations = 100000;
|
---|
[11865] | 301 |
|
---|
[11895] | 302 | //var prob = new SymbolicRegressionProblem(new Random(31415), "Tower");
|
---|
| 303 | var prob = new SymbolicRegressionPoly10Problem();
|
---|
| 304 | var sgp = new OffspringSelectionGP(prob, new Random(seed), true);
|
---|
| 305 | RunGP(sgp, prob, 200000, 500, 0.15, 50);
|
---|
[11865] | 306 | }
|
---|
| 307 |
|
---|
| 308 |
|
---|
[11973] | 309 | private static void RunFunApproxTest() {
|
---|
| 310 | const int nReps = 20;
|
---|
| 311 | const int seed = 31415;
|
---|
| 312 | //const int maxIters = 50000;
|
---|
| 313 | var rand = new Random(seed);
|
---|
| 314 | var problemFactories = new Func<Tuple<int, int, ISymbolicExpressionTreeProblem>>[]
|
---|
| 315 | {
|
---|
| 316 | () => Tuple.Create(100000, 23, (ISymbolicExpressionTreeProblem)new SymbolicRegressionPoly10Problem()),
|
---|
| 317 | //() => Tuple.Create(100000, 17, (ISymbolicExpressionTreeProblem)new SantaFeAntProblem()),
|
---|
| 318 | //() => Tuple.Create(50000, 32,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 319 | //() => Tuple.Create(50000, 64, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
|
---|
| 320 | //() => Tuple.Create(50000, 64,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 321 | //() => Tuple.Create(50000, 128, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
|
---|
| 322 | //() => Tuple.Create(50000, 128,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 323 | //() => Tuple.Create(50000, 256, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
|
---|
| 324 | //() => Tuple.Create(50000, 256,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 325 | //() => new RoyalPairProblem(),
|
---|
| 326 | //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 0, 1, 0, true),
|
---|
| 327 | //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 0, 1, 0, false),
|
---|
| 328 | //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 50, 1, 0.8, false),
|
---|
| 329 | };
|
---|
| 330 |
|
---|
| 331 | // skip experiments that are already done
|
---|
| 332 | foreach (var problemFactory in problemFactories) {
|
---|
| 333 | for (int i = 0; i < nReps; i++) {
|
---|
| 334 | {
|
---|
| 335 | var solverSeed = rand.Next();
|
---|
| 336 | var tupel = problemFactory();
|
---|
| 337 | var maxIters = tupel.Item1;
|
---|
| 338 | var maxSize = tupel.Item2;
|
---|
| 339 | var prob = tupel.Item3;
|
---|
| 340 |
|
---|
| 341 | var alg = new SequentialSearch(prob, maxSize, new Random(solverSeed), 0,
|
---|
| 342 | new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericFunctionApproximationGrammarPolicy(prob, true));
|
---|
| 343 |
|
---|
| 344 | int iterations = 0;
|
---|
| 345 | var globalStatistics = new SentenceSetStatistics(prob.BestKnownQuality(maxSize));
|
---|
| 346 | var algName = alg.GetType().Name;
|
---|
| 347 | var probName = prob.GetType().Name;
|
---|
| 348 | alg.SolutionEvaluated += (sentence, quality) => {
|
---|
| 349 | iterations++;
|
---|
| 350 | globalStatistics.AddSentence(sentence, quality);
|
---|
| 351 |
|
---|
| 352 | if (iterations % 1000 == 0) {
|
---|
| 353 | Console.WriteLine("\"{0,25}\" {1} \"{2,25}\" {3}", algName, maxSize, probName, globalStatistics);
|
---|
| 354 | }
|
---|
| 355 | };
|
---|
| 356 |
|
---|
| 357 | alg.Run(maxIters);
|
---|
| 358 |
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[11865] | 365 | private static void RunGpGridTest() {
|
---|
| 366 | const int nReps = 20;
|
---|
[11846] | 367 | const int seed = 31415;
|
---|
[11972] | 368 | //const int maxIters = 50000;
|
---|
[11865] | 369 | var rand = new Random(seed);
|
---|
[11972] | 370 | var problemFactories = new Func<Tuple<int, int, ISymbolicExpressionTreeProblem>>[]
|
---|
[11865] | 371 | {
|
---|
[11972] | 372 | () => Tuple.Create(50000, 32, (ISymbolicExpressionTreeProblem)new PermutationProblem()),
|
---|
| 373 | () => Tuple.Create(50000, 32, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
|
---|
| 374 | () => Tuple.Create(50000, 32,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 375 | () => Tuple.Create(50000, 64, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
|
---|
| 376 | () => Tuple.Create(50000, 64,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 377 | () => Tuple.Create(50000, 128, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
|
---|
| 378 | () => Tuple.Create(50000, 128,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 379 | () => Tuple.Create(50000, 256, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
|
---|
| 380 | () => Tuple.Create(50000, 256,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
|
---|
| 381 | //() => new RoyalPairProblem(),
|
---|
| 382 | //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 0, 1, 0, true),
|
---|
| 383 | //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 0, 1, 0, false),
|
---|
| 384 | //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 50, 1, 0.8, false),
|
---|
[11865] | 385 | };
|
---|
[11972] | 386 |
|
---|
| 387 | foreach (var popSize in new int[] { 100 /*, 250, 500, 1000, 2500, 5000, 10000 */ }) {
|
---|
[11895] | 388 | foreach (var mutationRate in new double[] { /* 0.05, /* 0.10, */ 0.15, /* 0.25, 0.3 */}) {
|
---|
[11972] | 389 | // skip experiments that are already done
|
---|
| 390 | foreach (var problemFactory in problemFactories) {
|
---|
| 391 | for (int i = 0; i < nReps; i++) {
|
---|
| 392 | {
|
---|
| 393 | var solverSeed = rand.Next();
|
---|
| 394 | var tupel = problemFactory();
|
---|
| 395 | var maxIters = tupel.Item1;
|
---|
| 396 | var maxSize = tupel.Item2;
|
---|
| 397 | var prob = tupel.Item3;
|
---|
| 398 | var sgp = new StandardGP(prob, new Random(solverSeed));
|
---|
| 399 | RunGP(sgp, prob, maxIters, popSize, mutationRate, maxSize);
|
---|
| 400 | }
|
---|
| 401 | //{
|
---|
| 402 | // var prob = problemFactory();
|
---|
| 403 | // var osgp = new OffspringSelectionGP(prob, new Random(solverSeed));
|
---|
| 404 | // RunGP(osgp, prob, maxIters, popSize, mutationRate, maxSize);
|
---|
| 405 | //}
|
---|
[11895] | 406 | }
|
---|
[11865] | 407 | }
|
---|
| 408 | }
|
---|
[11972] | 409 |
|
---|
[11865] | 410 | }
|
---|
| 411 | }
|
---|
[11846] | 412 |
|
---|
[11895] | 413 | private static void RunGP(IGPSolver gp, ISymbolicExpressionTreeProblem prob, int maxIters, int popSize, double mutationRate, int maxSize) {
|
---|
[11865] | 414 | int iterations = 0;
|
---|
| 415 | var globalStatistics = new SentenceSetStatistics(prob.BestKnownQuality(maxSize));
|
---|
[11895] | 416 | var gpName = gp.GetType().Name;
|
---|
| 417 | var probName = prob.GetType().Name;
|
---|
[11865] | 418 | gp.SolutionEvaluated += (sentence, quality) => {
|
---|
| 419 | iterations++;
|
---|
| 420 | globalStatistics.AddSentence(sentence, quality);
|
---|
| 421 |
|
---|
[11972] | 422 | if (iterations % 100 == 0) {
|
---|
[11895] | 423 | Console.WriteLine("\"{0,25}\" {1} {2:N2} {3} \"{4,25}\" {5}", gpName, popSize, mutationRate, maxSize, probName, globalStatistics);
|
---|
[11865] | 424 | }
|
---|
[11846] | 425 | };
|
---|
[11865] | 426 |
|
---|
| 427 | gp.PopulationSize = popSize;
|
---|
| 428 | gp.MutationRate = mutationRate;
|
---|
| 429 | gp.MaxSolutionSize = maxSize + 2;
|
---|
| 430 | gp.MaxSolutionDepth = maxSize + 2;
|
---|
| 431 |
|
---|
| 432 | gp.Run(maxIters);
|
---|
| 433 | }
|
---|
[11659] | 434 | }
|
---|
| 435 | }
|
---|