Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/10/15 11:29:34 (9 years ago)
Author:
mkommend
Message:

#2320: Merged the encoding class and all accompanying changes in the trunk.

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

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding

  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs

    r12012 r12422  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
    26 using HeuristicLab.Data;
    27 using HeuristicLab.Parameters;
    2826using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2927using HeuristicLab.PluginInfrastructure;
     
    3331  [StorableClass]
    3432  [Item("GrowTreeCreator", "An operator that creates new symbolic expression trees using the 'Grow' method")]
    35   public class GrowTreeCreator : SymbolicExpressionTreeCreator,
    36                                  ISymbolicExpressionTreeSizeConstraintOperator,
    37                                  ISymbolicExpressionTreeGrammarBasedOperator {
    38     private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
    39     private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
    40 
    41     #region Parameter Properties
    42     public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
    43       get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
    44     }
    45 
    46     public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
    47       get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
    48     }
    49 
    50     #endregion
    51     #region Properties
    52     public IntValue MaximumSymbolicExpressionTreeDepth {
    53       get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
    54     }
    55 
    56     public IntValue MaximumSymbolicExpressionTreeLength {
    57       get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
    58     }
    59     #endregion
    60 
     33  public class GrowTreeCreator : SymbolicExpressionTreeCreator {
    6134    [StorableConstructor]
    6235    protected GrowTreeCreator(bool deserializing) : base(deserializing) { }
    6336    protected GrowTreeCreator(GrowTreeCreator original, Cloner cloner) : base(original, cloner) { }
    6437
    65     public GrowTreeCreator()
    66       : base() {
    67       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName,
    68         "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored)."));
    69       Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName,
    70         "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
    71     }
     38    public GrowTreeCreator() : base() { }
    7239
    7340    public override IDeepCloneable Clone(Cloner cloner) {
     
    7845    protected override ISymbolicExpressionTree Create(IRandom random) {
    7946      return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue,
    80         MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
     47        MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value);
    8148    }
    8249
     
    12289        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    12390
    124       var allowedSymbols = seedNode.Grammar.AllowedSymbols
    125         .Where(s => s.InitialFrequency > 0.0)
    126         .ToList();
     91      var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
    12792
    12893      for (var i = 0; i < arity; i++) {
    129         var possibleSymbols = allowedSymbols
    130           .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i))
    131           .ToList();
     94        var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList();
    13295        var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
     96
     97#pragma warning disable 612, 618
    13398        var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
     99#pragma warning restore 612, 618
     100
    134101        var tree = selectedSymbol.CreateTreeNode();
    135102        if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
     
    146113    private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
    147114      var arity = SampleArity(random, root);
    148       if (arity <= 0)
    149         throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
     115      if (arity == 0)
     116        return;
    150117
    151118      var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
    152119
    153120      for (var i = 0; i < arity; i++) {
    154         var possibleSymbols = allowedSymbols
    155           .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
    156             root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth)
    157           .ToList();
     121        var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
     122                                                        root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth).ToList();
    158123
    159124        if (!possibleSymbols.Any())
    160125          throw new InvalidOperationException("No symbols are available for the tree.");
     126
    161127        var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
     128#pragma warning disable 612, 618
    162129        var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
     130#pragma warning restore 612, 618
     131
    163132        var tree = selectedSymbol.CreateTreeNode();
    164133        if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
     
    166135      }
    167136
    168       foreach (var subTree in root.Subtrees)
    169         if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0)
    170           RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
     137      if (maxDepth > currentDepth)
     138        foreach (var subTree in root.Subtrees)
     139          if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0)
     140            RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
    171141    }
    172142
Note: See TracChangeset for help on using the changeset viewer.