Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12921 was 12893, checked in by gkronber, 9 years ago

#2283: experiments on grammatical optimization algorithms (maxreward instead of avg reward, ...)

File size: 8.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Globalization;
5using System.Linq;
6using HeuristicLab.Algorithms.Bandits;
7using HeuristicLab.Algorithms.Bandits.BanditPolicies;
8using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
9using HeuristicLab.Algorithms.GrammaticalOptimization;
10using HeuristicLab.Problems.GrammaticalOptimization;
11
12// NOTES: gkronber
13// TODO: feature extraction for full symbolic expressions and experiment for all benchmark problems
14// TODO: why does GaussianThompsonSampling work so well with MCTS for the artificial ant problem?
15// TODO: research thompson sampling for max bandit?
16// TODO: verify TA implementation using example from the original paper     
17// TODO: implement thompson sampling for gaussian mixture models
18// TODO: gleichzeitige modellierung von transformierter zielvariable (y, 1/y, log(y), exp(y), sqrt(y), ...)
19// TODO: vergleich bei complete-randomly möglichst kurze sÀtze generieren vs. einfach zufÀllig alternativen wÀhlen
20// TODO: reward discounting (fÌr verÀnderliche reward distributions Ìber zeit). speziellen unit-test dafÌr erstellen
21// TODO: constant optimization
22using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
23using RandomPolicy = HeuristicLab.Algorithms.Bandits.GrammarPolicies.RandomPolicy;
24
25
26namespace Main {
27  class Program {
28    static void Main(string[] args) {
29      CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
30
31      foreach (var banditPolicy in new IBanditPolicy[]
32      {
33        //new HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy(),
34        //new UCB1TunedPolicy(),
35        //new UCB1Policy(),
36        //new UCB1Policy(0.8),
37        //new UCB1Policy(1),
38        new UCB1Policy(0.5),
39        //new ExtremeHunterPolicy(),
40        //new ThresholdAscentPolicy(),
41        //new BoltzmannExplorationPolicy(1),
42        //new BoltzmannExplorationPolicy(0.5),
43        //new BoltzmannExplorationPolicy(5),
44        //new EpsGreedyPolicy(0.1),
45        //new EpsGreedyPolicy(0.05),
46      }) {
47        var problem = new SymbolicRegressionPoly10Problem(500);
48        var random = new Random();
49        //var problem = new SymbolicRegressionProblem(random, "Vladislavleva-1", useConstantOpt: true);
50        //var problem = new PrimePolynomialProblem();
51        //var problem = new SantaFeAntProblem();
52        var policy = new GenericGrammarPolicy(problem, banditPolicy);
53        // var policy = new GenericGrammarPolicy(problem, new UCB1Policy(0.5));
54        //var alg = new MonteCarloTreeSearch(problem, 23, random, new UCB1Policy(), new RandomSimulation(problem, random, 30));
55
56        RunDemo(problem, random, policy, banditPolicy.ToString());
57      }
58    }
59
60
61    private static void RunDemo(IProblem problem, Random random, GenericGrammarPolicy policy, string banditPolicyName) {
62
63
64      for (int i = 0; i < 100; i++) {
65        int iterations = 0;
66        var globalStatistics = new SentenceSetStatistics();
67
68        var alg = new SequentialSearch(problem, 23, random, 0, policy);
69
70
71        alg.FoundNewBestSolution += (sentence, quality) => {
72          //Console.WriteLine("{0}", globalStatistics);
73        };
74
75        alg.SolutionEvaluated += (sentence, quality) => {
76          iterations++;
77          globalStatistics.AddSentence(sentence, quality);
78          //UpdateAlleleStatistics(sentence);
79          // comment this if you don't want to see solver statistics
80          if (iterations % 100 == 0) {
81            if (iterations % 1000 == 0) {
82              Console.Clear();
83            }
84            Console.SetCursorPosition(0, 0);
85            Console.WriteLine("{0} {1}", iterations, string.Join(" ", policy.OptimalPulls.Take(15).Select(p => string.Format("{0:F3}", p))));
86            //WriteAlleleStatistics();
87            Console.WriteLine(globalStatistics.BestSentenceQuality);
88            Console.WriteLine(globalStatistics.BestSentence);
89            Console.WriteLine(globalStatistics);
90            alg.PrintStats();
91            //policy.PrintStats();
92            //ResetAlleleStatistics();
93          }
94
95          // uncomment this if you want to collect statistics of the generated sentences
96          //if (iterations % 1000 == 0) {
97          //  Console.WriteLine("{0} {1} {2}", banditPolicyName, string.Join(" ", policy.OptimalPulls.Take(15).Select(p => string.Format("{0:F3}", p))), globalStatistics);
98          //}
99        };
100        int maxIterations = 300000;
101
102        // ResetAlleleStatistics();
103
104        //var problem = new SantaFeAntProblem();
105        //var problem = new RoyalPairProblem(10);
106        //var problem = new FindPhrasesProblem(random, 10, 5, 3, 5, 5, 1.0, 0.9, true);
107        //var problem = new PrimePolynomialProblem();
108        //var problem = new SymbolicRegressionProblem(random, "Tower");
109        // @"C:\reps\HeuristicLab\branches\HeuristicLab.Problems.GrammaticalOptimization\HeuristicLab.Problems.GrammaticalOptimization.SymbReg\nht-train.csv",
110        //  @"C:\reps\fhooe-new\research\Datasets\Benchmark\kommenda-1.csv",
111        //  1.0,
112        //  true);
113        // //var problem = new PrimePolynomialProblem();
114        // var alg = new SequentialSearch(problem, 25, random, 0,
115        //var policy = new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, new UCB1TunedPolicy());
116        //var policy = new GenericPolicy(problem);
117        //var policy = new GenericGrammarPolicy(problem, new ExtremeHunterPolicy());
118        //var policy = new GenericGrammarPolicy(problem, new UCB1Policy(0.5));
119        //var policy = new GenericGrammarPolicy(problem, new ActiveLearningPolicy(3));
120        //var policy = new GenericGrammarPolicy(problem, new IntervalEstimationPolicy());
121        //var policy = new GenericGrammarPolicy(problem, new ChernoffIntervalEstimationPolicy());
122
123        //var policy = new GenericGrammarPolicy(problem, new EpsGreedyPolicy(0.1));
124        //var policy = new GenericGrammarPolicy(problem, new ExtremeHunterPolicy(0.001, 0.001, 1, 100000, minPulls: 100));
125        //var policy = new GenericGrammarPolicy(problem, new ThresholdAscentPolicy(s: 1000, delta: 1));
126        //var policy = new GenericGrammarPolicy(problem, new HeuristicLab.Algorithms.Bandits.BanditPolicies.UCB1TunedPolicy());
127        //var policy = new GenericGrammarPolicy(problem, new HeuristicLab.Algorithms.Bandits.BanditPolicies.BoltzmannExplorationPolicy(1));
128        //var policy = new GenericGrammarPolicy(problem, new HeuristicLab.Algorithms.Bandits.BanditPolicies.ThresholdAscentPolicy(500, 0.01)); // santa fe ant
129
130
131        //var policy = new GenericGrammarPolicy(problem, new HeuristicLab.Algorithms.Bandits.BanditPolicies.BoltzmannExplorationWithCoolingPolicy(0.01));
132
133
134
135
136        var sw = new Stopwatch();
137        sw.Start();
138        alg.Run(maxIterations);
139        sw.Stop();
140
141        //Console.WriteLine(globalStatistics);
142        //
143        //Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol",
144        //  sw.Elapsed.TotalSeconds,
145        //  maxIterations / (double)sw.Elapsed.TotalSeconds,
146        //  (double)sw.ElapsedMilliseconds * 1000 / maxIterations);
147      }
148    }
149
150    private static void UpdateAlleleStatistics(string sentence) {
151      for (int i = 0; i < sentence.Length; i++) {
152        var allele = sentence.Substring(i, 1);
153        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
154      }
155      for (int i = 0; i < sentence.Length - 2; i += 2) {
156        var allele = sentence.Substring(i, 3);
157        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
158      }
159      for (int i = 0; i < sentence.Length - 4; i += 2) {
160        var allele = sentence.Substring(i, 5);
161        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
162      }
163    }
164
165
166    private static Dictionary<string, int> alleleStatistics;
167
168    private static void ResetAlleleStatistics() {
169      alleleStatistics = new Dictionary<string, int>()
170      {
171        {"a", 0},
172        {"b", 0},
173        {"c", 0},
174        {"d", 0},
175        {"e", 0},
176        {"f", 0},
177        {"g", 0},
178        {"h", 0},
179        {"i", 0},
180        {"j", 0},
181        {"a*b", 0},
182        {"b*a", 0},
183        {"c*d", 0},
184        {"d*c", 0},
185        {"e*f", 0},
186        {"f*e", 0},
187        {"a*g*i", 0},
188        {"a*i*g", 0},
189        {"g*a*i", 0},
190        {"g*i*a", 0},
191        {"i*g*a", 0},
192        {"i*a*g", 0},
193        {"j*c*f", 0},
194        {"j*f*c", 0},
195        {"c*j*f", 0},
196        {"c*f*j", 0},
197        {"f*c*j", 0},
198        {"f*j*c", 0}
199      };
200    }
201
202
203    private static void WriteAlleleStatistics() {
204      double count = alleleStatistics.Sum(e => e.Value);
205      foreach (var entry in alleleStatistics.OrderByDescending(e => e.Value)) {
206        Console.WriteLine("{0,-10} {1,-10}", entry.Key, entry.Value);
207      }
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.