- Timestamp:
- 11/25/11 15:25:54 (13 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
r7012 r7076 63 63 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 64 64 } 65 65 66 66 public IntValue MaximumSymbolicExpressionTreeLength { 67 67 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } … … 107 107 108 108 public override ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) { 109 return Create(random, grammar, maxTreeLength, maxTreeDepth);109 return Create(random, grammar, maxTreeLength, maxTreeDepth); 110 110 } 111 111 … … 149 149 150 150 for (var i = 0; i != arity; ++i) { 151 var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol, i).Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0);151 var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol, i).Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0); 152 152 var selectedSymbol = possibleSymbols.SelectRandom(random); 153 153 var tree = selectedSymbol.CreateTreeNode(); … … 168 168 throw new ArgumentException("Cannot grow node of arity zero. Expected a function node."); 169 169 170 for (var i = 0; i != arity; ++i) { 171 var possibleSymbols = root.Grammar.GetAllowedChildSymbols(root.Symbol, i); 172 possibleSymbols = possibleSymbols.Where(s => s.InitialFrequency > 0.0 && 173 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth && 174 root.Grammar.GetMaximumExpressionDepth(s) > maxDepth - currentDepth); 170 175 171 for (var i = 0; i != arity; ++i) { 172 var possibleSymbols = currentDepth < maxDepth ? 173 root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) > 0) : 174 root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) == 0); 176 177 if (!possibleSymbols.Any()) throw new InvalidOperationException("No symbols are available for the tree."); 175 178 var selectedSymbol = possibleSymbols.SelectRandom(random); 176 179 var tree = selectedSymbol.CreateTreeNode(); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs
r7012 r7076 64 64 } 65 65 66 public IntValue MaximumSymbolicExpressionTreeLength { 66 public IntValue MaximumSymbolicExpressionTreeLength { 67 67 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 68 68 } … … 166 166 throw new ArgumentException("Cannot grow node of arity zero. Expected a function node."); 167 167 168 169 168 for (var i = 0; i != arity; ++i) { 170 var possibleSymbols = currentDepth < maxDepth171 ? root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(s => s.InitialFrequency > 0.0)172 : root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(173 s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) == 0);169 var possibleSymbols = root.Grammar.GetAllowedChildSymbols(root.Symbol, i); 170 possibleSymbols = possibleSymbols.Where(s => s.InitialFrequency > 0.0 && 171 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth); 172 if (!possibleSymbols.Any()) throw new InvalidOperationException("No symbols are available for the tree."); 174 173 var selectedSymbol = possibleSymbols.SelectRandom(random); 175 174 var tree = selectedSymbol.CreateTreeNode(); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammarBase.cs
r6911 r7076 40 40 41 41 int GetMinimumExpressionDepth(ISymbol start); 42 int GetMaximumExpressionDepth(ISymbol start); 42 43 int GetMinimumExpressionLength(ISymbol start); 43 44 int GetMaximumExpressionLength(ISymbol start, int maxDepth); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r7001 r7076 82 82 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>(); 83 83 cachedMinExpressionDepth = new Dictionary<string, int>(); 84 cachedMaxExpressionDepth = new Dictionary<string, int>(); 84 85 85 86 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>(); … … 94 95 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>(); 95 96 cachedMinExpressionDepth = new Dictionary<string, int>(); 97 cachedMaxExpressionDepth = new Dictionary<string, int>(); 96 98 97 99 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>(); … … 117 119 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>(); 118 120 cachedMinExpressionDepth = new Dictionary<string, int>(); 121 cachedMaxExpressionDepth = new Dictionary<string, int>(); 119 122 120 123 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>(); … … 372 375 cachedMaxExpressionLength.Clear(); 373 376 cachedMinExpressionDepth.Clear(); 377 cachedMaxExpressionDepth.Clear(); 374 378 375 379 cachedIsAllowedChildSymbol.Clear(); … … 428 432 } 429 433 434 private readonly Dictionary<string, int> cachedMaxExpressionDepth; 435 public int GetMaximumExpressionDepth(ISymbol symbol) { 436 int temp; 437 if (!cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) { 438 cachedMaxExpressionDepth[symbol.Name] = int.MaxValue; 439 long maxDepth = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) 440 let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 441 where s.InitialFrequency > 0.0 442 select GetMaximumExpressionDepth(s)).DefaultIfEmpty(0).Max() 443 select maxForSlot).DefaultIfEmpty(0).Max(); 444 cachedMaxExpressionDepth[symbol.Name] = (int)Math.Min(maxDepth, int.MaxValue); 445 return cachedMaxExpressionDepth[symbol.Name]; 446 } 447 return temp; 448 } 449 430 450 public event EventHandler Changed; 431 451 protected virtual void OnChanged() {
Note: See TracChangeset
for help on using the changeset viewer.