Changeset 12422 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs
- Timestamp:
- 06/10/15 11:29:34 (9 years ago)
- 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 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 using HeuristicLab.Parameters;28 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 27 using HeuristicLab.PluginInfrastructure; … … 33 31 [StorableClass] 34 32 [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 { 61 34 [StorableConstructor] 62 35 protected GrowTreeCreator(bool deserializing) : base(deserializing) { } 63 36 protected GrowTreeCreator(GrowTreeCreator original, Cloner cloner) : base(original, cloner) { } 64 37 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() { } 72 39 73 40 public override IDeepCloneable Clone(Cloner cloner) { … … 78 45 protected override ISymbolicExpressionTree Create(IRandom random) { 79 46 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 80 MaximumSymbolicExpressionTreeLength .Value, MaximumSymbolicExpressionTreeDepth.Value);47 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 81 48 } 82 49 … … 122 89 throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero."); 123 90 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(); 127 92 128 93 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(); 132 95 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 96 97 #pragma warning disable 612, 618 133 98 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 99 #pragma warning restore 612, 618 100 134 101 var tree = selectedSymbol.CreateTreeNode(); 135 102 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 146 113 private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) { 147 114 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; 150 117 151 118 var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); 152 119 153 120 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(); 158 123 159 124 if (!possibleSymbols.Any()) 160 125 throw new InvalidOperationException("No symbols are available for the tree."); 126 161 127 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 128 #pragma warning disable 612, 618 162 129 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 130 #pragma warning restore 612, 618 131 163 132 var tree = selectedSymbol.CreateTreeNode(); 164 133 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 166 135 } 167 136 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); 171 141 } 172 142
Note: See TracChangeset
for help on using the changeset viewer.