Changeset 11976
- Timestamp:
- 02/11/15 02:22:18 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/Policies/ThresholdAscentPolicy.cs
r11806 r11976 31 31 public double Value { 32 32 get { 33 if (Tries == 0.0) return 0.0;33 if (Tries == 0.0) return double.PositiveInfinity; 34 34 return rewardHistogram[thresholdBin] / (double)Tries; 35 35 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/SequentialDecisionPolicies/GenericFunctionApproximationGrammarPolicy.cs
r11974 r11976 14 14 public sealed class GenericFunctionApproximationGrammarPolicy : IGrammarPolicy { 15 15 private Dictionary<string, double> featureWeigths; // stores the necessary information for bandit policies for each state (=canonical phrase) 16 private Dictionary<string, int> featureTries; 16 17 private HashSet<string> done; 17 18 private readonly bool useCanonicalPhrases; 18 19 private readonly IProblem problem; 20 19 21 20 22 … … 23 25 this.problem = problem; 24 26 this.featureWeigths = new Dictionary<string, double>(); 27 this.featureTries = new Dictionary<string, int>(); 25 28 this.done = new HashSet<string>(); 26 29 } … … 57 60 originalIdx++; 58 61 } 59 60 const double beta = 20.0; 61 var w = from q in activeAfterStates 62 select Math.Exp(beta * q); 62 63 64 /* 65 const double beta = 1; 66 var w = from idx in Enumerable.Range(0, maxIdx) 67 let afterStateQ = activeAfterStates[idx] 68 select Math.Exp(beta * afterStateQ); 63 69 64 70 var bestAction = Enumerable.Range(0, maxIdx).SampleProportional(random, w); 65 71 selectedStateIdx = actionIndexMap[bestAction]; 66 72 Debug.Assert(selectedStateIdx >= 0); 67 68 /* 73 */ 74 75 69 76 if (random.NextDouble() < 0.2) { 70 77 selectedStateIdx = actionIndexMap[random.Next(maxIdx)]; … … 84 91 selectedStateIdx = actionIndexMap[bestIdxs[random.Next(bestIdxs.Count)]]; 85 92 } 86 */ 93 87 94 88 95 … … 114 121 115 122 public int GetTries(string state) { 116 return 1; 123 return 0; 124 } 125 126 public int GetFeatureTries(string featureId) { 127 int t; 128 if (featureTries.TryGetValue(featureId, out t)) { 129 return t; 130 } else return 0; 117 131 } 118 132 119 133 public double GetValue(string state) { 120 return problem.GetFeatures(state). Sum(feature => GetWeight(feature));134 return problem.GetFeatures(state).Average(feature => GetWeight(feature)); 121 135 } 122 136 … … 124 138 double w; 125 139 if (featureWeigths.TryGetValue(feature.Id, out w)) return w * feature.Value; 126 else return 0.0; // TODO: alternatives?140 else return 0.0; 127 141 } 128 142 private void UpdateWeights(string state, double reward) { 129 const double alpha = 0.01;130 143 double delta = reward - GetValue(state); 144 delta /= problem.GetFeatures(state).Count(); 145 const double alpha = 0.001; 131 146 foreach (var feature in problem.GetFeatures(state)) { 147 featureTries[feature.Id] = GetFeatureTries(feature.Id) + 1; 148 Debug.Assert(GetFeatureTries(feature.Id) >= 1); 149 //double alpha = 1.0 / GetFeatureTries(feature.Id); 150 //alpha = Math.Max(alpha, 0.01); 151 132 152 double w; 133 153 if (!featureWeigths.TryGetValue(feature.Id, out w)) { 134 featureWeigths[feature.Id] = alpha * delta ;154 featureWeigths[feature.Id] = alpha * delta * feature.Value; 135 155 } else { 136 featureWeigths[feature.Id] += alpha * delta ;156 featureWeigths[feature.Id] += alpha * delta * feature.Value; 137 157 } 138 158 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers/SequentialSearch.cs
r11850 r11976 166 166 private void DistributeReward(double reward) { 167 167 behaviourPolicy.UpdateReward(stateChain, reward); 168 greedyPolicy.UpdateReward(stateChain, reward);168 //greedyPolicy.UpdateReward(stateChain, reward); 169 169 } 170 170 -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Distributions/GaussianModel.cs
r11851 r11976 38 38 this.meanPriorMu = meanPriorMu; 39 39 this.meanPriorVariance = meanPriorVariance; 40 40 41 41 this.knownVariance = false; 42 42 this.precisionPriorAlpha = precisionPriorAlpha; -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Feature.cs
r11832 r11976 15 15 this.Value = val; 16 16 } 17 18 public override string ToString() { 19 return string.Format("{0} {1:N3}", Id, Value); 20 } 17 21 } 18 22 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs
r11974 r11976 54 54 } 55 55 56 private void Run(Ant ant, string sentence, ref int p) {56 private static void Run(Ant ant, string sentence, ref int p, bool stopAfterFirst = false) { 57 57 while (!ant.Done()) { 58 if (p >= sentence.Length) p = 0; // restart 58 if (p >= sentence.Length) { 59 if (stopAfterFirst) return; 60 p = 0; // restart 61 } 59 62 switch (sentence[p]) { 60 63 case 'r': { … … 93 96 break; 94 97 } 98 case '.': { 99 // nop 100 p++; 101 ant.Nop(); 102 break; 103 } 95 104 case ')': { 96 105 p++; // skip … … 104 113 } 105 114 106 private void Skip(string sentence, ref int p) {115 private static void Skip(string sentence, ref int p) { 107 116 int openCount = 1; 108 117 while (openCount > 0) { … … 114 123 115 124 public string CanonicalRepresentation(string phrase) { 125 phrase = phrase.Replace("A", "."); 116 126 var sb = new StringBuilder(phrase); 117 127 string canonicalPhrase = phrase; … … 119 129 do { 120 130 oldPhrase = canonicalPhrase; 121 sb.Replace("ll", "rr").Replace("rl", "lr").Replace("lr", "").Replace("lll", "r").Replace("rrr", "l"); 131 sb.Replace("ll", "rr").Replace("rl", "").Replace("lr", "").Replace("lll", "r").Replace("rrr", "l"); 132 sb.Replace("?(m)(m)", "?()()m").Replace("?(l)(l)", "?()()l").Replace("?(r)(r)", "?()()r").Replace("?()()", ""); 122 133 canonicalPhrase = sb.ToString(); 123 134 } while (canonicalPhrase != oldPhrase); … … 126 137 127 138 public IEnumerable<Feature> GetFeatures(string phrase) { 128 yield return new Feature(CanonicalRepresentation(phrase), 1.0); 139 phrase = CanonicalRepresentation(phrase); 140 var isTerminal = grammar.IsTerminal(phrase); 141 142 143 //yield return new Feature("const", 0.0); 144 //if (phrase.Length > 0) { 145 // var ant = new Ant(recordTrail: true); 146 // int pos = 0; 147 // Run(ant, phrase, ref pos, true); 148 // //yield return new Feature("food", ant.FoodEaten); 149 // yield return new Feature(ant.Trail, 1.0); 150 //} 151 //yield return new Feature(isTerminal + "const", 0.0); 152 //yield return new Feature(isTerminal.ToString() + phrase.Length, 1.0); 153 //int ntIdx; 154 //for (ntIdx = 0; ntIdx < phrase.Length; ntIdx++) if (grammar.IsNonTerminal(phrase[ntIdx])) break; 155 //for (int l = ntIdx-2; l >= 0; l--) { 156 // yield return new Feature(phrase.Substring(l, ntIdx-l-1), 1.0); 157 //} 158 // 159 ////yield return new Feature("$" + phrase[0], 1.0); 160 // if (!isTerminal) { 161 // for (int i = 4; i < phrase.Length; i++) { 162 // if (grammar.IsNonTerminal(phrase[i])) { 163 // yield return new Feature(phrase[i - 4].ToString() + phrase[i - 3].ToString() + phrase[i - 2] + phrase[i - 1], 0.1); 164 // break; 165 // } 166 // } 167 // } 168 // var d = 0; 169 // var ls = 0; 170 // var rs = 0; 171 // var qs = 0; 172 // foreach (var ch in phrase) if (ch == 'l') { d--; ls++; } else if (ch == 'r') { d++; rs++; } else if (ch == '?') qs++; 173 // yield return new Feature(isTerminal + "D" + Math.Abs(d), 1.0); 174 // yield return new Feature(isTerminal + "R" + rs, 1.0); 175 // yield return new Feature(isTerminal + "L" + ls, 1.0); 176 // yield return new Feature(isTerminal + "?" + qs, 1.0); 177 178 yield return new Feature(phrase, 1.0); 179 //for (int i = 0; i < phrase.Length; i++) 180 // yield return new Feature(i.ToString() + phrase[i].ToString(), 1.0); 129 181 // yield return new Feature("Length", phrase.Length); // 130 182 // foreach (var pair in phrase.Zip(phrase.Skip(1), Tuple.Create)) { … … 158 210 159 211 public class Ant { 160 private const int maxSteps = 600; 212 private int maxSteps = 600; 213 public int MaxSteps { get { return maxSteps; } set { maxSteps = value; } } 161 214 public enum HeadingEnum { North, East, South, West }; 162 215 public int FoodEaten { get; private set; } … … 166 219 private int steps; 167 220 private HeadingEnum heading; 168 169 170 171 public Ant() { 221 public int PosX { get { return posX; } } 222 public int PosY { get { return posY; } } 223 public HeadingEnum Heading { get { return heading; } } 224 private bool recordTrail = false; 225 private StringBuilder trailBuilder; 226 227 public string Trail { 228 get { 229 if (!recordTrail) throw new NotSupportedException(); 230 else return trailBuilder.ToString() + heading; // add final heading as state 231 } 232 } 233 234 public Ant(bool recordTrail = false) { 172 235 world[00] = " ### ".ToCharArray(); 173 236 world[01] = " # ".ToCharArray(); … … 208 271 FoodEaten = 0; 209 272 steps = 0; 273 274 this.recordTrail = recordTrail; 275 if (this.recordTrail) trailBuilder = new StringBuilder(); 210 276 } 211 277 … … 238 304 world[posY][posX] = '.'; 239 305 } 306 if (recordTrail) trailBuilder.Append("m" + posX + "x" + posY); // record position change 307 } 308 } 309 310 public void Nop() { 311 // wait one time step 312 if (steps <= maxSteps) { 313 steps++; 240 314 } 241 315 } … … 262 336 int nextPosY = posY; 263 337 MoveInternal(ref nextPosX, ref nextPosY); 338 339 if (recordTrail) trailBuilder.Append("?" + nextPosX + "x" + nextPosY); // record check 340 264 341 return world[nextPosY][nextPosX] == '#'; 265 342 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs
r11857 r11976 153 153 canonicalTerms.Add(CanonicalTerm(t)); 154 154 } 155 return canonicalTerms.Select(entry => new Feature(entry, 1.0)).Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); 155 return canonicalTerms.Select(entry => new Feature(entry, 1.0)) 156 .Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); 156 157 } 157 158 -
branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs
r11974 r11976 28 28 //RunDemo(); 29 29 //RunGpDemo(); 30 // RunGridTest();30 // RunGridTest(); 31 31 //RunGpGridTest(); 32 32 RunFunApproxTest(); … … 83 83 //() => new BoltzmannExplorationPolicy(200), 84 84 //() => new BoltzmannExplorationPolicy(500), 85 () => new ChernoffIntervalEstimationPolicy( 0.01),86 () => new ChernoffIntervalEstimationPolicy( 0.05),87 () => new ChernoffIntervalEstimationPolicy( 0.1),88 () => new ChernoffIntervalEstimationPolicy( 0.2),85 // () => new ChernoffIntervalEstimationPolicy( 0.01), 86 // () => new ChernoffIntervalEstimationPolicy( 0.05), 87 // () => new ChernoffIntervalEstimationPolicy( 0.1), 88 // () => new ChernoffIntervalEstimationPolicy( 0.2), 89 89 //() => new ThresholdAscentPolicy(5, 0.01), 90 90 //() => new ThresholdAscentPolicy(5, 0.05), … … 100 100 //() => new ThresholdAscentPolicy(50, 0.2), 101 101 //() => new ThresholdAscentPolicy(100, 0.01), 102 //() => new ThresholdAscentPolicy(100, 0.05),102 () => new ThresholdAscentPolicy(100, 0.05), 103 103 //() => new ThresholdAscentPolicy(100, 0.1), 104 104 //() => new ThresholdAscentPolicy(100, 0.2), … … 113 113 var instanceFactories = new Func<Random, Tuple<IProblem, int>>[] 114 114 { 115 (rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17),115 //(rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17), 116 116 //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:false ), 15), 117 117 //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:true ), 15), … … 122 122 123 123 foreach (var instanceFactory in instanceFactories) { 124 foreach (var useCanonical in new bool[] { true , false}) {125 foreach (var randomTries in new int[] { 0, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) {124 foreach (var useCanonical in new bool[] { true /*, false */ }) { 125 foreach (var randomTries in new int[] { 1 /*, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) { 126 126 foreach (var policyFactory in policyFactories) { 127 127 var myRandomTries = randomTries; … … 311 311 const int seed = 31415; 312 312 //const int maxIters = 50000; 313 var rand = new Random( seed);313 var rand = new Random(); 314 314 var problemFactories = new Func<Tuple<int, int, ISymbolicExpressionTreeProblem>>[] 315 315 { … … 349 349 iterations++; 350 350 globalStatistics.AddSentence(sentence, quality); 351 352 if (iterations % 1000 == 0) { 351 //if (iterations % 100 == 0) { 352 // Console.Clear(); 353 // Console.SetCursorPosition(0, 0); 354 // alg.PrintStats(); 355 //} 356 //Console.WriteLine("{0:N5} {1}", quality, sentence); 357 if (iterations % 200 == 0) { 353 358 Console.WriteLine("\"{0,25}\" {1} \"{2,25}\" {3}", algName, maxSize, probName, globalStatistics); 354 359 }
Note: See TracChangeset
for help on using the changeset viewer.