Changeset 12891 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
- Timestamp:
- 08/22/15 14:27:37 (9 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
r12155 r12891 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; … … 36 34 ISymbolicExpressionTreeSizeConstraintOperator, 37 35 ISymbolicExpressionTreeGrammarBasedOperator { 38 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";39 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";40 41 #region Parameter Properties42 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 #endregion50 #region Properties51 public IntValue MaximumSymbolicExpressionTreeDepth {52 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }53 }54 55 public IntValue MaximumSymbolicExpressionTreeLength {56 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }57 }58 59 #endregion60 36 61 37 [StorableConstructor] … … 63 39 protected FullTreeCreator(FullTreeCreator original, Cloner cloner) : base(original, cloner) { } 64 40 65 public FullTreeCreator() 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 } 41 public FullTreeCreator() : base() { } 72 42 73 43 public override IDeepCloneable Clone(Cloner cloner) { … … 77 47 78 48 protected override ISymbolicExpressionTree Create(IRandom random) { 79 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 49 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 50 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 80 51 } 81 52 … … 106 77 rootNode.AddSubtree(startNode); 107 78 108 Create(random, startNode, maxTreeDepth - 2);79 Create(random, startNode, maxTreeDepth-1); 109 80 tree.Root = rootNode; 110 81 return tree; … … 128 99 for (var i = 0; i < arity; i++) { 129 100 var possibleSymbols = allowedSymbols 130 .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)) 101 .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i) 102 && seedNode.Grammar.GetMinimumExpressionDepth(s) <= maxDepth 103 && seedNode.Grammar.GetMaximumExpressionDepth(s) >= maxDepth) 131 104 .ToList(); 132 105 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 106 107 #pragma warning disable 612, 618 133 108 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 109 #pragma warning restore 612, 618 110 134 111 var tree = selectedSymbol.CreateTreeNode(); 135 112 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 163 140 throw new InvalidOperationException("No symbols are available for the tree."); 164 141 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 142 143 #pragma warning disable 612, 618 165 144 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 145 #pragma warning restore 612, 618 146 166 147 var tree = selectedSymbol.CreateTreeNode(); 167 148 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 169 150 } 170 151 171 foreach (var subTree in root.Subtrees) 172 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0) 173 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 152 //additional levels should only be added if the maximum depth is not reached yet 153 if (maxDepth > currentDepth) { 154 foreach (var subTree in root.Subtrees) 155 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0) 156 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 157 } 174 158 } 175 159 }
Note: See TracChangeset
for help on using the changeset viewer.