Changeset 6911
- Timestamp:
- 10/12/11 17:06:14 (13 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 added
- 7 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 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammarBase.cs
r6803 r6911 39 39 int GetMaximumSubtreeCount(ISymbol symbol); 40 40 41 int GetMinimumExpressionDepth(ISymbol start); 41 42 int GetMinimumExpressionLength(ISymbol start); 42 int GetMaximumExpressionLength(ISymbol start); 43 int GetMinimumExpressionDepth(ISymbol start); 43 int GetMaximumExpressionLength(ISymbol start, int maxDepth); 44 44 45 45 event EventHandler Changed; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6814 r6911 80 80 : base(deserializing) { 81 81 cachedMinExpressionLength = new Dictionary<string, int>(); 82 cachedMaxExpressionLength = new Dictionary< string, int>();82 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>(); 83 83 cachedMinExpressionDepth = new Dictionary<string, int>(); 84 84 … … 92 92 : base(original, cloner) { 93 93 cachedMinExpressionLength = new Dictionary<string, int>(); 94 cachedMaxExpressionLength = new Dictionary< string, int>();94 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>(); 95 95 cachedMinExpressionDepth = new Dictionary<string, int>(); 96 96 … … 115 115 : base(name, description) { 116 116 cachedMinExpressionLength = new Dictionary<string, int>(); 117 cachedMaxExpressionLength = new Dictionary< string, int>();117 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>(); 118 118 cachedMinExpressionDepth = new Dictionary<string, int>(); 119 119 … … 348 348 } 349 349 350 public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 351 List<string> childs; 352 if (!allowedChildSymbols.TryGetValue(parent.Name, out childs)) 353 return Enumerable.Empty<ISymbol>(); 354 355 return childs.Select(GetSymbol).Where(s => s.Enabled); 356 } 357 358 public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) { 359 var result = Enumerable.Empty<string>(); 360 361 List<string> temp; 362 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 363 result = result.Union(temp); 364 var key = Tuple.Create(parent.Name, argumentIndex); 365 if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) 366 result = result.Union(temp); 367 368 return result.Select(GetSymbol).Where(s => s.Enabled); 350 public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 351 return from child in AllowedSymbols 352 where IsAllowedChildSymbol(parent, child) 353 select child; 354 } 355 356 public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) { 357 return from child in AllowedSymbols 358 where IsAllowedChildSymbol(parent, child, argumentIndex) 359 select child; 369 360 } 370 361 … … 392 383 long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 393 384 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 385 where s.InitialFrequency > 0.0 394 386 select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min() 395 387 select minForSlot).DefaultIfEmpty(0).Sum(); … … 401 393 } 402 394 403 private readonly Dictionary< string, int> cachedMaxExpressionLength;404 public int GetMaximumExpressionLength(ISymbol symbol ) {395 private readonly Dictionary<Tuple<string, int>, int> cachedMaxExpressionLength; 396 public int GetMaximumExpressionLength(ISymbol symbol, int maxDepth) { 405 397 int temp; 406 if (!cachedMaxExpressionLength.TryGetValue(symbol.Name, out temp)) { 407 cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 398 var key = Tuple.Create(symbol.Name, maxDepth); 399 if (!cachedMaxExpressionLength.TryGetValue(key, out temp)) { 400 cachedMaxExpressionLength[key] = int.MaxValue; // prevent infinite recursion 408 401 long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) 409 402 let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 410 select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max() 403 where s.InitialFrequency > 0.0 404 where GetMinimumExpressionDepth(s) < maxDepth 405 select GetMaximumExpressionLength(s, maxDepth - 1)).DefaultIfEmpty(0).Max() 411 406 select maxForSlot).DefaultIfEmpty(0).Sum(); 412 cachedMaxExpressionLength[ symbol.Name] = (int)Math.Min(sumOfMaxTrees, int.MaxValue);413 return cachedMaxExpressionLength[ symbol.Name];407 cachedMaxExpressionLength[key] = (int)Math.Min(sumOfMaxTrees, int.MaxValue); 408 return cachedMaxExpressionLength[key]; 414 409 } 415 410 return temp; … … 423 418 long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 424 419 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 420 where s.InitialFrequency > 0.0 425 421 select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min() 426 422 select minForSlot).DefaultIfEmpty(0).Max(); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs
r6814 r6911 80 80 return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, argumentIndex); 81 81 } 82 public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {83 var symbols = grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent));84 return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled).Distinct();85 }86 public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {87 var symbols = grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex));88 return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled).Distinct();89 }90 82 91 83 public override int GetMinimumSubtreeCount(ISymbol symbol) { … … 122 114 base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount); 123 115 } 116 117 int ISymbolicExpressionGrammarBase.GetMinimumExpressionDepth(ISymbol symbol) { 118 if (symbols.Count == 0) return grammar.GetMinimumExpressionDepth(symbol); 119 else return base.GetMinimumExpressionDepth(symbol); 120 } 121 int ISymbolicExpressionGrammarBase.GetMinimumExpressionLength(ISymbol symbol) { 122 if (symbols.Count == 0) return grammar.GetMinimumExpressionLength(symbol); 123 else return base.GetMinimumExpressionLength(symbol); 124 } 125 int ISymbolicExpressionGrammarBase.GetMaximumExpressionLength(ISymbol symbol, int maxDepth) { 126 if (symbols.Count == 0) return grammar.GetMaximumExpressionLength(symbol, maxDepth); 127 else return base.GetMaximumExpressionLength(symbol, maxDepth); 128 } 124 129 } 125 130 } -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/ProbabilisticTreeCreaterTest.cs
r6866 r6911 72 72 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 73 73 ); 74 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 500 random trees / s 74 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 250); // must achieve more than 250 random trees / s 75 } 76 77 [TestMethod] 78 public void ProbabilisticTreeCreatorSpecificTreeSizesTest() { 79 var trees = new List<ISymbolicExpressionTree>(); 80 var grammar = Grammars.CreateSimpleArithmeticGrammar(); 81 var random = new MersenneTwister(31415); 82 var treeGrammarType = SymbolicExpressionTreeGrammar_Accessor.ShadowedType.ReferencedType; 83 84 85 for (int targetTreeSize = 1; targetTreeSize <= 100; targetTreeSize++) { 86 var tree = new SymbolicExpressionTree(); 87 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 88 rootNode.SetGrammar((ISymbolicExpressionTreeGrammar)Activator.CreateInstance(treeGrammarType, grammar)); 89 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random); 90 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 91 startNode.SetGrammar((ISymbolicExpressionTreeGrammar)Activator.CreateInstance(treeGrammarType, grammar)); 92 if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random); 93 rootNode.AddSubtree(startNode); 94 95 ProbabilisticTreeCreator_Accessor.TryCreateFullTreeFromSeed(random, startNode, targetTreeSize, ((int)Math.Log(targetTreeSize, 2)) + 1); 96 tree.Root = rootNode; 97 Assert.AreEqual(targetTreeSize + 2, tree.Length); //the root and start node must be additionally added 98 } 75 99 } 76 100 } -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/Util.cs
r6866 r6911 154 154 Assert.IsTrue(grammar.Symbols.Count() == grammar.Symbols.Distinct().Count()); 155 155 foreach (ISymbol symbol in grammar.AllowedSymbols) { 156 Assert.IsTrue(grammar.GetMinimumSubtreeCount(symbol) <= grammar.GetMaximumExpressionLength(symbol));157 156 Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol).Count() == grammar.GetAllowedChildSymbols(symbol).Distinct().Count()); 158 157 for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++) { … … 165 164 for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++) 166 165 Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Any()); 167 168 //if (symbol is ProgramRootSymbol) continue;169 ////check if symbol is allowed as at least one child symbol170 //bool result = false;171 //foreach (var parentSymbol in grammar.Symbols) {172 // if (result) break;173 // for (int i = 0; i < grammar.GetMaximumSubtreeCount(parentSymbol); i++)174 // result = result || grammar.IsAllowedChildSymbol(parentSymbol, symbol, i);175 //}176 //Assert.IsTrue(result);177 178 166 } 179 167 } -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r6891 r6911 424 424 <EmbeddedResource Include="HeuristicLab.Problems.QuadraticAssignment-3.3\QAPLIB\wil50.dat" /> 425 425 <None Include="HeuristicLab.snk" /> 426 <Shadow Include="Test References\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.accessor" /> 426 427 <Shadow Include="Test References\HeuristicLab.PluginInfrastructure-3.3.accessor" /> 427 428 <Shadow Include="Test References\HeuristicLab.MainForm.WindowsForms-3.3.accessor" />
Note: See TracChangeset
for help on using the changeset viewer.