Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/12/11 17:06:14 (13 years ago)
Author:
mkommend
Message:

#1657: Corrected and adapted implementation of !PTC2 to handle symbol frequencies correctly and to always create trees of the target size. Additionally the performance of the grammars have been improved and a unit test was added.

File:
1 edited

Legend:

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

    r6803 r6911  
    120120      public int ChildIndex { get; set; }
    121121      public int ExtensionPointDepth { get; set; }
     122      public int MaximumExtensionLength { get; set; }
     123      public int MinimumExtensionLength { get; set; }
    122124    }
    123125
     
    132134      // tree length is limited by the grammar and by the explicit size constraints
    133135      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));
    135137      int tries = 0;
    136138      while (tries++ < MAX_TRIES) {
     
    140142        if (targetTreeLength <= 1 || maxDepth <= 1) return;
    141143
    142         bool success = TryCreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth);
     144        bool success = TryCreateFullTreeFromSeed(random, seedNode, targetTreeLength - 1, maxDepth - 1);
    143145
    144146        // if successful => check constraints and return the tree if everything looks ok       
     
    154156    }
    155157
    156     private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
     158    private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root,
    157159      int targetLength, int maxDepth) {
    158160      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);
    162163      if (actualArity < 0) return false;
    163164
     
    166167        var dummy = new SymbolicExpressionTreeNode();
    167168        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
    170176      // 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) {
    172178        int randomIndex = random.Next(extensionPoints.Count);
    173179        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
     
    176182        int argumentIndex = nextExtension.ChildIndex;
    177183        int extensionDepth = nextExtension.ExtensionPointDepth;
    178         if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth - extensionDepth) {
     184
     185        if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) > maxDepth - extensionDepth) {
    179186          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
     187          int insertedTreeLength = parent.GetSubtree(argumentIndex).GetLength();
     188          currentLength += insertedTreeLength;
     189          minExtensionPointsLength -= insertedTreeLength;
     190          maxExtensionPointsLength -= insertedTreeLength;
    180191        } 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
    187207          if (allowedSymbols.Count == 0) return false;
    188208          var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
     
    198218
    199219          currentLength++;
    200           totalListMinLength--;
    201 
    202           actualArity = SampleArity(random, newTree, targetLength - currentLength);
     220          actualArity = SampleArity(random, newTree, targetLength - currentLength, maxDepth - extensionDepth);
    203221          if (actualArity < 0) return false;
    204222          for (int i = 0; i < actualArity; i++) {
     
    206224            var dummy = new SymbolicExpressionTreeNode();
    207225            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;
    209231          }
    210           totalListMinLength += newTree.Grammar.GetMinimumExpressionLength(newTree.Symbol);
    211232        }
    212233      }
     
    218239        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
    219240        int a = nextExtension.ChildIndex;
    220         int d = nextExtension.ExtensionPointDepth;
    221241        ReplaceWithMinimalTree(random, root, parent, a);
    222242      }
     
    252272    }
    253273
    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) {
    259292      // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
    260293      int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol);
     
    263296        maxArity = targetLength;
    264297      }
     298      if (minArity == maxArity) return minArity;
     299
    265300      // 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
    266301      // 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
     
    269304        aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
    270305                                              where s.InitialFrequency > 0.0
    271                                               select node.Grammar.GetMaximumExpressionLength(s)).Max();
     306                                              select node.Grammar.GetMaximumExpressionLength(s, maxDepth)).Max();
    272307        if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1;
    273308        else break;
     
    289324      return random.Next(minArity, maxArity + 1);
    290325    }
     326
    291327  }
    292328}
Note: See TracChangeset for help on using the changeset viewer.