- Timestamp:
- 04/09/10 17:28:32 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
r3270 r3294 37 37 38 38 protected override SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight) { 39 return Apply(random, grammar, maxTreeSize.Value, maxTreeHeight.Value);39 return Create(random, grammar, maxTreeSize.Value, maxTreeHeight.Value); 40 40 } 41 41 42 public SymbolicExpressionTree Apply(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) {42 public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) { 43 43 // tree size is limited by the grammar and by the explicit size constraints 44 int allowedMinSize = grammar. MinimalExpressionLength(grammar.StartSymbol);45 int allowedMaxSize = Math.Min(maxTreeSize, grammar. MaximalExpressionLength(grammar.StartSymbol));44 int allowedMinSize = grammar.GetMinExpressionLength(grammar.StartSymbol); 45 int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(grammar.StartSymbol)); 46 46 // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar) 47 47 int treeSize = random.Next(allowedMinSize, allowedMaxSize); … … 49 49 do { 50 50 try { 51 tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1); 51 tree.Root = grammar.ProgramRootSymbol.CreateTreeNode(); 52 tree.Root.AddSubTree(PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1)); 52 53 } 53 54 catch (ArgumentException) { … … 55 56 treeSize = random.Next(allowedMinSize, allowedMaxSize); 56 57 } 57 } while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight); 58 } while (tree.Root.SubTrees.Count == 0 || tree.Size > maxTreeSize || tree.Height > maxTreeHeight); 59 System.Diagnostics.Debug.Assert(grammar.IsValidExpression(tree)); 58 60 return tree; 59 61 } 60 62 61 private Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {63 private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) { 62 64 var symbolList = symbols.ToList(); 63 var ticketsSum = symbolList.Select(x => x. Tickets.Value).Sum();65 var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum(); 64 66 var r = random.NextDouble() * ticketsSum; 65 67 double aggregatedTickets = 0; 66 68 for (int i = 0; i < symbolList.Count; i++) { 67 aggregatedTickets += symbolList[i]. Tickets.Value;69 aggregatedTickets += symbolList[i].InitialFrequency; 68 70 if (aggregatedTickets >= r) { 69 71 return symbolList[i]; … … 75 77 76 78 77 p rivateSymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) {79 public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) { 78 80 SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode(); 79 81 if (size <= 1 || maxDepth <= 1) return root; 80 List<object[]> list= new List<object[]>();82 List<object[]> extensionPoints = new List<object[]>(); 81 83 int currentSize = 1; 82 int totalListMinSize = grammar. MinimalExpressionLength(rootSymbol) - 1;84 int totalListMinSize = grammar.GetMinExpressionLength(rootSymbol) - 1; 83 85 84 86 int actualArity = SampleArity(random, grammar, rootSymbol, size); … … 86 88 // insert a dummy sub-tree and add the pending extension to the list 87 89 root.AddSubTree(null); 88 list.Add(new object[] { root, i, 2 });90 extensionPoints.Add(new object[] { root, i, 2 }); 89 91 } 90 92 91 93 // while there are pending extension points and we have not reached the limit of adding new extension points 92 while ( list.Count > 0 && totalListMinSize + currentSize < size) {93 int randomIndex = random.Next( list.Count);94 object[] nextExtension = list[randomIndex];95 list.RemoveAt(randomIndex);94 while (extensionPoints.Count > 0 && totalListMinSize + currentSize < size) { 95 int randomIndex = random.Next(extensionPoints.Count); 96 object[] nextExtension = extensionPoints[randomIndex]; 97 extensionPoints.RemoveAt(randomIndex); 96 98 SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0]; 97 99 int argumentIndex = (int)nextExtension[1]; 98 100 int extensionDepth = (int)nextExtension[2]; 99 if (extensionDepth + grammar. MinimalExpressionDepth(parent.Symbol) >= maxDepth) {101 if (extensionDepth + grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) { 100 102 parent.RemoveSubTree(argumentIndex); 101 SymbolicExpressionTreeNode branch = CreateMinimalTree(random, grammar, grammar. AllowedSymbols(parent.Symbol, argumentIndex));103 SymbolicExpressionTreeNode branch = CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, argumentIndex)); 102 104 parent.InsertSubTree(argumentIndex, branch); // insert a smallest possible tree 103 105 currentSize += branch.GetSize(); 104 106 totalListMinSize -= branch.GetSize(); 105 107 } else { 106 var allowedSubFunctions = from s in grammar. AllowedSymbols(parent.Symbol, argumentIndex)107 where grammar. MinimalExpressionDepth(parent.Symbol) + extensionDepth - 1 < maxDepth108 where grammar. MaximalExpressionLength(s) > size - totalListMinSize - currentSize ||108 var allowedSubFunctions = from s in grammar.GetAllowedSymbols(parent.Symbol, argumentIndex) 109 where grammar.GetMinExpressionDepth(parent.Symbol) + extensionDepth - 1 < maxDepth 110 where grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize || 109 111 totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow 110 112 // terminals or terminal-branches … … 121 123 // insert a dummy sub-tree and add the pending extension to the list 122 124 newTree.AddSubTree(null); 123 list.Add(new object[] { newTree, i, extensionDepth + 1 });125 extensionPoints.Add(new object[] { newTree, i, extensionDepth + 1 }); 124 126 } 125 totalListMinSize += grammar. MinimalExpressionLength(selectedSymbol);127 totalListMinSize += grammar.GetMinExpressionLength(selectedSymbol); 126 128 } 127 129 } 128 130 // fill all pending extension points 129 while ( list.Count > 0) {130 int randomIndex = random.Next( list.Count);131 object[] nextExtension = list[randomIndex];132 list.RemoveAt(randomIndex);131 while (extensionPoints.Count > 0) { 132 int randomIndex = random.Next(extensionPoints.Count); 133 object[] nextExtension = extensionPoints[randomIndex]; 134 extensionPoints.RemoveAt(randomIndex); 133 135 SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0]; 134 136 int a = (int)nextExtension[1]; … … 136 138 parent.RemoveSubTree(a); 137 139 parent.InsertSubTree(a, 138 CreateMinimalTree(random, grammar, grammar. AllowedSymbols(parent.Symbol, a))); // append a tree with minimal possible height140 CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, a))); // append a tree with minimal possible height 139 141 } 140 142 return root; 141 143 } 142 144 143 private int SampleArity(IRandom random, ISymbolicExpressionGrammar grammar, Symbol symbol, int targetSize) {145 private static int SampleArity(IRandom random, ISymbolicExpressionGrammar grammar, Symbol symbol, int targetSize) { 144 146 // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough 145 int minArity = grammar. MinSubTrees(symbol);146 int maxArity = grammar. MaxSubTrees(symbol);147 int minArity = grammar.GetMinSubTreeCount(symbol); 148 int maxArity = grammar.GetMaxSubTreeCount(symbol); 147 149 if (maxArity > targetSize) { 148 150 maxArity = targetSize; … … 152 154 long aggregatedLongestExpressionLength = 0; 153 155 for (int i = 0; i < maxArity; i++) { 154 aggregatedLongestExpressionLength += (from s in grammar. AllowedSymbols(symbol, i)155 select grammar. MaximalExpressionLength(s)).Max();156 aggregatedLongestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i) 157 select grammar.GetMaxExpressionLength(s)).Max(); 156 158 if (aggregatedLongestExpressionLength < targetSize) minArity = i; 157 159 else break; … … 162 164 long aggregatedShortestExpressionLength = 0; 163 165 for (int i = 0; i < maxArity; i++) { 164 aggregatedShortestExpressionLength += (from s in grammar. AllowedSymbols(symbol, i)165 select grammar. MinimalExpressionLength(s)).Min();166 aggregatedShortestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i) 167 select grammar.GetMinExpressionLength(s)).Min(); 166 168 if (aggregatedShortestExpressionLength > targetSize) { 167 169 maxArity = i; … … 173 175 } 174 176 175 private SymbolicExpressionTreeNode CreateMinimalTree(IRandom random, ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> symbols) {177 private static SymbolicExpressionTreeNode CreateMinimalTree(IRandom random, ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> symbols) { 176 178 // determine possible symbols that will lead to the smallest possible tree 177 179 var possibleSymbols = (from s in symbols 178 group s by grammar. MinimalExpressionLength(s) into g180 group s by grammar.GetMinExpressionLength(s) into g 179 181 orderby g.Key 180 182 select g).First(); … … 182 184 // build minimal tree by recursive application 183 185 var tree = selectedSymbol.CreateTreeNode(); 184 for (int i = 0; i < grammar. MinSubTrees(selectedSymbol); i++)185 tree.AddSubTree(CreateMinimalTree(random, grammar, grammar. AllowedSymbols(selectedSymbol, i)));186 for (int i = 0; i < grammar.GetMinSubTreeCount(selectedSymbol); i++) 187 tree.AddSubTree(CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(selectedSymbol, i))); 186 188 return tree; 187 189 }
Note: See TracChangeset
for help on using the changeset viewer.