[11795] | 1 | using System;
|
---|
[12298] | 2 | using System.Collections.Generic;
|
---|
[11659] | 3 | using System.Diagnostics;
|
---|
[11730] | 4 | using System.Globalization;
|
---|
[12298] | 5 | using System.Linq;
|
---|
| 6 | using System.Text.RegularExpressions;
|
---|
[11742] | 7 | using HeuristicLab.Algorithms.Bandits.BanditPolicies;
|
---|
[12290] | 8 | using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
|
---|
[11659] | 9 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
[12098] | 10 | using HeuristicLab.Algorithms.MonteCarloTreeSearch;
|
---|
| 11 | using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
|
---|
[11659] | 12 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 13 |
|
---|
[11981] | 14 | // NOTES: gkronber
|
---|
| 15 | // TODO: feature extraction for full symbolic expressions and experiment for all benchmark problems
|
---|
| 16 | // TODO: why does GaussianThompsonSampling work so well with MCTS for the artificial ant problem?
|
---|
| 17 | // TODO: research thompson sampling for max bandit?
|
---|
| 18 | // TODO: verify TA implementation using example from the original paper
|
---|
| 19 | // TODO: implement thompson sampling for gaussian mixture models
|
---|
| 20 | // TODO: gleichzeitige modellierung von transformierter zielvariable (y, 1/y, log(y), exp(y), sqrt(y), ...)
|
---|
| 21 | // TODO: vergleich bei complete-randomly möglichst kurze sÀtze generieren vs. einfach zufÀllig alternativen wÀhlen
|
---|
| 22 | // TODO: reward discounting (fÌr verÀnderliche reward distributions Ìber zeit). speziellen unit-test dafÌr erstellen
|
---|
| 23 | // TODO: constant optimization
|
---|
[12290] | 24 | using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
|
---|
[11981] | 25 |
|
---|
| 26 |
|
---|
[12290] | 27 | namespace Main {
|
---|
| 28 | class Program {
|
---|
| 29 | static void Main(string[] args) {
|
---|
| 30 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
[11730] | 31 |
|
---|
[12290] | 32 | RunDemo();
|
---|
| 33 | }
|
---|
[11727] | 34 |
|
---|
[11730] | 35 |
|
---|
[12290] | 36 | private static void RunDemo() {
|
---|
[11727] | 37 |
|
---|
[12290] | 38 | for (int i = 0; i < 100; i++) {
|
---|
| 39 | int maxIterations = 2000000;
|
---|
| 40 | int iterations = 0;
|
---|
[11730] | 41 |
|
---|
[12290] | 42 | var globalStatistics = new SentenceSetStatistics();
|
---|
[12298] | 43 | ResetAlleleStatistics();
|
---|
[12290] | 44 | var random = new Random();
|
---|
[11770] | 45 |
|
---|
[12290] | 46 | var problem = new SymbolicRegressionPoly10Problem();
|
---|
| 47 | //var problem = new SantaFeAntProblem();
|
---|
| 48 | //var problem = new RoyalPairProblem(25);
|
---|
| 49 | //var problem = new FindPhrasesProblem(random, 10, 5, 3, 5, 5, 1.0, 0.9, true);
|
---|
| 50 | //var problem = new PrimePolynomialProblem();
|
---|
| 51 | //var problem = new SymbolicRegressionProblem(random,
|
---|
| 52 | // //@"C:\reps\HeuristicLab\branches\HeuristicLab.Problems.GrammaticalOptimization\HeuristicLab.Problems.GrammaticalOptimization.SymbReg\nht-train.csv",
|
---|
| 53 | // @"C:\reps\fhooe-new\research\Datasets\Benchmark\kommenda-1.csv",
|
---|
| 54 | // 1.0,
|
---|
| 55 | // true);
|
---|
| 56 | // //var problem = new PrimePolynomialProblem();
|
---|
| 57 | // var alg = new SequentialSearch(problem, 25, random, 0,
|
---|
| 58 | // new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, new UCB1TunedPolicy()));
|
---|
[12294] | 59 | var policy = new GenericPolicy(problem);
|
---|
[12295] | 60 | var alg = new SequentialSearch(problem, 23, random, 0,
|
---|
[12294] | 61 | policy);
|
---|
[12290] | 62 | //var alg = new MonteCarloTreeSearch(problem, 23, random, new UCB1Policy(), new RandomSimulation(problem, random, 30));
|
---|
[11659] | 63 |
|
---|
| 64 |
|
---|
[12290] | 65 | alg.FoundNewBestSolution += (sentence, quality) => {
|
---|
| 66 | //Console.WriteLine("{0}", globalStatistics);
|
---|
| 67 | };
|
---|
[11981] | 68 |
|
---|
[12290] | 69 | alg.SolutionEvaluated += (sentence, quality) => {
|
---|
| 70 | iterations++;
|
---|
| 71 | globalStatistics.AddSentence(sentence, quality);
|
---|
[12298] | 72 | UpdateAlleleStatistics(sentence);
|
---|
[12290] | 73 | // comment this if you don't want to see solver statistics
|
---|
| 74 | if (iterations % 100 == 0) {
|
---|
[12298] | 75 | if (iterations % 1000 == 0) {
|
---|
| 76 | Console.Clear();
|
---|
| 77 | }
|
---|
[12290] | 78 | Console.SetCursorPosition(0, 0);
|
---|
[12298] | 79 | Console.WriteLine(iterations);
|
---|
| 80 | WriteAlleleStatistics();
|
---|
| 81 | Console.WriteLine(globalStatistics.BestSentenceQuality);
|
---|
| 82 | Console.WriteLine(globalStatistics.BestSentence);
|
---|
| 83 | Console.WriteLine(globalStatistics);
|
---|
| 84 | //alg.PrintStats();
|
---|
| 85 | policy.PrintStats();
|
---|
| 86 | //ResetAlleleStatistics();
|
---|
[12290] | 87 | }
|
---|
[12298] | 88 |
|
---|
[12290] | 89 | // uncomment this if you want to collect statistics of the generated sentences
|
---|
| 90 | //if (iterations % 100 == 0) {
|
---|
| 91 | // Console.WriteLine("{0}", globalStatistics);
|
---|
| 92 | //}
|
---|
| 93 | };
|
---|
[11981] | 94 |
|
---|
[12290] | 95 | var sw = new Stopwatch();
|
---|
| 96 | sw.Start();
|
---|
| 97 | alg.Run(maxIterations);
|
---|
| 98 | sw.Stop();
|
---|
[11659] | 99 |
|
---|
[12290] | 100 | Console.WriteLine(globalStatistics);
|
---|
[11659] | 101 |
|
---|
[12290] | 102 | Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol",
|
---|
| 103 | sw.Elapsed.TotalSeconds,
|
---|
| 104 | maxIterations / (double)sw.Elapsed.TotalSeconds,
|
---|
| 105 | (double)sw.ElapsedMilliseconds * 1000 / maxIterations);
|
---|
| 106 | }
|
---|
[11659] | 107 | }
|
---|
[12298] | 108 |
|
---|
| 109 | private static void UpdateAlleleStatistics(string sentence) {
|
---|
| 110 | for (int i = 0; i < sentence.Length; i++) {
|
---|
| 111 | var allele = sentence.Substring(i, 1);
|
---|
| 112 | if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
|
---|
| 113 | }
|
---|
| 114 | for (int i = 0; i < sentence.Length - 2; i+=2) {
|
---|
| 115 | var allele = sentence.Substring(i, 3);
|
---|
| 116 | if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
|
---|
| 117 | }
|
---|
| 118 | for (int i = 0; i < sentence.Length - 4; i+=2) {
|
---|
| 119 | var allele = sentence.Substring(i, 5);
|
---|
| 120 | if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | private static Dictionary<string, int> alleleStatistics;
|
---|
| 126 |
|
---|
| 127 | private static void ResetAlleleStatistics() {
|
---|
| 128 | alleleStatistics = new Dictionary<string, int>()
|
---|
| 129 | {
|
---|
| 130 | {"a", 0},
|
---|
| 131 | {"b", 0},
|
---|
| 132 | {"c", 0},
|
---|
| 133 | {"d", 0},
|
---|
| 134 | {"e", 0},
|
---|
| 135 | {"f", 0},
|
---|
| 136 | {"g", 0},
|
---|
| 137 | {"h", 0},
|
---|
| 138 | {"i", 0},
|
---|
| 139 | {"j", 0},
|
---|
| 140 | {"a*b", 0},
|
---|
| 141 | {"b*a", 0},
|
---|
| 142 | {"c*d", 0},
|
---|
| 143 | {"d*c", 0},
|
---|
| 144 | {"e*f", 0},
|
---|
| 145 | {"f*e", 0},
|
---|
| 146 | {"a*g*i", 0},
|
---|
| 147 | {"a*i*g", 0},
|
---|
| 148 | {"g*a*i", 0},
|
---|
| 149 | {"g*i*a", 0},
|
---|
| 150 | {"i*g*a", 0},
|
---|
| 151 | {"i*a*g", 0},
|
---|
| 152 | {"j*c*f", 0},
|
---|
| 153 | {"j*f*c", 0},
|
---|
| 154 | {"c*j*f", 0},
|
---|
| 155 | {"c*f*j", 0},
|
---|
| 156 | {"f*c*j", 0},
|
---|
| 157 | {"f*j*c", 0}
|
---|
| 158 | };
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | private static void WriteAlleleStatistics() {
|
---|
| 163 | double count = alleleStatistics.Sum(e => e.Value);
|
---|
| 164 | foreach (var entry in alleleStatistics.OrderByDescending(e=>e.Value)) {
|
---|
| 165 | Console.WriteLine("{0,-10} {1,-10}", entry.Key, entry.Value);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
[12290] | 168 | }
|
---|
[11659] | 169 | }
|
---|