Changeset 7236


Ignore:
Timestamp:
12/27/11 12:36:14 (8 years ago)
Author:
gkronber
Message:

#1654: made some minor changes while reviewing the new tree creation operators.

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

    r7108 r7236  
    6868    }
    6969
    70     public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
     70    public ISymbolicExpressionGrammar ClonedSymbolicExpressionTreeGrammar {
    7171      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
    7272    }
     
    8080    public FullTreeCreator()
    8181      : base() {
    82       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
    83       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
    84       Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
    85       Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
     82      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
     83        "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
     84      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
     85        "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     86      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName,
     87        "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
     88      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName,
     89        "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
    8690    }
    8791
     
    97101          globalScope = globalScope.Parent;
    98102
    99         globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
     103        globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName,
     104          (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
    100105      }
    101106      return base.Apply();
     
    103108
    104109    protected override ISymbolicExpressionTree Create(IRandom random) {
    105       return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
     110      return Create(random, ClonedSymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
    106111    }
    107112
     
    127132
    128133      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
     134      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
    129135      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
    130       if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
    131136
    132137      rootNode.AddSubtree(startNode);
    133138
    134       Grow(random, startNode, maxTreeDepth - 2);
     139      Create(random, startNode, maxTreeDepth - 2);
    135140      tree.Root = rootNode;
    136141      return tree;
    137142    }
    138143
    139     public static void Grow(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) {
     144    public static void Create(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) {
    140145      // make sure it is possible to create a trees smaller than maxDepth
    141146      if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
     
    148153        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    149154
    150       var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0).ToList();
    151 
    152       for (var i = 0; i != arity; ++i) {
    153         var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList();
     155      var allowedSymbols = seedNode.Grammar.AllowedSymbols
     156        .Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0)
     157        .ToList();
     158
     159      for (var i = 0; i < arity; i++) {
     160        var possibleSymbols = allowedSymbols
     161          .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i))
     162          .ToList();
    154163        var selectedSymbol = possibleSymbols.SelectRandom(random);
    155164        var tree = selectedSymbol.CreateTreeNode();
     
    160169      // Only iterate over the non-terminal nodes (those which have arity > 0)
    161170      // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
    162       foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0))
    163         RecursiveGrowFull(random, subTree, 2, maxDepth);
    164     }
    165 
    166     public static void RecursiveGrowFull(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
     171      foreach (var subTree in seedNode.Subtrees)
     172        if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0)
     173          RecursiveCreate(random, subTree, 2, maxDepth);
     174    }
     175
     176    private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
    167177      var arity = root.Grammar.GetMaximumSubtreeCount(root.Symbol);
    168178      // In the 'Full' grow method, we cannot have terminals on the intermediate tree levels.
     
    170180        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    171181
    172       var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); ;
    173 
    174       for (var i = 0; i != arity; ++i) {
    175         var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
    176                                                    root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth &&
    177                                                    root.Grammar.GetMaximumExpressionDepth(s) > maxDepth - currentDepth).ToList();
     182      var allowedSymbols = root.Grammar.AllowedSymbols
     183        .Where(s => s.InitialFrequency > 0.0)
     184        .ToList();
     185
     186      for (var i = 0; i < arity; i++) {
     187        var possibleSymbols = allowedSymbols
     188          .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
     189            root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth &&
     190            root.Grammar.GetMaximumExpressionDepth(s) > maxDepth - currentDepth)
     191          .ToList();
    178192        if (!possibleSymbols.Any())
    179193          throw new InvalidOperationException("No symbols are available for the tree.");
     
    185199      }
    186200
    187       foreach (var subTree in root.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0))
    188         RecursiveGrowFull(random, subTree, currentDepth + 1, maxDepth);
     201      foreach (var subTree in root.Subtrees)
     202        if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0)
     203          RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
    189204    }
    190205  }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs

    r7108 r7236  
    6868    }
    6969
    70     public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
     70    public ISymbolicExpressionGrammar ClonedSymbolicExpressionTreeGrammar {
    7171      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
    7272    }
     
    8080    public GrowTreeCreator()
    8181      : base() {
    82       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
    83       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
    84       Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
    85       Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
     82      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
     83        "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
     84      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
     85        "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     86      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName,
     87        "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
     88      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName,
     89        "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
    8690    }
    8791
     
    97101          globalScope = globalScope.Parent;
    98102
    99         globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
     103        globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName,
     104          (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
    100105      }
    101106      return base.Apply();
     
    103108
    104109    protected override ISymbolicExpressionTree Create(IRandom random) {
    105       return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
     110      return Create(random, ClonedSymbolicExpressionTreeGrammar,
     111        MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
    106112    }
    107113
     
    126132
    127133      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
     134      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
    128135      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
    129       if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
    130136
    131137      rootNode.AddSubtree(startNode);
    132138
    133       Grow(random, startNode, maxTreeDepth - 2);
     139      Create(random, startNode, maxTreeDepth - 2);
    134140      tree.Root = rootNode;
    135141      return tree;
    136142    }
    137143
    138     public static void Grow(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) {
     144    public static void Create(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) {
    139145      // make sure it is possible to create a trees smaller than maxDepth
    140146      if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
     
    146152        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    147153
    148       var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
    149 
    150       for (var i = 0; i != arity; ++i) {
    151         var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList();
     154      var allowedSymbols = seedNode.Grammar.AllowedSymbols
     155        .Where(s => s.InitialFrequency > 0.0)
     156        .ToList();
     157
     158      for (var i = 0; i < arity; i++) {
     159        var possibleSymbols = allowedSymbols
     160          .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i))
     161          .ToList();
    152162        var selectedSymbol = possibleSymbols.SelectRandom(random);
    153163        var tree = selectedSymbol.CreateTreeNode();
     
    158168      // Only iterate over the non-terminal nodes (those which have arity > 0)
    159169      // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
    160       foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0))
    161         RecursiveGrow(random, subTree, 2, maxDepth);
    162     }
    163 
    164     public static void RecursiveGrow(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
     170      foreach (var subTree in seedNode.Subtrees)
     171        if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0)
     172          RecursiveCreate(random, subTree, 2, maxDepth);
     173    }
     174
     175    private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
    165176      var arity = SampleArity(random, root);
    166177      if (arity <= 0)
    167178        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    168179
    169       var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
    170 
    171       for (var i = 0; i != arity; ++i) {
    172         var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth).ToList();
     180      var allowedSymbols = root.Grammar.AllowedSymbols
     181        .Where(s => s.InitialFrequency > 0.0)
     182        .ToList();
     183
     184      for (var i = 0; i < arity; i++) {
     185        var possibleSymbols = allowedSymbols
     186          .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
     187            root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth)
     188          .ToList();
    173189        if (!possibleSymbols.Any())
    174190          throw new InvalidOperationException("No symbols are available for the tree.");
     
    179195      }
    180196
    181       foreach (var subTree in root.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
    182         RecursiveGrow(random, subTree, currentDepth + 1, maxDepth);
     197      foreach (var subTree in root.Subtrees)
     198        if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0)
     199          RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
    183200    }
    184201
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/RampedHalfAndHalfTreeCreator.cs

    r7034 r7236  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.ComponentModel;
    25 using System.Linq;
    2622using HeuristicLab.Common;
    2723using HeuristicLab.Core;
     
    5450    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
    5551      get {
    56         return
    57             (IValueLookupParameter<ISymbolicExpressionGrammar>)
    58             Parameters[SymbolicExpressionTreeGrammarParameterName];
     52        return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName];
    5953      }
    6054    }
     
    6256    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
    6357      get {
    64         return
    65             (ILookupParameter<ISymbolicExpressionGrammar>)
    66             Parameters[ClonedSymbolicExpressionTreeGrammarParameterName];
     58        return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName];
    6759      }
    6860    }
     
    7870    }
    7971
    80     public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
     72    public ISymbolicExpressionGrammar ClonedSymbolicExpressionTreeGrammar {
    8173      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
    8274    }
     
    8981    public RampedHalfAndHalfTreeCreator()
    9082      : base() {
    91       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
    92       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
    93       Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
    94       Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
     83      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
     84        "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
     85      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
     86        "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     87      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName,
     88        "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
     89      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName,
     90        "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
    9591    }
    9692
     
    106102          globalScope = globalScope.Parent;
    107103
    108         globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
     104        globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName,
     105          (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone()));
    109106      }
    110107      return base.Apply();
     
    112109
    113110    protected override ISymbolicExpressionTree Create(IRandom random) {
    114       return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
     111      return Create(random, ClonedSymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
    115112    }
    116113
     
    125122    /// <param name="random">Random generator</param>
    126123    /// <param name="grammar">Available tree grammar</param>
     124    /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
    127125    /// <param name="maxTreeDepth">Maximum tree depth</param>
    128126    /// <returns></returns>
     
    134132
    135133      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
     134      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
    136135      startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
    137       if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
    138136
    139137      rootNode.AddSubtree(startNode);
     
    142140
    143141      if (p < 0.5)
    144         GrowTreeCreator.Grow(random, startNode, maxTreeDepth - 2);
     142        GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
    145143      else
    146         FullTreeCreator.Grow(random, startNode, maxTreeDepth - 2);
     144        FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
    147145
    148146      tree.Root = rootNode;
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs

    r7079 r7236  
    4545    private ISymbolicExpressionGrammar grammar;
    4646    public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
    47       : base("SymbolicExpressionTreeGrammar", "A grammar that is used held by symbolic expression trees and allows extensions to the wraped grammar.") {
     47      : base("SymbolicExpressionTreeGrammar", "A grammar that is used held by symbolic expression trees and allows extensions to the wrapped grammar.") {
    4848      if (grammar == null) throw new ArgumentNullException();
    4949      this.grammar = grammar;
Note: See TracChangeset for help on using the changeset viewer.