Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs @ 11727

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

#2283: worked on grammatical optimization problem solvers (simple MCTS done)

File size: 4.8 KB
RevLine 
[11659]1using System;
2using System.Collections.Generic;
[11727]3using System.Data;
[11659]4using System.Diagnostics;
5using System.Linq;
6using System.Text;
[11727]7using System.Threading.Tasks;
8using HeuristicLab.Algorithms.Bandits;
[11659]9using HeuristicLab.Algorithms.GrammaticalOptimization;
10using HeuristicLab.Problems.GrammaticalOptimization;
11
12namespace Main {
13  class Program {
14    static void Main(string[] args) {
[11727]15      // RunDemo();
16      RunGridTest();
17    }
18
19    private static void RunGridTest() {
20      int maxIterations = 150000;
21      var globalRandom = new Random(31415);
22      var reps = 10;
23      Parallel.ForEach(new int[] { 1, 5, 10, 100, 500, 1000 }, (randomTries) => {
24        Random localRand;
25        lock (globalRandom) {
26          localRand = new Random(globalRandom.Next());
27        }
28        var policyFactories = new Func<int, IPolicy>[]
29        {
30          (numActions) => new RandomPolicy(localRand, numActions),
31          (numActions) => new UCB1Policy(numActions),
32          (numActions) => new UCB1TunedPolicy(numActions),
33          (numActions) => new UCBNormalPolicy(numActions),
34          (numActions) => new EpsGreedyPolicy(localRand, numActions, 0.01),
35          (numActions) => new EpsGreedyPolicy(localRand, numActions, 0.05),
36          (numActions) => new EpsGreedyPolicy(localRand, numActions, 0.1),
37          (numActions) => new EpsGreedyPolicy(localRand, numActions, 0.2),
38          (numActions) => new EpsGreedyPolicy(localRand, numActions, 0.5),
39          (numActions) => new GaussianThompsonSamplingPolicy(localRand, numActions),
40          (numActions) => new BernoulliThompsonSamplingPolicy(localRand, numActions)
41        };
42
43        foreach (var policyFactory in policyFactories)
44          for (int i = 0; i < reps; i++) {
45            int iterations = 0;
46            var sw = new Stopwatch();
47            var globalStatistics = new SentenceSetStatistics();
48
49            // var problem = new SymbolicRegressionPoly10Problem();
50            var problem = new SantaFeAntProblem();
51            //var problem = new PalindromeProblem();
52            //var problem = new HardPalindromeProblem();
53            //var problem = new RoyalPairProblem();
54            //var problem = new EvenParityProblem();
55            var alg = new MctsSampler(problem, 17, localRand, randomTries, policyFactory);
56            //var alg = new ExhaustiveBreadthFirstSearch(problem, 25);
57            //var alg = new AlternativesContextSampler(problem, 25);
58
59            alg.SolutionEvaluated += (sentence, quality) => {
60              iterations++;
61              globalStatistics.AddSentence(sentence, quality);
62              if (iterations % 10000 == 0) {
63                Console.WriteLine("{0} {1} {2}", randomTries, policyFactory(1), globalStatistics);
64              }
65            };
66
67            sw.Start();
68
69            alg.Run(maxIterations);
70
71            sw.Stop();
72          }
73      });
74    }
75
76    private static void RunDemo() {
77      // TODO: implement threshold ascent
78      // TODO: implement inspection for MCTS
79
[11690]80      int maxIterations = 10000000;
[11659]81      int iterations = 0;
82      var sw = new Stopwatch();
83      double bestQuality = 0;
84      string bestSentence = "";
[11727]85      var globalStatistics = new SentenceSetStatistics();
86      var random = new Random(31415);
[11659]87
[11727]88      // var problem = new SymbolicRegressionPoly10Problem();
89      var problem = new SantaFeAntProblem();
90      //var problem = new PalindromeProblem();
91      //var problem = new HardPalindromeProblem();
92      //var problem = new RoyalPairProblem();
93      //var problem = new EvenParityProblem();
94      var alg = new MctsSampler(problem, 17, random);
95      //var alg = new ExhaustiveBreadthFirstSearch(problem, 25);
96      //var alg = new AlternativesContextSampler(problem, 25);
[11659]97
[11727]98      alg.FoundNewBestSolution += (sentence, quality) => {
[11659]99        bestQuality = quality;
100        bestSentence = sentence;
101        Console.WriteLine("{0,10} {1,10:F5} {2,10:F5} {3}", iterations, bestQuality, quality, sentence);
102      };
[11727]103      alg.SolutionEvaluated += (sentence, quality) => {
[11659]104        iterations++;
[11727]105        globalStatistics.AddSentence(sentence, quality);
[11690]106        if (iterations % 10000 == 0) {
[11727]107          //Console.WriteLine("{0,10} {1,10:F5} {2,10:F5} {3}", iterations, bestQuality, quality, sentence);
108          Console.WriteLine(globalStatistics.ToString());
[11659]109        }
110      };
111
112
113      sw.Start();
114
[11727]115      alg.Run(maxIterations);
[11659]116
117      sw.Stop();
118
119      Console.WriteLine("{0,10} Best soultion: {1,10:F5} {2}", iterations, bestQuality, bestSentence);
120      Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol",
121        sw.Elapsed.TotalSeconds,
122        maxIterations / (double)sw.Elapsed.TotalSeconds,
123        (double)sw.ElapsedMilliseconds * 1000 / maxIterations);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.