Changeset 10415
- Timestamp:
- 01/28/14 19:23:46 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/MonteCarloTreeSearchCodeGen.cs
r10411 r10415 15 15 public double sumQuality = 0.0; 16 16 public double bestQuality = double.NegativeInfinity; 17 public bool ready;17 public bool done; 18 18 public SearchTreeNode[] children; 19 19 public double[] Ucb { 20 get { 21 return (from c in children 22 select ?IDENT?Solver.UCB(this, c) 23 ).ToArray(); 24 } 25 } 20 26 public SearchTreeNode() { 21 27 } … … 37 43 38 44 private void SampleTree(SearchTreeNode searchTree, Stack<Tuple<Tree, int, int>> extensionPoints) { 39 const int RANDOM_TRIES = 100; 40 if(extensionPoints.Count == 0) return; // nothing to do 45 const int RANDOM_TRIES = 1000; 46 if(extensionPoints.Count == 0) { 47 searchTree.done = true; 48 return; // nothing to do 49 } 41 50 var extensionPoint = extensionPoints.Pop(); 42 51 Tree parent = extensionPoint.Item1; … … 44 53 int subtreeIdx = extensionPoint.Item3; 45 54 Tree t = null; 46 47 55 if(searchTree.tries < RANDOM_TRIES || Grammar.subtreeCount[state] == 0) { 48 56 t = SampleTreeRandom(state); … … 53 61 searchTree.children = new SearchTreeNode[] { new SearchTreeNode() } ; 54 62 SampleTree(searchTree.children[0], extensionPoints); 63 if(searchTree.children[0].done) searchTree.done = true; 55 64 } else { 56 65 // fill up all remaining slots randomly … … 81 90 extensionPoints.Push(Tuple.Create(t, Grammar.transition[state][i], i)); 82 91 } 83 SampleTree(searchTree, extensionPoints); 84 } 85 } 92 SampleTree(searchTree, extensionPoints); 93 } 94 } 86 95 Debug.Assert(parent.subtrees[subtreeIdx] == null); 87 96 parent.subtrees[subtreeIdx] = t; … … 95 104 return altIdx; 96 105 } else { 106 altIdx = Array.FindIndex(searchTree.children, (e) => !e.done && e.tries < 1000); 107 if(altIdx >= 0) return altIdx; 97 108 // select the least sampled alternative 109 //altIdx = 0; 110 //int minSamples = searchTree.children[altIdx].tries; 111 //for(int idx = 1; idx < searchTree.children.Length; idx++) { 112 // if(!searchTree.children[idx].done && searchTree.children[idx].tries < minSamples) { 113 // minSamples = searchTree.children[idx].tries; 114 // altIdx = idx; 115 // } 116 //} 117 // select the alternative with the largest average 98 118 altIdx = 0; 99 int minSamples = searchTree.children[altIdx].tries;119 double bestAverage = UCB(searchTree, searchTree.children[altIdx]); 100 120 for(int idx = 1; idx < searchTree.children.Length; idx++) { 101 if(searchTree.children[idx].tries < minSamples) { 102 minSamples = searchTree.children[idx].tries; 121 if (!searchTree.children[idx].done && UCB(searchTree, searchTree.children[idx]) > UCB(searchTree, searchTree.children[altIdx])) { 103 122 altIdx = idx; 104 123 } 105 124 } 106 // select the alternative with the largest average 107 // altIdx = 0; 108 // double bestAverage = UCB(searchTree, searchTree.children[altIdx]); 109 // for(int idx = 1; idx < searchTree.children.Length; idx++) { 110 // if (UCB(searchTree, searchTree.children[idx]) > UCB(searchTree, searchTree.children[altIdx])) { 111 // altIdx = idx; 112 // } 113 // } 125 126 searchTree.done = searchTree.children.All(c=>c.done); 114 127 return altIdx; 115 128 } 116 129 } 117 130 118 p rivatedouble UCB(SearchTreeNode parent, SearchTreeNode n) {131 public static double UCB(SearchTreeNode parent, SearchTreeNode n) { 119 132 Debug.Assert(parent.tries >= n.tries); 120 133 Debug.Assert(n.tries > 0); 121 return n.sumQuality / n.tries + Math.Sqrt(( 2* Math.Log(parent.tries)) / n.tries ); // constant is dependent fitness function values134 return n.sumQuality / n.tries + Math.Sqrt((10 * Math.Log(parent.tries)) / n.tries ); // constant is dependent fitness function values 122 135 } 123 136 … … 144 157 if(t.subtrees != null) { 145 158 Debug.Assert(t.subtrees.Length == 1); 146 if(searchTree.children !=null) {159 if(searchTree.children != null) { 147 160 trees.Push(t.subtrees[0]); 148 161 UpdateSearchTree(searchTree.children[t.altIdx], trees, quality); 149 162 } 150 163 } else { 151 if(searchTree.children !=null) {164 if(searchTree.children != null) { 152 165 Debug.Assert(searchTree.children.Length == 1); 153 166 UpdateSearchTree(searchTree.children[0], trees, quality); … … 225 238 var sw = new System.Diagnostics.Stopwatch(); 226 239 sw.Start(); 227 while ( true) {240 while (!searchTree.done) { 228 241 229 242 int steps, depth; … … 297 310 GenerateReturnStatement(terminalAltIndexes, sb); 298 311 sb.Append("} else {"); 299 GenerateReturnStatement(nonTerminalAltIndexes , sb);312 GenerateReturnStatement(nonTerminalAltIndexes.Concat(terminalAltIndexes), sb); 300 313 sb.Append("}").EndBlock(); 301 314 } else { … … 317 330 foreach (var constr in terminal.Constraints) { 318 331 if (constr.Type == ConstraintNodeType.Set) { 319 sb.Append("{").BeginBlock(); 320 sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); 321 sb.AppendFormat("t.{0} = elements[random.Next(elements.Length)]; ", constr.Ident).EndBlock(); 322 sb.AppendLine("}"); 332 throw new NotImplementedException("Support for terminal symbols with attributes is not yet implemented."); 333 // sb.Append("{").BeginBlock(); 334 // sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); 335 // sb.AppendFormat("t.{0} = elements[random.Next(elements.Length)]; ", constr.Ident).EndBlock(); 336 // sb.AppendLine("}"); 323 337 } else { 324 338 throw new NotSupportedException("The MTCS solver does not support RANGE constraints."); -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10400 r10415 151 151 // var randomSearchCodeGen = new RandomSearchCodeGen(); 152 152 // randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode); 153 // var bruteForceSearchCodeGen = new BruteForceCodeGen();154 // bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);153 // var bruteForceSearchCodeGen = new BruteForceCodeGen(); 154 // bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode); 155 155 var mctsCodeGen = new MonteCarloTreeSearchCodeGen(); 156 156 mctsCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode); -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.sln
r10394 r10415 43 43 Examples\multi-output-multiplier.txt = Examples\multi-output-multiplier.txt 44 44 Examples\Multiplexer.txt = Examples\Multiplexer.txt 45 Examples\OneMaxBinary.txt = Examples\OneMaxBinary.txt 45 46 Examples\OnlyTerminals.txt = Examples\OnlyTerminals.txt 46 47 Examples\Rastrigin.txt = Examples\Rastrigin.txt
Note: See TracChangeset
for help on using the changeset viewer.