- Timestamp:
- 01/19/15 20:09:12 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs
r11795 r11799 24 24 CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; 25 25 26 //RunDemo();27 RunGridTest();26 RunDemo(); 27 //RunGridTest(); 28 28 } 29 29 … … 32 32 //var globalRandom = new Random(31415); 33 33 var localRandSeed = 31415; 34 var reps = 5;35 36 var polic ies = new Func<IBanditPolicy>[]34 var reps = 10; 35 36 var policyFactories = new Func<IBanditPolicy>[] 37 37 { 38 38 () => new RandomPolicy(), … … 72 72 () => new ChernoffIntervalEstimationPolicy( 0.1), 73 73 () => new ChernoffIntervalEstimationPolicy( 0.2), 74 () => new ThresholdAscentPolicy(5, 0.01), 75 () => new ThresholdAscentPolicy(5, 0.05), 76 () => new ThresholdAscentPolicy(5, 0.1), 77 () => new ThresholdAscentPolicy(5, 0.2), 74 78 () => new ThresholdAscentPolicy(10, 0.01), 75 79 () => new ThresholdAscentPolicy(10, 0.05), 76 80 () => new ThresholdAscentPolicy(10, 0.1), 77 81 () => new ThresholdAscentPolicy(10, 0.2), 82 () => new ThresholdAscentPolicy(50, 0.01), 83 () => new ThresholdAscentPolicy(50, 0.05), 84 () => new ThresholdAscentPolicy(50, 0.1), 85 () => new ThresholdAscentPolicy(50, 0.2), 78 86 () => new ThresholdAscentPolicy(100, 0.01), 79 87 () => new ThresholdAscentPolicy(100, 0.05), 80 88 () => new ThresholdAscentPolicy(100, 0.1), 81 89 () => new ThresholdAscentPolicy(100, 0.2), 82 () => new ThresholdAscentPolicy(100, 0.01), 83 () => new ThresholdAscentPolicy(100, 0.05), 84 () => new ThresholdAscentPolicy(100, 0.1), 85 () => new ThresholdAscentPolicy(100, 0.2), 86 //() => new ThresholdAscentPolicy(1000, 0.01), 87 //() => new ThresholdAscentPolicy(1000, 0.05), 88 //() => new ThresholdAscentPolicy(1000, 0.1), 89 //() => new ThresholdAscentPolicy(1000, 0.2), 90 () => new ThresholdAscentPolicy(500, 0.01), 91 () => new ThresholdAscentPolicy(500, 0.05), 92 () => new ThresholdAscentPolicy(500, 0.1), 93 () => new ThresholdAscentPolicy(500, 0.2), 90 94 //() => new ThresholdAscentPolicy(5000, 0.01), 91 95 //() => new ThresholdAscentPolicy(10000, 0.01), 92 96 }; 93 97 94 foreach (var problem in new Tuple<IProblem, int>[] 95 { 96 Tuple.Create((IProblem)new SantaFeAntProblem(), 17), 97 Tuple.Create((IProblem)new SymbolicRegressionPoly10Problem(), 23), 98 }) 99 foreach (var useCanonical in new bool[] { true, false }) 100 foreach (var randomTries in new int[] { 0, /*1, 10, /* 5, 100 /*, 500, 1000 */}) { 101 foreach (var policy in policies) { 98 var instanceFactories = new Func<Random, Tuple<IProblem, int>>[] 99 { 100 (rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17), 101 (rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:false ), 15), 102 (rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:true ), 15), 103 (rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:false), 15), 104 (rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:true), 15), 105 (rand) => Tuple.Create((IProblem)new SymbolicRegressionPoly10Problem(), 23) 106 }; 107 108 foreach (var instanceFactory in instanceFactories) { 109 foreach (var useCanonical in new bool[] { true, false }) { 110 foreach (var randomTries in new int[] { 0, 1, 10, /* 5, 100 /*, 500, 1000 */}) { 111 foreach (var policyFactory in policyFactories) { 102 112 var myRandomTries = randomTries; 103 113 var localRand = new Random(localRandSeed); 104 114 var options = new ParallelOptions(); 105 options.MaxDegreeOfParallelism = 1;115 options.MaxDegreeOfParallelism = 4; 106 116 Parallel.For(0, reps, options, (i) => { 107 //var t = Task.Run(() => {108 117 Random myLocalRand; 109 118 lock (localRand) 110 119 myLocalRand = new Random(localRand.Next()); 111 112 //for (int i = 0; i < reps; i++) {113 120 114 121 int iterations = 0; … … 121 128 //var problem = new RoyalPairProblem(); 122 129 //var problem = new EvenParityProblem(); 123 // var alg = new MctsSampler(problem.Item1, problem.Item2, myLocalRand, myRandomTries, policy()); // TODO: Make sure we generate the same random numbers for each 124 var alg = new SequentialSearch(problem.Item1, problem.Item2, myLocalRand, myRandomTries, new GenericGrammarPolicy(problem.Item1, policy(), useCanonical)); 130 // var alg = new MctsSampler(problem.Item1, problem.Item2, myLocalRand, myRandomTries, policy()); 131 var instance = instanceFactory(myLocalRand); 132 var problem = instance.Item1; 133 var maxLen = instance.Item2; 134 var alg = new SequentialSearch(problem, maxLen, myLocalRand, myRandomTries, 135 new GenericGrammarPolicy(problem, policyFactory(), useCanonical)); 125 136 //var alg = new ExhaustiveBreadthFirstSearch(problem, 25); 126 137 //var alg = new AlternativesContextSampler(problem, 25); … … 129 140 iterations++; 130 141 globalStatistics.AddSentence(sentence, quality); 131 if (iterations % 1000 == 0) {132 Console.WriteLine("{0, 5} {1,25} {2} {3}", myRandomTries, policy(), useCanonical, globalStatistics);142 if (iterations % 10000 == 0) { 143 Console.WriteLine("{0,3} {1,5} \"{2,25}\" {3} {4}", i, myRandomTries, policyFactory(), useCanonical, globalStatistics); 133 144 } 134 145 }; 135 146 alg.FoundNewBestSolution += (sentence, quality) => { 136 Console.WriteLine("{0,5} {1,25} {2} {3}", myRandomTries, policy(), useCanonical, globalStatistics); 147 //Console.WriteLine("{0,5} {1,25} {2} {3}", 148 // myRandomTries, policyFactory(), useCanonical, 149 // globalStatistics); 137 150 }; 138 151 139 140 152 alg.Run(maxIterations); 141 142 //Console.WriteLine("{0,5} {1} {2}", randomTries, policyFactory(1), globalStatistics);143 //}144 //});145 //tasks.Add(t);146 153 }); 147 154 } 148 155 } 149 //Task.WaitAll(tasks.ToArray()); 156 } 157 } 150 158 } 151 159 152 160 private static void RunDemo() { 153 // TODO: clone problem for parallel grid test154 161 // TODO: move problem instances into a separate folder 155 // TODO: improve performance of SequentialSearch (memory allocations related to sequences)156 162 // TODO: implement bridge to HL-GP 157 163 // TODO: unify MCTS, TD and ContextMCTS Solvers (stateInfos) … … 183 189 var random = new Random(); 184 190 191 192 var problem = new RoyalSequenceProblem(random, 10, 30, 2, 1, 0); 185 193 //var phraseLen = 3; 186 194 //var numPhrases = 5; 187 195 //var problem = new RoyalPhraseSequenceProblem(random, 15, numPhrases, phraseLen: phraseLen, numCorrectPhrases: 1, correctReward: 1, incorrectReward: 0.0, phrasesAsSets: true); 188 196 189 // var phraseLen = 2;197 // var phraseLen = 3; 190 198 // var numPhrases = 5; 191 // var problem = new FindPhrasesProblem(random, 1 5, numPhrases, phraseLen, numOptimalPhrases: numPhrases, numDecoyPhrases: 0, correctReward: 1.0, decoyReward: 0.0, phrasesAsSets: true);199 // var problem = new FindPhrasesProblem(random, 10, numPhrases, phraseLen, numOptimalPhrases: numPhrases, numDecoyPhrases: 200, correctReward: 1.0, decoyReward: 0.5, phrasesAsSets: true); 192 200 193 201 // good results for symb-reg … … 197 205 // - GenericThompsonSamplingPolicy("") 198 206 // - UCTPolicy(0.10) (5 of 5 runs, 35000 iters avg.) 199 207 200 208 // good results for artificial ant: 201 209 // prev results: … … 203 211 // - GaussianModelWithUnknownVariance (and Q= 0.99-quantil) also works well for Ant 204 212 // 2015 01 19: grid test with canonical states (non-canonical slightly worse) 205 // - Threshold Ascent (best 100, 0.01; all variants relatively good 206 207 //var problem = new SymbolicRegressionPoly10Problem(); 208 209 var problem = new SantaFeAntProblem(); 213 // - Threshold Ascent (best 100, 0.01; all variants relatively good) 214 // - Policies where the variance has a large weight compared to the mean? (Gaussian(compatible), Gaussian with fixed variance, UCT with large c, alle TA) 215 216 //var problem = new SymbolicRegressionPoly10Problem(); 217 218 //var problem = new SantaFeAntProblem(); 210 219 //var problem = new SymbolicRegressionProblem("Tower"); 211 220 //var problem = new PalindromeProblem(); … … 216 225 //var alg = new MctsSampler(problem, 23, random, 0, new BoltzmannExplorationPolicy(100)); 217 226 //var alg = new MctsSampler(problem, 23, random, 0, new EpsGreedyPolicy(0.1)); 218 var alg = new SequentialSearch(problem, 17, random, 0,219 new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, new BoltzmannExplorationPolicy(10), true));227 var alg = new SequentialSearch(problem, 30, random, 0, 228 new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, new EpsGreedyPolicy(0.1), true)); 220 229 //var alg = new MctsQLearningSampler(problem, sentenceLen, random, 0, null); 221 230 //var alg = new MctsQLearningSampler(problem, 30, random, 0, new EpsGreedyPolicy(0.2)); … … 236 245 iterations++; 237 246 globalStatistics.AddSentence(sentence, quality); 238 if (iterations % 100 == 0) {247 if (iterations % 1000 == 0) { 239 248 if (iterations % 1000 == 0) Console.Clear(); 240 249 Console.SetCursorPosition(0, 0);
Note: See TracChangeset
for help on using the changeset viewer.