Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/Main/Program.cs @ 12667

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

#2283: experiments with q-learning

File size: 6.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Globalization;
5using System.Linq;
6using System.Text.RegularExpressions;
7using HeuristicLab.Algorithms.Bandits.BanditPolicies;
8using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
9using HeuristicLab.Algorithms.GrammaticalOptimization;
10using HeuristicLab.Algorithms.MonteCarloTreeSearch;
11using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
12using HeuristicLab.Problems.GrammaticalOptimization;
13
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
24using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
25
26
27namespace Main {
28  class Program {
29    static void Main(string[] args) {
30      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
31
32      RunDemo();
33    }
34
35
36    private static void RunDemo() {
37
38      for (int i = 0; i < 100; i++) {
39        int maxIterations = 2000000;
40        int iterations = 0;
41
42        var globalStatistics = new SentenceSetStatistics();
43        ResetAlleleStatistics();
44        var random = new Random();
45
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()));
59        var policy = new GenericPolicy(problem);
60        var alg = new SequentialSearch(problem, 23, random, 0,
61          policy);
62        //var alg = new MonteCarloTreeSearch(problem, 23, random, new UCB1Policy(), new RandomSimulation(problem, random, 30));
63
64
65        alg.FoundNewBestSolution += (sentence, quality) => {
66          //Console.WriteLine("{0}", globalStatistics);
67        };
68
69        alg.SolutionEvaluated += (sentence, quality) => {
70          iterations++;
71          globalStatistics.AddSentence(sentence, quality);
72          UpdateAlleleStatistics(sentence);
73          // comment this if you don't want to see solver statistics
74          if (iterations % 100 == 0) {
75            if (iterations % 1000 == 0) {
76              Console.Clear();
77            }
78            Console.SetCursorPosition(0, 0);
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();
87          }
88         
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        };
94
95        var sw = new Stopwatch();
96        sw.Start();
97        alg.Run(maxIterations);
98        sw.Stop();
99
100        Console.WriteLine(globalStatistics);
101
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      }
107    }
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    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.