Changeset 5510 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs
- Timestamp:
- 02/17/11 13:51:04 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs
r5499 r5510 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Parameters; 29 30 30 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 33 34 /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 106 34 35 /// </summary> 35 [Item("ArgumentCreater", "Manipulates a symbolic expression by creating a new argument within one function-defining branch. ")]36 [Item("ArgumentCreater", "Manipulates a symbolic expression by creating a new argument within one function-defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 106")] 36 37 [StorableClass] 37 public sealed class ArgumentCreater : SymbolicExpressionTreeArchitectureManipulator { 38 public sealed class ArgumentCreater : SymbolicExpressionTreeArchitectureManipulator, ISymbolicExpressionTreeSizeConstraintOperator { 39 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 40 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 41 #region Parameter Properties 42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 44 } 45 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 46 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 47 } 48 #endregion 49 #region Properties 50 public IntValue MaximumSymbolicExpressionTreeLength { 51 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 52 } 53 public IntValue MaximumSymbolicExpressionTreeDepth { 54 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 55 } 56 #endregion 38 57 [StorableConstructor] 39 58 private ArgumentCreater(bool deserializing) : base(deserializing) { } 40 59 private ArgumentCreater(ArgumentCreater original, Cloner cloner) : base(original, cloner) { } 41 public ArgumentCreater() : base() { } 60 public ArgumentCreater() 61 : base() { 62 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 63 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 64 } 65 42 66 public override sealed void ModifyArchitecture( 43 67 IRandom random, 44 SymbolicExpressionTree symbolicExpressionTree, 45 ISymbolicExpressionGrammar grammar, 46 IntValue maxTreeSize, IntValue maxTreeHeight, 47 IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, 48 out bool success) { 49 success = CreateNewArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value); 68 ISymbolicExpressionTree symbolicExpressionTree, 69 IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) { 70 CreateNewArgument(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value, maxFunctionDefinitions.Value, maxFunctionArguments.Value); 50 71 } 51 72 … … 56 77 public static bool CreateNewArgument( 57 78 IRandom random, 58 SymbolicExpressionTree symbolicExpressionTree, 59 ISymbolicExpressionGrammar grammar, 60 int maxTreeSize, int maxTreeHeight, 61 int maxFunctionDefiningBranches, int maxFunctionArguments) { 79 ISymbolicExpressionTree symbolicExpressionTree, 80 int maxTreeLength, int maxTreeDepth, 81 int maxFunctionDefinitions, int maxFunctionArguments) { 62 82 // work on a copy in case we find out later that the tree would be too big 63 83 // in this case it's easiest to simply return the original tree. 64 SymbolicExpressionTree clonedTree = (SymbolicExpressionTree)symbolicExpressionTree.Clone();84 ISymbolicExpressionTree clonedTree = (ISymbolicExpressionTree)symbolicExpressionTree.Clone(); 65 85 66 86 var functionDefiningBranches = clonedTree.IterateNodesPrefix().OfType<DefunTreeNode>(); … … 83 103 // this operation potentially creates very big trees so the access to the size property might throw overflow exception 84 104 try { 85 if (CreateNewArgumentForDefun(random, clonedTree, selectedDefunBranch, newArgumentNode) && clonedTree.Size <= maxTree Size && clonedTree.Height <= maxTreeHeight) {105 if (CreateNewArgumentForDefun(random, clonedTree, selectedDefunBranch, newArgumentNode) && clonedTree.Size <= maxTreeLength && clonedTree.Height <= maxTreeDepth) { 86 106 87 107 // size constraints are fulfilled … … 100 120 } 101 121 102 private static bool CreateNewArgumentForDefun(IRandom random, SymbolicExpressionTree tree, DefunTreeNode defunBranch, ArgumentTreeNode newArgumentNode) {122 private static bool CreateNewArgumentForDefun(IRandom random, ISymbolicExpressionTree tree, DefunTreeNode defunBranch, ArgumentTreeNode newArgumentNode) { 103 123 // select a random cut point in the function defining branch 104 124 // the branch at the cut point is to be replaced by a new argument node 105 125 var cutPoints = (from node in defunBranch.IterateNodesPrefix() 106 where node.SubTrees.Count > 0126 where node.SubTrees.Count() > 0 107 127 from subtree in node.SubTrees 108 select new { Parent = node, ReplacedChildIndex = node. SubTrees.IndexOf(subtree), ReplacedChild = subtree }).ToList();128 select new { Parent = node, ReplacedChildIndex = node.IndexOfSubTree(subtree), ReplacedChild = subtree }).ToList(); 109 129 110 130 if (cutPoints.Count() == 0) … … 121 141 var invocationNodes = (from node in tree.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>() 122 142 where node.Symbol.FunctionName == defunBranch.FunctionName 123 where node.SubTrees.Count == defunBranch.NumberOfArguments143 where node.SubTrees.Count() == defunBranch.NumberOfArguments 124 144 select node).ToList(); 125 145 // do this repeatedly until no matching invocations are found 126 146 while (invocationNodes.Count > 0) { 127 List< SymbolicExpressionTreeNode> newlyAddedBranches = new List<SymbolicExpressionTreeNode>();147 List<ISymbolicExpressionTreeNode> newlyAddedBranches = new List<ISymbolicExpressionTreeNode>(); 128 148 foreach (var invocationNode in invocationNodes) { 129 149 // check that the invocation node really has the correct number of arguments 130 if (invocationNode.SubTrees.Count != defunBranch.NumberOfArguments) throw new InvalidOperationException();150 if (invocationNode.SubTrees.Count() != defunBranch.NumberOfArguments) throw new InvalidOperationException(); 131 151 // append a new argument branch after expanding all argument nodes 132 var clonedBranch = ( SymbolicExpressionTreeNode)replacedBranch.Clone();152 var clonedBranch = (ISymbolicExpressionTreeNode)replacedBranch.Clone(); 133 153 clonedBranch = ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees); 134 154 invocationNode.InsertSubTree(newArgumentNode.Symbol.ArgumentIndex, clonedBranch); … … 139 159 from node in newlyAddedBranch.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>() 140 160 where node.Symbol.FunctionName == defunBranch.FunctionName 141 where node.SubTrees.Count == defunBranch.NumberOfArguments161 where node.SubTrees.Count() == defunBranch.NumberOfArguments 142 162 select node).ToList(); 143 163 } … … 171 191 } 172 192 173 private static SymbolicExpressionTreeNode ReplaceArgumentsInBranch(SymbolicExpressionTreeNode branch, IList<SymbolicExpressionTreeNode> argumentTrees) {193 private static ISymbolicExpressionTreeNode ReplaceArgumentsInBranch(ISymbolicExpressionTreeNode branch, IEnumerable<ISymbolicExpressionTreeNode> argumentTrees) { 174 194 ArgumentTreeNode argNode = branch as ArgumentTreeNode; 175 195 if (argNode != null) { 176 196 // replace argument nodes by a clone of the original subtree that provided the result for the argument node 177 return (SymbolicExpressionTreeNode)argumentTrees [argNode.Symbol.ArgumentIndex].Clone();197 return (SymbolicExpressionTreeNode)argumentTrees.ElementAt(argNode.Symbol.ArgumentIndex).Clone(); 178 198 } else { 179 199 // call recursively for all subtree 180 List< SymbolicExpressionTreeNode> subtrees = new List<SymbolicExpressionTreeNode>(branch.SubTrees);181 while (branch.SubTrees.Count > 0) branch.RemoveSubTree(0);200 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(branch.SubTrees); 201 while (branch.SubTrees.Count() > 0) branch.RemoveSubTree(0); 182 202 foreach (var subtree in subtrees) { 183 203 branch.AddSubTree(ReplaceArgumentsInBranch(subtree, argumentTrees));
Note: See TracChangeset
for help on using the changeset viewer.