Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12876 was 12876, checked in by gkronber, 8 years ago

#2283: implemented first crude version of extreme hunter algorithm in branch

File size: 6.5 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 policy = new GenericGrammarPolicy(problem, new ExtremeHunterPolicy());
61        //var policy = new GenericGrammarPolicy(problem, new UCB1Policy());
62        //var policy = new GenericGrammarPolicy(problem, new ActiveLearningPolicy(0.1));
63        var policy = new GenericGrammarPolicy(problem, new ExtremeHunterPolicy(1.0E-2, 1E-2, 1));
64        var alg = new SequentialSearch(problem, 23, random, 0,
65          policy);
66        //var alg = new MonteCarloTreeSearch(problem, 23, random, new UCB1Policy(), new RandomSimulation(problem, random, 30));
67
68
69        alg.FoundNewBestSolution += (sentence, quality) => {
70          //Console.WriteLine("{0}", globalStatistics);
71        };
72
73        alg.SolutionEvaluated += (sentence, quality) => {
74          iterations++;
75          globalStatistics.AddSentence(sentence, quality);
76          UpdateAlleleStatistics(sentence);
77          // comment this if you don't want to see solver statistics
78          if (iterations % 100 == 0) {
79            if (iterations % 1000 == 0) {
80              Console.Clear();
81            }
82            Console.SetCursorPosition(0, 0);
83            Console.WriteLine(iterations);
84            //WriteAlleleStatistics();
85            Console.WriteLine(globalStatistics.BestSentenceQuality);
86            Console.WriteLine(globalStatistics.BestSentence);
87            Console.WriteLine(globalStatistics);
88            alg.PrintStats();
89            //policy.PrintStats();
90            //ResetAlleleStatistics();
91          }
92         
93          // uncomment this if you want to collect statistics of the generated sentences
94          //if (iterations % 100 == 0) {
95          //  Console.WriteLine("{0}", globalStatistics);
96          //}
97        };
98
99        var sw = new Stopwatch();
100        sw.Start();
101        alg.Run(maxIterations);
102        sw.Stop();
103
104        Console.WriteLine(globalStatistics);
105
106        Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol",
107          sw.Elapsed.TotalSeconds,
108          maxIterations / (double)sw.Elapsed.TotalSeconds,
109          (double)sw.ElapsedMilliseconds * 1000 / maxIterations);
110      }
111    }
112
113    private static void UpdateAlleleStatistics(string sentence) {
114      for (int i = 0; i < sentence.Length; i++) {
115        var allele = sentence.Substring(i, 1);
116        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
117      }
118      for (int i = 0; i < sentence.Length - 2; i+=2) {
119        var allele = sentence.Substring(i, 3);
120        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
121      }
122      for (int i = 0; i < sentence.Length - 4; i+=2) {
123        var allele = sentence.Substring(i, 5);
124        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
125      }
126    }
127
128
129    private static Dictionary<string, int> alleleStatistics;
130
131    private static void ResetAlleleStatistics() {
132      alleleStatistics = new Dictionary<string, int>()
133      {
134        {"a", 0},
135        {"b", 0},
136        {"c", 0},
137        {"d", 0},
138        {"e", 0},
139        {"f", 0},
140        {"g", 0},
141        {"h", 0},
142        {"i", 0},
143        {"j", 0},
144        {"a*b", 0},
145        {"b*a", 0},
146        {"c*d", 0},
147        {"d*c", 0},
148        {"e*f", 0},
149        {"f*e", 0},
150        {"a*g*i", 0},
151        {"a*i*g", 0},
152        {"g*a*i", 0},
153        {"g*i*a", 0},
154        {"i*g*a", 0},
155        {"i*a*g", 0},
156        {"j*c*f", 0},
157        {"j*f*c", 0},
158        {"c*j*f", 0},
159        {"c*f*j", 0},
160        {"f*c*j", 0},
161        {"f*j*c", 0}
162      };
163    }
164
165
166    private static void WriteAlleleStatistics() {
167      double count = alleleStatistics.Sum(e => e.Value);
168      foreach (var entry in alleleStatistics.OrderByDescending(e=>e.Value)) {
169        Console.WriteLine("{0,-10} {1,-10}", entry.Key, entry.Value);
170      }
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.