Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/15/11 13:34:38 (14 years ago)
Author:
mkommend
Message:

#1418: Finally added results from the grammar refactoring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs

    r5618 r5686  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Text;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
     
    3433  [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")]
    3534  public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator,
    36     ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator, ISymbolicExpressionTreeArchitectureAlteringOperator {
     35    ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator {
    3736    private const int MAX_TRIES = 100;
    3837    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
    3938    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
    40     private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
    41     private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
    4239    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
    4340    #region Parameter Properties
     
    4845      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
    4946    }
    50     public IValueLookupParameter<IntValue> MaximumFunctionDefinitionsParameter {
    51       get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
    52     }
    53     public IValueLookupParameter<IntValue> MaximumFunctionArgumentsParameter {
    54       get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
    55     }
    56     public IValueLookupParameter<ISymbolicExpressionTreeGrammar> SymbolicExpressionTreeGrammarParameter {
    57       get { return (IValueLookupParameter<ISymbolicExpressionTreeGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
     47    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
     48      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
    5849    }
    5950    #endregion
     
    6556      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
    6657    }
    67     public IntValue MaximumFunctionDefinitions {
    68       get { return MaximumFunctionDefinitionsParameter.ActualValue; }
    69     }
    70     public IntValue MaximumFunctionArguments {
    71       get { return MaximumFunctionArgumentsParameter.ActualValue; }
    72     }
    73     public ISymbolicExpressionTreeGrammar SymbolicExpressionTreeGrammar {
     58    public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
    7459      get { return SymbolicExpressionTreeGrammarParameter.ActualValue; }
    7560    }
     
    8368      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
    8469      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
    85       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionDefinitionsParameterName, "The maximum allowed number of automatically defined functions."));
    86       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionArgumentsParameterName, "The maximum allowed number of arguments of automatically defined functions."));
    87       Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
     70      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
    8871    }
    8972
     
    9376
    9477    protected override ISymbolicExpressionTree Create(IRandom random) {
    95       return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value,
    96         MaximumFunctionDefinitions.Value, MaximumFunctionArguments.Value);
    97     }
    98 
    99     public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar,
    100       int maxTreeLength, int maxTreeDepth,
    101       int maxFunctionDefinitions, int maxFunctionArguments
    102       ) {
     78      return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
     79
     80    }
     81
     82    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar,
     83      int maxTreeLength, int maxTreeDepth) {
    10384      SymbolicExpressionTree tree = new SymbolicExpressionTree();
    104       var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
     85      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
    10586      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
    10687      rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
    107       tree.Root = PTC2(random, rootNode, maxTreeLength, maxTreeDepth, maxFunctionDefinitions, maxFunctionArguments);
     88      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
     89      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
     90      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
     91      rootNode.AddSubTree(startNode);
     92      PTC2(random, startNode, maxTreeLength, maxTreeDepth);
     93      tree.Root = rootNode;
    10894      return tree;
    10995    }
     
    115101    }
    116102
    117     public static ISymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
    118       int maxLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
     103    public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
     104      int maxLength, int maxDepth) {
    119105      // tree length is limited by the grammar and by the explicit size constraints
    120       int allowedMinLength = seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol);
    121       int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol));
     106      int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
     107      int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol));
    122108      int tries = 0;
    123109      while (tries++ < MAX_TRIES) {
     
    125111        int targetTreeLength;
    126112        targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1);
    127         if (targetTreeLength <= 1 || maxDepth <= 1) return seedNode;
    128 
    129         bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
     113        if (targetTreeLength <= 1 || maxDepth <= 1) return;
     114
     115        bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth);
    130116
    131117        // if successful => check constraints and return the tree if everything looks ok       
    132118        if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) {
    133           return seedNode;
     119          return;
    134120        } else {
    135121          // clean seedNode
     
    142128
    143129    private static bool CreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
    144       int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
     130      int targetLength, int maxDepth) {
    145131      try {
    146         TryCreateFullTreeFromSeed(random, root, globalGrammar, targetLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
     132        TryCreateFullTreeFromSeed(random, root, globalGrammar, targetLength, maxDepth);
    147133        return true;
    148134      }
     
    151137
    152138    private static void TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
    153       int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
     139      int targetLength, int maxDepth) {
    154140      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
    155141      int currentLength = 1;
    156       int totalListMinLength = globalGrammar.GetMinExpressionLength(root.Symbol) - 1;
     142      int totalListMinLength = globalGrammar.GetMinimumExpressionLength(root.Symbol) - 1;
    157143      int actualArity = SampleArity(random, root, targetLength);
    158144      for (int i = 0; i < actualArity; i++) {
     
    170156        int argumentIndex = nextExtension.ChildIndex;
    171157        int extensionDepth = nextExtension.ExtensionPointDepth;
    172         if (extensionDepth + parent.Grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) {
    173           ReplaceWithMinimalTree(random, root, parent, argumentIndex, maxFunctionDefinitions, maxFunctionArguments);
     158        if (extensionDepth + parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth) {
     159          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
    174160        } else {
    175161          var allowedSymbols = (from s in parent.Grammar.Symbols
    176                                 where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)
    177                                 where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth
    178                                 where parent.Grammar.GetMaxExpressionLength(s) > targetLength - totalListMinLength - currentLength
     162                                where parent.Grammar.IsAllowedChildSymbol(parent.Symbol, s, argumentIndex)
     163                                where parent.Grammar.GetMinimumExpressionDepth(s) + extensionDepth - 1 < maxDepth
     164                                where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength
    179165                                select s)
    180166                               .ToList();
     
    186172          parent.InsertSubTree(argumentIndex, newTree);
    187173
    188           InitializeNewTreeNode(random, root, newTree, maxFunctionDefinitions, maxFunctionArguments);
     174          var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode;
     175          if (topLevelNode != null)
     176            topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
    189177
    190178          currentLength++;
     
    198186            extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 });
    199187          }
    200           totalListMinLength += newTree.Grammar.GetMinExpressionLength(newTree.Symbol);
     188          totalListMinLength += newTree.Grammar.GetMinimumExpressionLength(newTree.Symbol);
    201189        }
    202190      }
     
    209197        int a = nextExtension.ChildIndex;
    210198        int d = nextExtension.ExtensionPointDepth;
    211         ReplaceWithMinimalTree(random, root, parent, a, maxFunctionDefinitions, maxFunctionArguments);
     199        ReplaceWithMinimalTree(random, root, parent, a);
    212200      }
    213201    }
    214202
    215203    private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
    216       int argumentIndex, int maxFunctionDefinitions, int maxFunctionArguments) {
     204      int childIndex) {
    217205      // determine possible symbols that will lead to the smallest possible tree
    218       var possibleSymbols = (from s in parent.Grammar.GetAllowedSymbols(parent.Symbol, argumentIndex)
    219                              group s by parent.Grammar.GetMinExpressionLength(s) into g
     206      var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
     207                             group s by parent.Grammar.GetMinimumExpressionLength(s) into g
    220208                             orderby g.Key
    221209                             select g).First().ToList();
     
    224212      var tree = selectedSymbol.CreateTreeNode();
    225213      if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
    226       parent.RemoveSubTree(argumentIndex);
    227       parent.InsertSubTree(argumentIndex, tree);
    228       InitializeNewTreeNode(random, root, tree, maxFunctionDefinitions, maxFunctionArguments);
    229       for (int i = 0; i < tree.Grammar.GetMinSubtreeCount(tree.Symbol); i++) {
     214      parent.RemoveSubTree(childIndex);
     215      parent.InsertSubTree(childIndex, tree);
     216
     217      var topLevelNode = tree as SymbolicExpressionTreeTopLevelNode;
     218      if (topLevelNode != null)
     219        topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
     220
     221      for (int i = 0; i < tree.Grammar.GetMinimumSubtreeCount(tree.Symbol); i++) {
    230222        // insert a dummy sub-tree and add the pending extension to the list
    231223        var dummy = new SymbolicExpressionTreeNode();
    232224        tree.AddSubTree(dummy);
    233225        // replace the just inserted dummy by recursive application
    234         ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments);
    235       }
    236     }
    237 
    238     private static void InitializeNewTreeNode(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) {
    239       // NB it is assumed that defuns are only allowed as children of root and nowhere else
    240       // also assumes that newTree is already attached to root somewhere
    241       if (IsTopLevelBranch(root, newTree)) {
    242         ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
    243 
    244         // allow invokes of existing ADFs with higher index
    245         int argIndex = root.IndexOfSubTree(newTree);
    246         for (int i = argIndex + 1; i < root.SubTrees.Count(); i++) {
    247           var otherDefunNode = root.GetSubTree(i) as DefunTreeNode;
    248           if (otherDefunNode != null) {
    249             GrammarModifier.AddDynamicSymbol(newTree.Grammar, newTree.Symbol, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);
    250           }
    251         }
    252       }
    253       if (newTree.Symbol is Defun) {
    254         var defunTree = newTree as DefunTreeNode;
    255         string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ###
    256         var allowedNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
    257                            select "ADF" + index.ToString(formatString);
    258         var takenNames = (from node in root.IterateNodesPrefix().OfType<DefunTreeNode>()
    259                           select node.FunctionName).Distinct();
    260         var remainingNames = allowedNames.Except(takenNames).ToList();
    261         string functionName = remainingNames[random.Next(remainingNames.Count)];
    262         // set name and number of arguments of the ADF
    263         int nArgs = random.Next(maxFunctionArguments);
    264         defunTree.FunctionName = functionName;
    265         defunTree.NumberOfArguments = nArgs;
    266         if (nArgs > 0) {
    267           GrammarModifier.AddDynamicArguments(defunTree.Grammar, defunTree.Symbol, Enumerable.Range(0, nArgs));
    268         }
    269         // in existing branches with smaller index allow invoke of current function
    270         int argIndex = root.IndexOfSubTree(newTree);
    271         for (int i = 0; i < argIndex; i++) {
    272           // if not dummy node
    273           if (root.GetSubTree(i).Symbol != null) {
    274             var existingBranch = root.GetSubTree(i);
    275             GrammarModifier.AddDynamicSymbol(existingBranch.Grammar, existingBranch.Symbol, functionName, nArgs);
    276           }
    277         }
     226        ReplaceWithMinimalTree(random, root, tree, i);
    278227      }
    279228    }
     
    285234    private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) {
    286235      // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
    287       int minArity = node.Grammar.GetMinSubtreeCount(node.Symbol);
    288       int maxArity = node.Grammar.GetMaxSubtreeCount(node.Symbol);
     236      int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol);
     237      int maxArity = node.Grammar.GetMaximumSubtreeCount(node.Symbol);
    289238      if (maxArity > targetLength) {
    290239        maxArity = targetLength;
     
    294243      long aggregatedLongestExpressionLength = 0;
    295244      for (int i = 0; i < maxArity; i++) {
    296         aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i)
    297                                               select node.Grammar.GetMaxExpressionLength(s)).Max();
     245        aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
     246                                              select node.Grammar.GetMaximumExpressionLength(s)).Max();
    298247        if (aggregatedLongestExpressionLength < targetLength) minArity = i;
    299248        else break;
     
    304253      long aggregatedShortestExpressionLength = 0;
    305254      for (int i = 0; i < maxArity; i++) {
    306         aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i)
    307                                                select node.Grammar.GetMinExpressionLength(s)).Min();
     255        aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i)
     256                                               select node.Grammar.GetMinimumExpressionLength(s)).Min();
    308257        if (aggregatedShortestExpressionLength > targetLength) {
    309258          maxArity = i;
Note: See TracChangeset for help on using the changeset viewer.