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