- Timestamp:
- 04/16/10 12:12:29 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
r3360 r3369 35 35 [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed size")] 36 36 public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator { 37 private const int MAX_TRIES = 100; 37 38 38 39 public ProbabilisticTreeCreator() … … 53 54 ) { 54 55 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 55 tree.Root = PTC2(random, grammar, grammar.StartSymbol, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments); 56 var rootNode = grammar.StartSymbol.CreateTreeNode(); 57 rootNode.Grammar = grammar; 58 tree.Root = PTC2(random, rootNode, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments); 56 59 return tree; 57 60 } … … 63 66 } 64 67 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 public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, 68 public static SymbolicExpressionTreeNode PTC2(IRandom random, SymbolicExpressionTreeNode seedNode, 77 69 int maxTreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 78 70 // 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); 97 return root; 98 } 99 100 private static void CreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 71 int allowedMinSize = seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol); 72 int allowedMaxSize = Math.Min(maxTreeSize, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol)); 73 int tries = 0; 74 while (tries++ < MAX_TRIES) { 75 // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar) 76 int treeSize = random.Next(allowedMinSize, allowedMaxSize + 1); 77 if (treeSize <= 1 || maxDepth <= 1) return seedNode; 78 79 bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, treeSize, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 80 81 // if successfull => check constraints and return the tree if everything looks ok 82 if (success && seedNode.GetSize() <= maxTreeSize && seedNode.GetHeight() <= maxDepth) { 83 return seedNode; 84 } else { 85 // clean seedNode 86 while (seedNode.SubTrees.Count > 0) seedNode.RemoveSubTree(0); 87 } 88 // try a different size MAX_TRIES times 89 } 90 throw new ArgumentException("Couldn't create a valid tree with the specified constraints."); 91 } 92 93 private static bool CreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, ISymbolicExpressionGrammar globalGrammar, 94 int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 95 try { 96 TryCreateFullTreeFromSeed(random, root, globalGrammar, size, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 97 return true; 98 } 99 catch (ArgumentException) { return false; } 100 } 101 102 private static void TryCreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, ISymbolicExpressionGrammar globalGrammar, 103 int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 101 104 List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>(); 102 105 int currentSize = 1; 103 int totalListMinSize = root.Grammar.GetMinExpressionLength(root.Symbol) - 1;106 int totalListMinSize = globalGrammar.GetMinExpressionLength(root.Symbol) - 1; 104 107 int actualArity = SampleArity(random, root, size); 105 108 for (int i = 0; i < actualArity; i++) { … … 107 110 var dummy = new SymbolicExpressionTreeNode(); 108 111 root.AddSubTree(dummy); 109 dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();112 // dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone(); 110 113 extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 2 }); 111 114 } … … 141 144 var dummy = new SymbolicExpressionTreeNode(); 142 145 newTree.AddSubTree(dummy); 143 if (IsTopLevelBranch(root, dummy))144 dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();146 //if (IsTopLevelBranch(root, dummy)) 147 // dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone(); 145 148 extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 }); 146 149 } … … 175 178 var dummy = new SymbolicExpressionTreeNode(); 176 179 tree.AddSubTree(dummy); 177 dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();180 // dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone(); 178 181 // replace the just inserted dummy by recursive application 179 182 ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments); … … 185 188 // also assumes that newTree is already attached to root somewhere 186 189 if (IsTopLevelBranch(root, newTree)) { 187 newTree.Grammar = (ISymbolicExpressionGrammar) newTree.Grammar.Clone();190 newTree.Grammar = (ISymbolicExpressionGrammar)root.Grammar.Clone(); 188 191 189 192 // allow invokes of existing ADFs with higher index … … 225 228 226 229 private static bool IsTopLevelBranch(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode branch) { 227 return root.SubTrees.IndexOf(branch) > -1; 230 //return root.SubTrees.IndexOf(branch) > -1; 231 return branch is SymbolicExpressionTreeTopLevelNode; 228 232 } 229 233
Note: See TracChangeset
for help on using the changeset viewer.