Changeset 6911 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
- Timestamp:
- 10/12/11 17:06:14 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r6803 r6911 120 120 public int ChildIndex { get; set; } 121 121 public int ExtensionPointDepth { get; set; } 122 public int MaximumExtensionLength { get; set; } 123 public int MinimumExtensionLength { get; set; } 122 124 } 123 125 … … 132 134 // tree length is limited by the grammar and by the explicit size constraints 133 135 int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol); 134 int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol ));136 int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol, maxDepth)); 135 137 int tries = 0; 136 138 while (tries++ < MAX_TRIES) { … … 140 142 if (targetTreeLength <= 1 || maxDepth <= 1) return; 141 143 142 bool success = TryCreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth);144 bool success = TryCreateFullTreeFromSeed(random, seedNode, targetTreeLength - 1, maxDepth - 1); 143 145 144 146 // if successful => check constraints and return the tree if everything looks ok … … 154 156 } 155 157 156 private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,158 private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, 157 159 int targetLength, int maxDepth) { 158 160 List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>(); 159 int currentLength = 1; 160 int totalListMinLength = globalGrammar.GetMinimumExpressionLength(root.Symbol) - 1; 161 int actualArity = SampleArity(random, root, targetLength); 161 int currentLength = 0; 162 int actualArity = SampleArity(random, root, targetLength, maxDepth); 162 163 if (actualArity < 0) return false; 163 164 … … 166 167 var dummy = new SymbolicExpressionTreeNode(); 167 168 root.AddSubtree(dummy); 168 extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 }); 169 } 169 var x = new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 }; 170 FillExtensionLengths(x, maxDepth); 171 extensionPoints.Add(x); 172 } 173 long minExtensionPointsLength = extensionPoints.Select(x => x.MinimumExtensionLength).Sum(); 174 long maxExtensionPointsLength = extensionPoints.Select(x => x.MaximumExtensionLength).Sum(); 175 170 176 // while there are pending extension points and we have not reached the limit of adding new extension points 171 while (extensionPoints.Count > 0 && totalListMinLength + currentLength <targetLength) {177 while (extensionPoints.Count > 0 && minExtensionPointsLength + currentLength <= targetLength) { 172 178 int randomIndex = random.Next(extensionPoints.Count); 173 179 TreeExtensionPoint nextExtension = extensionPoints[randomIndex]; … … 176 182 int argumentIndex = nextExtension.ChildIndex; 177 183 int extensionDepth = nextExtension.ExtensionPointDepth; 178 if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth - extensionDepth) { 184 185 if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) > maxDepth - extensionDepth) { 179 186 ReplaceWithMinimalTree(random, root, parent, argumentIndex); 187 int insertedTreeLength = parent.GetSubtree(argumentIndex).GetLength(); 188 currentLength += insertedTreeLength; 189 minExtensionPointsLength -= insertedTreeLength; 190 maxExtensionPointsLength -= insertedTreeLength; 180 191 } else { 181 var allowedSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex) 182 where s.InitialFrequency > 0.0 183 where parent.Grammar.GetMinimumExpressionDepth(s) < maxDepth - extensionDepth + 1 184 where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength 185 select s) 186 .ToList(); 192 //remove currently chosen extension point from calculation 193 minExtensionPointsLength -= nextExtension.MinimumExtensionLength; 194 maxExtensionPointsLength -= nextExtension.MaximumExtensionLength; 195 196 var symbols = from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex) 197 where s.InitialFrequency > 0.0 198 where parent.Grammar.GetMinimumExpressionDepth(s) <= maxDepth - extensionDepth 199 where parent.Grammar.GetMinimumExpressionLength(s) <= targetLength - currentLength - minExtensionPointsLength 200 select s; 201 if (maxExtensionPointsLength < targetLength - currentLength) 202 symbols = from s in symbols 203 where parent.Grammar.GetMaximumExpressionLength(s, maxDepth - extensionDepth) >= targetLength - currentLength - maxExtensionPointsLength 204 select s; 205 var allowedSymbols = symbols.ToList(); 206 187 207 if (allowedSymbols.Count == 0) return false; 188 208 var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList(); … … 198 218 199 219 currentLength++; 200 totalListMinLength--; 201 202 actualArity = SampleArity(random, newTree, targetLength - currentLength); 220 actualArity = SampleArity(random, newTree, targetLength - currentLength, maxDepth - extensionDepth); 203 221 if (actualArity < 0) return false; 204 222 for (int i = 0; i < actualArity; i++) { … … 206 224 var dummy = new SymbolicExpressionTreeNode(); 207 225 newTree.AddSubtree(dummy); 208 extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 }); 226 var x = new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 }; 227 FillExtensionLengths(x, maxDepth); 228 extensionPoints.Add(x); 229 maxExtensionPointsLength += x.MaximumExtensionLength; 230 minExtensionPointsLength += x.MinimumExtensionLength; 209 231 } 210 totalListMinLength += newTree.Grammar.GetMinimumExpressionLength(newTree.Symbol);211 232 } 212 233 } … … 218 239 ISymbolicExpressionTreeNode parent = nextExtension.Parent; 219 240 int a = nextExtension.ChildIndex; 220 int d = nextExtension.ExtensionPointDepth;221 241 ReplaceWithMinimalTree(random, root, parent, a); 222 242 } … … 252 272 } 253 273 254 private static bool IsTopLevelBranch(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode branch) { 255 return branch is SymbolicExpressionTreeTopLevelNode; 256 } 257 258 private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) { 274 private static void FillExtensionLengths(TreeExtensionPoint extension, int maxDepth) { 275 var grammar = extension.Parent.Grammar; 276 int maxLength = int.MinValue; 277 int minLength = int.MaxValue; 278 foreach (ISymbol s in grammar.GetAllowedChildSymbols(extension.Parent.Symbol, extension.ChildIndex)) { 279 if (s.InitialFrequency > 0.0) { 280 int max = grammar.GetMaximumExpressionLength(s, maxDepth - extension.ExtensionPointDepth); 281 maxLength = Math.Max(maxLength, max); 282 int min = grammar.GetMinimumExpressionLength(s); 283 minLength = Math.Min(minLength, min); 284 } 285 } 286 287 extension.MaximumExtensionLength = maxLength; 288 extension.MinimumExtensionLength = minLength; 289 } 290 291 private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength, int maxDepth) { 259 292 // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough 260 293 int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol); … … 263 296 maxArity = targetLength; 264 297 } 298 if (minArity == maxArity) return minArity; 299 265 300 // the min number of sub-trees has to be set to a value that is large enough so that the largest possible tree is at least tree length 266 301 // if 1..3 trees are possible and the largest possible first sub-tree is smaller larger than the target length then minArity should be at least 2 … … 269 304 aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i) 270 305 where s.InitialFrequency > 0.0 271 select node.Grammar.GetMaximumExpressionLength(s )).Max();306 select node.Grammar.GetMaximumExpressionLength(s, maxDepth)).Max(); 272 307 if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1; 273 308 else break; … … 289 324 return random.Next(minArity, maxArity + 1); 290 325 } 326 291 327 } 292 328 }
Note: See TracChangeset
for help on using the changeset viewer.