- Timestamp:
- 04/15/10 19:58:42 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
r3338 r3360 29 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols; 30 30 using System.Text; 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators; 31 32 32 33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 51 52 int maxFunctionDefinitions, int maxFunctionArguments 52 53 ) { 53 // tree size is limited by the grammar and by the explicit size constraints54 int allowedMinSize = grammar.GetMinExpressionLength(grammar.StartSymbol);55 int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(grammar.StartSymbol));56 // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)57 int treeSize = random.Next(allowedMinSize, allowedMaxSize);58 54 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 59 do { 60 try { 61 tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1, maxFunctionDefinitions, maxFunctionArguments); 62 } 63 catch (ArgumentException) { 64 // try a different size 65 treeSize = random.Next(allowedMinSize, allowedMaxSize); 66 } 67 } while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight); 55 tree.Root = PTC2(random, grammar, grammar.StartSymbol, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments); 68 56 return tree; 69 57 } … … 74 62 public int ExtensionPointDepth { get; set; } 75 63 } 64 65 /// <summary> 66 /// Creates a random tree with <paramref name="maxTreeSize"/> and <paramref name="maxDepth"/>. 67 /// </summary> 68 /// <param name="random"></param> 69 /// <param name="grammar"></param> 70 /// <param name="rootSymbol"></param> 71 /// <param name="maxTreeSize"></param> 72 /// <param name="maxDepth"></param> 73 /// <param name="maxFunctionDefinitions"></param> 74 /// <param name="maxFunctionArguments"></param> 75 /// <returns></returns> 76 76 public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, 77 int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 78 SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode(); 79 root.Grammar = grammar; 80 if (size <= 1 || maxDepth <= 1) return root; 81 CreateFullTreeFromSeed(random, root, size, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 77 int maxTreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 78 // tree size is limited by the grammar and by the explicit size constraints 79 int allowedMinSize = grammar.GetMinExpressionLength(rootSymbol); 80 int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(rootSymbol)); 81 // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar) 82 int treeSize = random.Next(allowedMinSize, allowedMaxSize + 1); 83 SymbolicExpressionTreeNode root = null; 84 do { 85 try { 86 root = rootSymbol.CreateTreeNode(); 87 root.Grammar = grammar; 88 if (treeSize <= 1 || maxDepth <= 1) return root; 89 CreateFullTreeFromSeed(random, root, treeSize, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 90 } 91 catch (ArgumentException) { 92 // try a different size 93 root = null; 94 treeSize = random.Next(allowedMinSize, allowedMaxSize); 95 } 96 } while (root == null || root.GetSize() > maxTreeSize || root.GetHeight() > maxDepth); 82 97 return root; 83 98 } … … 105 120 if (extensionDepth + parent.Grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) { 106 121 ReplaceWithMinimalTree(random, root, parent, argumentIndex, maxFunctionDefinitions, maxFunctionArguments); 107 //parent.RemoveSubTree(argumentIndex);108 //var allowedSymbols = from s in parent.Grammar.Symbols109 // where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)110 // select s;111 //SymbolicExpressionTreeNode branch = CreateMinimalTree(random, parent, allowedSymbols);112 //parent.InsertSubTree(argumentIndex, branch); // insert a smallest possible tree113 //currentSize += branch.GetSize();114 //totalListMinSize -= branch.GetSize();115 122 } else { 116 123 var allowedSymbols = from s in parent.Grammar.Symbols … … 118 125 where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth 119 126 where parent.Grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize 120 /*||121 totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow122 // terminals or terminal-branches*/123 // where !IsDynamicSymbol(s) || IsDynamicSymbolAllowed(grammar, root, parent, s)124 127 select s; 125 128 Symbol selectedSymbol = SelectRandomSymbol(random, allowedSymbols); … … 172 175 var dummy = new SymbolicExpressionTreeNode(); 173 176 tree.AddSubTree(dummy); 174 dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone(); 177 dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone(); 175 178 // replace the just inserted dummy by recursive application 176 179 ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments); 177 180 } 178 181 } 179 182 180 183 private static void InitializeNewTreeNode(IRandom random, SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) { 181 184 // NB it is assumed that defuns are only allowed as children of root and nowhere else 182 185 // also assumes that newTree is already attached to root somewhere 183 if (IsTopLevelBranch(root, newTree)) 186 if (IsTopLevelBranch(root, newTree)) { 184 187 newTree.Grammar = (ISymbolicExpressionGrammar)newTree.Grammar.Clone(); 188 189 // allow invokes of existing ADFs with higher index 190 int argIndex = root.SubTrees.IndexOf(newTree); 191 for (int i = argIndex + 1; i < root.SubTrees.Count; i++) { 192 var otherDefunNode = root.SubTrees[i] as DefunTreeNode; 193 if (otherDefunNode != null) { 194 GrammarModifier.AddDynamicSymbol(newTree.Grammar, newTree.Symbol, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments); 195 } 196 } 197 } 185 198 if (newTree.Symbol is Defun) { 186 199 var defunTree = newTree as DefunTreeNode; … … 197 210 defunTree.NumberOfArguments = nArgs; 198 211 if (nArgs > 0) { 199 AddDynamicArguments(defunTree.Grammar, nArgs); 200 } 212 GrammarModifier.AddDynamicArguments(defunTree.Grammar, defunTree.Symbol, Enumerable.Range(0, nArgs)); 213 } 214 // in existing branches with smaller index allow invoke of current function 201 215 int argIndex = root.SubTrees.IndexOf(newTree); 202 // allow invokes of ADFs with higher index203 for (int i = argIndex + 1; i < root.SubTrees.Count; i++) {204 var otherDefunNode = root.SubTrees[i] as DefunTreeNode;205 if (otherDefunNode != null) {206 var allowedParents = from sym in defunTree.Grammar.Symbols207 where defunTree.Grammar.IsAllowedChild(defunTree.Symbol, sym, 0)208 select sym;209 var allowedChildren = allowedParents;210 AddDynamicSymbol(defunTree.Grammar, allowedParents, allowedChildren, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);211 }212 }213 // make the ADF available as symbol for other branches (with smaller index to prevent recursions)214 216 for (int i = 0; i < argIndex; i++) { 215 217 // if not dummy node 216 218 if (root.SubTrees[i].Symbol != null) { 217 var topLevelGrammar = root.SubTrees[i].Grammar; 218 var allowedParents = from sym in root.SubTrees[i].Grammar.Symbols 219 where root.SubTrees[i].Grammar.IsAllowedChild(root.SubTrees[i].Symbol, sym, 0) 220 select sym; 221 var allowedChildren = allowedParents; 222 223 AddDynamicSymbol(topLevelGrammar, allowedParents, allowedChildren, functionName, nArgs); 219 var existingBranch = root.SubTrees[i]; 220 GrammarModifier.AddDynamicSymbol(existingBranch.Grammar, existingBranch.Symbol, functionName, nArgs); 224 221 } 225 222 } 226 }227 }228 229 private static void AddDynamicSymbol(ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> allowedParents, IEnumerable<Symbol> allowedChildren, string symbolName, int nArgs) {230 var invokeSym = new InvokeFunction(symbolName);231 grammar.AddSymbol(invokeSym);232 grammar.SetMinSubtreeCount(invokeSym, nArgs);233 grammar.SetMaxSubtreeCount(invokeSym, nArgs);234 foreach (var parent in allowedParents) {235 for (int arg = 0; arg < grammar.GetMaxSubtreeCount(parent); arg++) {236 grammar.SetAllowedChild(parent, invokeSym, arg);237 }238 }239 foreach (var child in allowedChildren) {240 for (int arg = 0; arg < grammar.GetMaxSubtreeCount(invokeSym); arg++) {241 grammar.SetAllowedChild(invokeSym, child, arg);242 }243 }244 }245 246 private static void AddDynamicArguments(ISymbolicExpressionGrammar grammar, int nArgs) {247 for (int argIndex = 0; argIndex < nArgs; argIndex++) {248 var argNode = new Argument(argIndex);249 grammar.AddSymbol(argNode);250 grammar.SetMinSubtreeCount(argNode, 0);251 grammar.SetMaxSubtreeCount(argNode, 0);252 // allow the argument as child of any other symbol253 foreach (var symb in grammar.Symbols)254 for (int i = 0; i < grammar.GetMaxSubtreeCount(symb); i++) {255 grammar.SetAllowedChild(symb, argNode, i);256 }257 223 } 258 224 }
Note: See TracChangeset
for help on using the changeset viewer.