Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/Test/RunDemo.cs @ 14052

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

#2283: worked on transformation of arithmetic expressions to canonical form

File size: 8.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Algorithms.Bandits;
7using HeuristicLab.Algorithms.Bandits.BanditPolicies;
8using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
9using HeuristicLab.Algorithms.Bandits.Models;
10using HeuristicLab.Algorithms.GrammaticalOptimization;
11using Microsoft.VisualStudio.TestTools.UnitTesting;
12using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
13
14namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
15  [TestClass]
16  public class RunDemo {
17    [TestMethod]
18    public void RunGridTest() {
19      int maxIterations = 20000; // for poly-10 with 50000 evaluations no successful try with hl yet
20      //var globalRandom = new Random(31415);
21      var localRandSeed = new Random().Next();
22      var reps = 20;
23
24      var policyFactories = new Func<IBanditPolicy>[]
25        {
26         () => new RandomPolicy(),
27          () => new ActiveLearningPolicy(), 
28         () => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"),
29         () => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"),
30         () => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"),
31         () => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"),
32         //() => new GaussianThompsonSamplingPolicy(),
33         () => new GaussianThompsonSamplingPolicy(true),
34         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),
35         () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),
36         //() => new BernoulliThompsonSamplingPolicy(),
37         () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),
38         () => new EpsGreedyPolicy(0.01),
39         () => new EpsGreedyPolicy(0.05),
40         () => new EpsGreedyPolicy(0.1),
41         () => new EpsGreedyPolicy(0.2),
42         () => new EpsGreedyPolicy(0.5),
43         () => new UCTPolicy(0.01),
44         () => new UCTPolicy(0.05),
45         () => new UCTPolicy(0.1),
46         () => new UCTPolicy(0.5),
47         () => new UCTPolicy(1),
48         () => new UCTPolicy(2),
49         () => new UCTPolicy( 5),
50         () => new UCTPolicy( 10),
51         () => new ModifiedUCTPolicy(0.01),
52         () => new ModifiedUCTPolicy(0.05),
53         () => new ModifiedUCTPolicy(0.1),
54         () => new ModifiedUCTPolicy(0.5),
55         () => new ModifiedUCTPolicy(1),
56         () => new ModifiedUCTPolicy(2),
57         () => new ModifiedUCTPolicy( 5),
58         () => new ModifiedUCTPolicy( 10),
59         () => new UCB1Policy(),
60         () => new UCB1TunedPolicy(),
61         () => new UCBNormalPolicy(),
62         () => new BoltzmannExplorationPolicy(1),
63         () => new BoltzmannExplorationPolicy(10),
64         () => new BoltzmannExplorationPolicy(20),
65         () => new BoltzmannExplorationPolicy(100),
66         () => new BoltzmannExplorationPolicy(200),
67         () => new BoltzmannExplorationPolicy(500),
68          () => new ChernoffIntervalEstimationPolicy( 0.01),
69          () => new ChernoffIntervalEstimationPolicy( 0.05),
70          () => new ChernoffIntervalEstimationPolicy( 0.1),
71          () => new ChernoffIntervalEstimationPolicy( 0.2),
72         () => new ThresholdAscentPolicy(5, 0.01),
73         () => new ThresholdAscentPolicy(5, 0.05),
74         () => new ThresholdAscentPolicy(5, 0.1),
75         () => new ThresholdAscentPolicy(5, 0.2),
76         () => new ThresholdAscentPolicy(10, 0.01),
77         () => new ThresholdAscentPolicy(10, 0.05),
78         () => new ThresholdAscentPolicy(10, 0.1),
79         () => new ThresholdAscentPolicy(10, 0.2),
80         () => new ThresholdAscentPolicy(50, 0.01),
81         () => new ThresholdAscentPolicy(50, 0.05),
82         () => new ThresholdAscentPolicy(50, 0.1),
83         () => new ThresholdAscentPolicy(50, 0.2),
84         () => new ThresholdAscentPolicy(100, 0.01),
85         () => new ThresholdAscentPolicy(100, 0.05),
86         () => new ThresholdAscentPolicy(100, 0.1),
87         () => new ThresholdAscentPolicy(100, 0.2),
88         () => new ThresholdAscentPolicy(500, 0.01),
89         () => new ThresholdAscentPolicy(500, 0.05),
90         () => new ThresholdAscentPolicy(500, 0.1),
91         () => new ThresholdAscentPolicy(500, 0.2),
92         () => new ThresholdAscentPolicy(5000, 0.01),
93         () => new ThresholdAscentPolicy(10000, 0.01),
94        };
95
96      var instanceFactories = new Func<Random, Tuple<IProblem, int>>[]
97      {
98        //(rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17),
99        //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:false ), 15),
100        //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:true ), 15),
101        //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:false), 15),
102        //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:true), 15),
103        //(rand) => Tuple.Create((IProblem)new SymbolicRegressionPoly10Problem(), 23)
104        (rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17)
105      };
106
107      foreach (var instanceFactory in instanceFactories) {
108        foreach (var useCanonical in new bool[] { true /*, false */ }) {
109          foreach (var randomTries in new int[] { 0 /*, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) {
110            foreach (var policyFactory in policyFactories) {
111              var myRandomTries = randomTries;
112              var localRand = new Random(localRandSeed);
113              var options = new ParallelOptions();
114              options.MaxDegreeOfParallelism = 1;
115              Parallel.For(0, reps, options, (i) => {
116                Random myLocalRand;
117                lock (localRand)
118                  myLocalRand = new Random(localRand.Next());
119
120                int iterations = 0;
121                var globalStatistics = new SentenceSetStatistics();
122
123                // var problem = new SymbolicRegressionPoly10Problem();
124                // var problem = new SantaFeAntProblem();
125                //var problem = new PalindromeProblem();
126                //var problem = new HardPalindromeProblem();
127                //var problem = new RoyalPairProblem();
128                //var problem = new EvenParityProblem();
129                // var alg = new MctsSampler(problem.Item1, problem.Item2, myLocalRand, myRandomTries, policy());
130                var instance = instanceFactory(myLocalRand);
131                var problem = instance.Item1;
132                var maxLen = instance.Item2;
133                var alg = new SequentialSearch(problem, maxLen, myLocalRand, myRandomTries,
134                  new GenericGrammarPolicy(problem, policyFactory(), useCanonical));
135                // var alg = new SequentialSearch(problem, maxLen, myLocalRand,
136                //   myRandomTries,
137                //   new GenericFunctionApproximationGrammarPolicy(problem,
138                //     useCanonical));
139                //var alg = new ExhaustiveBreadthFirstSearch(problem, 25);
140                //var alg = new AlternativesContextSampler(problem, 25);
141
142                alg.SolutionEvaluated += (sentence, quality) => {
143                  iterations++;
144                  globalStatistics.AddSentence(sentence, quality);
145                  if (iterations % 1000 == 0) {
146                    Console.WriteLine("{0,3} {1,5} \"{2,25}\" {3} {4} {5}", i, myRandomTries, policyFactory(), useCanonical, problem.ToString(), globalStatistics);
147                  }
148                };
149                alg.FoundNewBestSolution += (sentence, quality) => {
150                  //Console.WriteLine("{0,5} {1,25} {2} {3}",
151                  //  myRandomTries, policyFactory(), useCanonical,
152                  //  globalStatistics);
153                };
154
155                alg.Run(maxIterations);
156              });
157            }
158          }
159        }
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.