Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/27/15 21:52:10 (9 years ago)
Author:
aballeit
Message:

#2283: implemented MCTS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs

    r12050 r12098  
    44using HeuristicLab.Algorithms.Bandits.BanditPolicies;
    55using HeuristicLab.Algorithms.GrammaticalOptimization;
     6using HeuristicLab.Algorithms.MonteCarloTreeSearch;
     7using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
    68using HeuristicLab.Problems.GrammaticalOptimization;
    79
     
    1820
    1921
    20 namespace Main {
    21   class Program {
    22     static void Main(string[] args) {
    23       CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
     22namespace Main
     23{
     24    class Program
     25    {
     26        static void Main(string[] args)
     27        {
     28            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
    2429
    25       RunDemo();
    26     }
     30            RunDemo();
     31        }
    2732
    2833
    29     private static void RunDemo() {
     34        private static void RunDemo()
     35        {
    3036
    3137
    32       int maxIterations = 100000;
    33       int iterations = 0;
     38            int maxIterations = 100000;
     39            int iterations = 0;
    3440
    35       var globalStatistics = new SentenceSetStatistics();
    36       var random = new Random();
     41            var globalStatistics = new SentenceSetStatistics();
     42            var random = new Random();
    3743
    38       //var problem = new SymbolicRegressionPoly10Problem();
    39       //var problem = new SantaFeAntProblem();             
    40       var problem = new RoyalPairProblem();
    41       //var problem = new EvenParityProblem();
    42       var alg = new SequentialSearch(problem, 23, random, 0,
    43        new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, new UCB1TunedPolicy()));
     44            //var problem = new SymbolicRegressionPoly10Problem();
     45            //var problem = new SantaFeAntProblem();             
     46            var problem = new RoyalPairProblem();
     47            //var problem = new EvenParityProblem();
     48            //var alg = new SequentialSearch(problem, 23, random, 0,
     49            // new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, new UCB1TunedPolicy()));
     50            var alg = new MonteCarloTreeSearch(problem, 23, random, new UCB1Policy(), new RandomSimulation(problem, random, 23));
    4451
    4552
    46       alg.FoundNewBestSolution += (sentence, quality) => {
    47         //Console.WriteLine("{0}", globalStatistics);
    48       };
     53            alg.FoundNewBestSolution += (sentence, quality) =>
     54            {
     55                //Console.WriteLine("{0}", globalStatistics);
     56            };
    4957
    50       alg.SolutionEvaluated += (sentence, quality) => {
    51         iterations++;
    52         globalStatistics.AddSentence(sentence, quality);
     58            alg.SolutionEvaluated += (sentence, quality) =>
     59            {
     60                iterations++;
     61                globalStatistics.AddSentence(sentence, quality);
    5362
    54         // comment this if you don't want to see solver statistics
    55         if (iterations % 100 == 0) {
    56           if (iterations % 10000 == 0) Console.Clear();
    57           Console.SetCursorPosition(0, 0);
    58           alg.PrintStats();
     63                // comment this if you don't want to see solver statistics
     64                if (iterations % 100 == 0)
     65                {
     66                    if (iterations % 10000 == 0) Console.Clear();
     67                    Console.SetCursorPosition(0, 0);
     68                    alg.PrintStats();
     69                }
     70
     71                // uncomment this if you want to collect statistics of the generated sentences
     72                // if (iterations % 1000 == 0) {
     73                //   Console.WriteLine("{0}", globalStatistics);
     74                // }
     75            };
     76
     77            var sw = new Stopwatch();
     78            sw.Start();
     79            alg.Run(maxIterations);
     80            sw.Stop();
     81
     82            Console.Clear();
     83            alg.PrintStats();
     84            Console.WriteLine(globalStatistics);
     85            Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol",
     86              sw.Elapsed.TotalSeconds,
     87              maxIterations / (double)sw.Elapsed.TotalSeconds,
     88              (double)sw.ElapsedMilliseconds * 1000 / maxIterations);
    5989        }
    60 
    61         // uncomment this if you want to collect statistics of the generated sentences
    62         // if (iterations % 1000 == 0) {
    63         //   Console.WriteLine("{0}", globalStatistics);
    64         // }
    65       };
    66 
    67       var sw = new Stopwatch();
    68       sw.Start();
    69       alg.Run(maxIterations);
    70       sw.Stop();
    71 
    72       Console.Clear();
    73       alg.PrintStats();
    74       Console.WriteLine(globalStatistics);
    75       Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol",
    76         sw.Elapsed.TotalSeconds,
    77         maxIterations / (double)sw.Elapsed.TotalSeconds,
    78         (double)sw.ElapsedMilliseconds * 1000 / maxIterations);
    7990    }
    80   }
    8191}
Note: See TracChangeset for help on using the changeset viewer.