Changeset 7268 for branches/HeuristicLab.TimeSeries/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
- Timestamp:
- 01/03/12 11:22:21 (12 years ago)
- Location:
- branches/HeuristicLab.TimeSeries
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries
- Property svn:mergeinfo changed
/trunk/sources merged: 7214,7216-7230,7233-7239,7241,7243-7252,7254,7256-7261,7265-7266
- Property svn:mergeinfo changed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
r7213 r7268 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 1Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 68 68 } 69 69 70 public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {70 public ISymbolicExpressionGrammar ClonedSymbolicExpressionTreeGrammar { 71 71 get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; } 72 72 } … … 80 80 public FullTreeCreator() 81 81 : 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.")); 86 90 } 87 91 … … 97 101 globalScope = globalScope.Parent; 98 102 99 globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone())); 103 globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, 104 (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone())); 100 105 } 101 106 return base.Apply(); … … 103 108 104 109 protected override ISymbolicExpressionTree Create(IRandom random) { 105 return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);110 return Create(random, ClonedSymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 106 111 } 107 112 … … 127 132 128 133 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 134 if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random); 129 135 startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar)); 130 if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);131 136 132 137 rootNode.AddSubtree(startNode); 133 138 134 Grow(random, startNode, maxTreeDepth - 2);139 Create(random, startNode, maxTreeDepth - 2); 135 140 tree.Root = rootNode; 136 141 return tree; 137 142 } 138 143 139 public static void Grow(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) {144 public static void Create(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxDepth) { 140 145 // make sure it is possible to create a trees smaller than maxDepth 141 146 if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth) … … 148 153 throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero."); 149 154 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(); 154 163 var selectedSymbol = possibleSymbols.SelectRandom(random); 155 164 var tree = selectedSymbol.CreateTreeNode(); … … 160 169 // Only iterate over the non-terminal nodes (those which have arity > 0) 161 170 // 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) { 167 177 var arity = root.Grammar.GetMaximumSubtreeCount(root.Symbol); 168 178 // In the 'Full' grow method, we cannot have terminals on the intermediate tree levels. … … 170 180 throw new ArgumentException("Cannot grow node of arity zero. Expected a function node."); 171 181 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(); 178 192 if (!possibleSymbols.Any()) 179 193 throw new InvalidOperationException("No symbols are available for the tree."); … … 185 199 } 186 200 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); 189 204 } 190 205 }
Note: See TracChangeset
for help on using the changeset viewer.