Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6009


Ignore:
Timestamp:
04/13/11 16:02:40 (12 years ago)
Author:
gkronber
Message:

#1472 implemented a check in PTC2 operator, fixed bugs in SymbolicExpressionGrammarBase and made some small changes in both classes to prevent numeric overflow exceptions.

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs

    r5925 r6009  
    103103    public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
    104104      int maxLength, int maxDepth) {
     105      // make sure it is possible to create a trees smaller than maxLength and maxDepth
     106      if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength)
     107        throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength");
     108      if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
     109        throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
     110
    105111      // tree length is limited by the grammar and by the explicit size constraints
    106112      int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
     
    149155        int argumentIndex = nextExtension.ChildIndex;
    150156        int extensionDepth = nextExtension.ExtensionPointDepth;
    151         if (extensionDepth + parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth) {
     157        if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth - extensionDepth) {
    152158          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
    153159        } else {
     
    155161                                where s.InitialFrequency > 0.0
    156162                                where parent.Grammar.IsAllowedChildSymbol(parent.Symbol, s, argumentIndex)
    157                                 where parent.Grammar.GetMinimumExpressionDepth(s) + extensionDepth - 1 < maxDepth
     163                                where parent.Grammar.GetMinimumExpressionDepth(s) < maxDepth - extensionDepth + 1
    158164                                where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength
    159165                                select s)
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r5809 r6009  
    229229
    230230    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    231       return from s in Symbols where IsAllowedChildSymbol(parent, s) select s;
     231      return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s;
    232232    }
    233233
     
    265265        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    266266        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    267                                               let minForSlot = (long)(from s in Symbols
     267                                              let minForSlot = (long)(from s in AllowedSymbols
    268268                                                                      where IsAllowedChildSymbol(symbol, s, argIndex)
    269269                                                                      select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
     
    282282        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    283283        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
    284                                   let maxForSlot = (long)(from s in Symbols
     284                                  let maxForSlot = (long)(from s in AllowedSymbols
    285285                                                          where IsAllowedChildSymbol(symbol, s, argIndex)
    286286                                                          select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max()
    287287                                  select maxForSlot).DefaultIfEmpty(0).Sum();
    288         long limit = int.MaxValue;
    289         cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit);
     288        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, int.MaxValue);
    290289        return cachedMaxExpressionLength[symbol.Name];
    291290      }
     
    298297      if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) {
    299298        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
    300         cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    301                                                      let minForSlot = (from s in Symbols
    302                                                                        where IsAllowedChildSymbol(symbol, s, argIndex)
    303                                                                        select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
    304                                                      select minForSlot).DefaultIfEmpty(0).Max();
     299        long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
     300                             let minForSlot = (long)(from s in AllowedSymbols
     301                                                     where IsAllowedChildSymbol(symbol, s, argIndex)
     302                                                     select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
     303                             select minForSlot).DefaultIfEmpty(0).Max();
     304        cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);
    305305        return cachedMinExpressionDepth[symbol.Name];
    306306      }
Note: See TracChangeset for help on using the changeset viewer.