Changeset 5686 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
- Timestamp:
- 03/15/11 13:34:38 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r5618 r5686 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 34 33 [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")] 35 34 public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator, 36 ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator , ISymbolicExpressionTreeArchitectureAlteringOperator{35 ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator { 37 36 private const int MAX_TRIES = 100; 38 37 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 39 38 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 40 private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";41 private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";42 39 private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar"; 43 40 #region Parameter Properties … … 48 45 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 49 46 } 50 public IValueLookupParameter<IntValue> MaximumFunctionDefinitionsParameter { 51 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; } 52 } 53 public IValueLookupParameter<IntValue> MaximumFunctionArgumentsParameter { 54 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; } 55 } 56 public IValueLookupParameter<ISymbolicExpressionTreeGrammar> SymbolicExpressionTreeGrammarParameter { 57 get { return (IValueLookupParameter<ISymbolicExpressionTreeGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; } 47 public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter { 48 get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; } 58 49 } 59 50 #endregion … … 65 56 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 66 57 } 67 public IntValue MaximumFunctionDefinitions { 68 get { return MaximumFunctionDefinitionsParameter.ActualValue; } 69 } 70 public IntValue MaximumFunctionArguments { 71 get { return MaximumFunctionArgumentsParameter.ActualValue; } 72 } 73 public ISymbolicExpressionTreeGrammar SymbolicExpressionTreeGrammar { 58 public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar { 74 59 get { return SymbolicExpressionTreeGrammarParameter.ActualValue; } 75 60 } … … 83 68 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 84 69 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 85 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionDefinitionsParameterName, "The maximum allowed number of automatically defined functions.")); 86 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionArgumentsParameterName, "The maximum allowed number of arguments of automatically defined functions.")); 87 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); 70 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); 88 71 } 89 72 … … 93 76 94 77 protected override ISymbolicExpressionTree Create(IRandom random) { 95 return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value, 96 MaximumFunctionDefinitions.Value, MaximumFunctionArguments.Value); 97 } 98 99 public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar, 100 int maxTreeLength, int maxTreeDepth, 101 int maxFunctionDefinitions, int maxFunctionArguments 102 ) { 78 return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 79 80 } 81 82 public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, 83 int maxTreeLength, int maxTreeDepth) { 103 84 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 104 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar. StartSymbol.CreateTreeNode();85 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 105 86 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random); 106 87 rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar)); 107 tree.Root = PTC2(random, rootNode, maxTreeLength, maxTreeDepth, maxFunctionDefinitions, maxFunctionArguments); 88 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 89 startNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar)); 90 if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random); 91 rootNode.AddSubTree(startNode); 92 PTC2(random, startNode, maxTreeLength, maxTreeDepth); 93 tree.Root = rootNode; 108 94 return tree; 109 95 } … … 115 101 } 116 102 117 public static ISymbolicExpressionTreeNodePTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,118 int maxLength, int maxDepth , int maxFunctionDefinitions, int maxFunctionArguments) {103 public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode, 104 int maxLength, int maxDepth) { 119 105 // tree length is limited by the grammar and by the explicit size constraints 120 int allowedMinLength = seedNode.Grammar.GetMin ExpressionLength(seedNode.Symbol);121 int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMax ExpressionLength(seedNode.Symbol));106 int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol); 107 int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol)); 122 108 int tries = 0; 123 109 while (tries++ < MAX_TRIES) { … … 125 111 int targetTreeLength; 126 112 targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1); 127 if (targetTreeLength <= 1 || maxDepth <= 1) return seedNode;128 129 bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth , maxFunctionDefinitions, maxFunctionArguments);113 if (targetTreeLength <= 1 || maxDepth <= 1) return; 114 115 bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth); 130 116 131 117 // if successful => check constraints and return the tree if everything looks ok 132 118 if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) { 133 return seedNode;119 return; 134 120 } else { 135 121 // clean seedNode … … 142 128 143 129 private static bool CreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar, 144 int targetLength, int maxDepth , int maxFunctionDefinitions, int maxFunctionArguments) {130 int targetLength, int maxDepth) { 145 131 try { 146 TryCreateFullTreeFromSeed(random, root, globalGrammar, targetLength, maxDepth , maxFunctionDefinitions, maxFunctionArguments);132 TryCreateFullTreeFromSeed(random, root, globalGrammar, targetLength, maxDepth); 147 133 return true; 148 134 } … … 151 137 152 138 private static void TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar, 153 int targetLength, int maxDepth , int maxFunctionDefinitions, int maxFunctionArguments) {139 int targetLength, int maxDepth) { 154 140 List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>(); 155 141 int currentLength = 1; 156 int totalListMinLength = globalGrammar.GetMin ExpressionLength(root.Symbol) - 1;142 int totalListMinLength = globalGrammar.GetMinimumExpressionLength(root.Symbol) - 1; 157 143 int actualArity = SampleArity(random, root, targetLength); 158 144 for (int i = 0; i < actualArity; i++) { … … 170 156 int argumentIndex = nextExtension.ChildIndex; 171 157 int extensionDepth = nextExtension.ExtensionPointDepth; 172 if (extensionDepth + parent.Grammar.GetMin ExpressionDepth(parent.Symbol) >= maxDepth) {173 ReplaceWithMinimalTree(random, root, parent, argumentIndex , maxFunctionDefinitions, maxFunctionArguments);158 if (extensionDepth + parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth) { 159 ReplaceWithMinimalTree(random, root, parent, argumentIndex); 174 160 } else { 175 161 var allowedSymbols = (from s in parent.Grammar.Symbols 176 where parent.Grammar.IsAllowedChild (parent.Symbol, s, argumentIndex)177 where parent.Grammar.GetMin ExpressionDepth(s) + extensionDepth - 1 < maxDepth178 where parent.Grammar.GetMax ExpressionLength(s) > targetLength - totalListMinLength - currentLength162 where parent.Grammar.IsAllowedChildSymbol(parent.Symbol, s, argumentIndex) 163 where parent.Grammar.GetMinimumExpressionDepth(s) + extensionDepth - 1 < maxDepth 164 where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength 179 165 select s) 180 166 .ToList(); … … 186 172 parent.InsertSubTree(argumentIndex, newTree); 187 173 188 InitializeNewTreeNode(random, root, newTree, maxFunctionDefinitions, maxFunctionArguments); 174 var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode; 175 if (topLevelNode != null) 176 topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone()); 189 177 190 178 currentLength++; … … 198 186 extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 }); 199 187 } 200 totalListMinLength += newTree.Grammar.GetMin ExpressionLength(newTree.Symbol);188 totalListMinLength += newTree.Grammar.GetMinimumExpressionLength(newTree.Symbol); 201 189 } 202 190 } … … 209 197 int a = nextExtension.ChildIndex; 210 198 int d = nextExtension.ExtensionPointDepth; 211 ReplaceWithMinimalTree(random, root, parent, a , maxFunctionDefinitions, maxFunctionArguments);199 ReplaceWithMinimalTree(random, root, parent, a); 212 200 } 213 201 } 214 202 215 203 private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent, 216 int argumentIndex, int maxFunctionDefinitions, int maxFunctionArguments) {204 int childIndex) { 217 205 // determine possible symbols that will lead to the smallest possible tree 218 var possibleSymbols = (from s in parent.Grammar.GetAllowed Symbols(parent.Symbol, argumentIndex)219 group s by parent.Grammar.GetMin ExpressionLength(s) into g206 var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex) 207 group s by parent.Grammar.GetMinimumExpressionLength(s) into g 220 208 orderby g.Key 221 209 select g).First().ToList(); … … 224 212 var tree = selectedSymbol.CreateTreeNode(); 225 213 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); 226 parent.RemoveSubTree(argumentIndex); 227 parent.InsertSubTree(argumentIndex, tree); 228 InitializeNewTreeNode(random, root, tree, maxFunctionDefinitions, maxFunctionArguments); 229 for (int i = 0; i < tree.Grammar.GetMinSubtreeCount(tree.Symbol); i++) { 214 parent.RemoveSubTree(childIndex); 215 parent.InsertSubTree(childIndex, tree); 216 217 var topLevelNode = tree as SymbolicExpressionTreeTopLevelNode; 218 if (topLevelNode != null) 219 topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone()); 220 221 for (int i = 0; i < tree.Grammar.GetMinimumSubtreeCount(tree.Symbol); i++) { 230 222 // insert a dummy sub-tree and add the pending extension to the list 231 223 var dummy = new SymbolicExpressionTreeNode(); 232 224 tree.AddSubTree(dummy); 233 225 // replace the just inserted dummy by recursive application 234 ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments); 235 } 236 } 237 238 private static void InitializeNewTreeNode(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) { 239 // NB it is assumed that defuns are only allowed as children of root and nowhere else 240 // also assumes that newTree is already attached to root somewhere 241 if (IsTopLevelBranch(root, newTree)) { 242 ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone()); 243 244 // allow invokes of existing ADFs with higher index 245 int argIndex = root.IndexOfSubTree(newTree); 246 for (int i = argIndex + 1; i < root.SubTrees.Count(); i++) { 247 var otherDefunNode = root.GetSubTree(i) as DefunTreeNode; 248 if (otherDefunNode != null) { 249 GrammarModifier.AddDynamicSymbol(newTree.Grammar, newTree.Symbol, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments); 250 } 251 } 252 } 253 if (newTree.Symbol is Defun) { 254 var defunTree = newTree as DefunTreeNode; 255 string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ### 256 var allowedNames = from index in Enumerable.Range(0, maxFunctionDefinitions) 257 select "ADF" + index.ToString(formatString); 258 var takenNames = (from node in root.IterateNodesPrefix().OfType<DefunTreeNode>() 259 select node.FunctionName).Distinct(); 260 var remainingNames = allowedNames.Except(takenNames).ToList(); 261 string functionName = remainingNames[random.Next(remainingNames.Count)]; 262 // set name and number of arguments of the ADF 263 int nArgs = random.Next(maxFunctionArguments); 264 defunTree.FunctionName = functionName; 265 defunTree.NumberOfArguments = nArgs; 266 if (nArgs > 0) { 267 GrammarModifier.AddDynamicArguments(defunTree.Grammar, defunTree.Symbol, Enumerable.Range(0, nArgs)); 268 } 269 // in existing branches with smaller index allow invoke of current function 270 int argIndex = root.IndexOfSubTree(newTree); 271 for (int i = 0; i < argIndex; i++) { 272 // if not dummy node 273 if (root.GetSubTree(i).Symbol != null) { 274 var existingBranch = root.GetSubTree(i); 275 GrammarModifier.AddDynamicSymbol(existingBranch.Grammar, existingBranch.Symbol, functionName, nArgs); 276 } 277 } 226 ReplaceWithMinimalTree(random, root, tree, i); 278 227 } 279 228 } … … 285 234 private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) { 286 235 // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough 287 int minArity = node.Grammar.GetMin SubtreeCount(node.Symbol);288 int maxArity = node.Grammar.GetMax SubtreeCount(node.Symbol);236 int minArity = node.Grammar.GetMinimumSubtreeCount(node.Symbol); 237 int maxArity = node.Grammar.GetMaximumSubtreeCount(node.Symbol); 289 238 if (maxArity > targetLength) { 290 239 maxArity = targetLength; … … 294 243 long aggregatedLongestExpressionLength = 0; 295 244 for (int i = 0; i < maxArity; i++) { 296 aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowed Symbols(node.Symbol, i)297 select node.Grammar.GetMax ExpressionLength(s)).Max();245 aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i) 246 select node.Grammar.GetMaximumExpressionLength(s)).Max(); 298 247 if (aggregatedLongestExpressionLength < targetLength) minArity = i; 299 248 else break; … … 304 253 long aggregatedShortestExpressionLength = 0; 305 254 for (int i = 0; i < maxArity; i++) { 306 aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowed Symbols(node.Symbol, i)307 select node.Grammar.GetMin ExpressionLength(s)).Min();255 aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedChildSymbols(node.Symbol, i) 256 select node.Grammar.GetMinimumExpressionLength(s)).Min(); 308 257 if (aggregatedShortestExpressionLength > targetLength) { 309 258 maxArity = i;
Note: See TracChangeset
for help on using the changeset viewer.