Changeset 5510 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs
- Timestamp:
- 02/17/11 13:51:04 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs
r5499 r5510 28 28 using HeuristicLab.Data; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Parameters; 30 31 31 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 35 36 /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 97 36 37 /// </summary> 37 [Item("SubroutineCreater", "Manipulates a symbolic expression by adding one new function-defining branch containing a proportion of a preexisting branch and by creating a reference to the new branch. ")]38 [Item("SubroutineCreater", "Manipulates a symbolic expression by adding one new function-defining branch containing a proportion of a preexisting branch and by creating a reference to the new branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 97")] 38 39 [StorableClass] 39 public sealed class SubroutineCreater : SymbolicExpressionTreeArchitectureManipulator {40 public sealed class SubroutineCreater : SymbolicExpressionTreeArchitectureManipulator, ISymbolicExpressionTreeSizeConstraintOperator { 40 41 private const double ARGUMENT_CUTOFF_PROBABILITY = 0.05; 41 42 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 43 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 44 #region Parameter Properties 45 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 46 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 47 } 48 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 49 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 50 } 51 #endregion 52 #region Properties 53 public IntValue MaximumSymbolicExpressionTreeLength { 54 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 55 } 56 public IntValue MaximumSymbolicExpressionTreeDepth { 57 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 58 } 59 #endregion 42 60 [StorableConstructor] 43 61 private SubroutineCreater(bool deserializing) : base(deserializing) { } 44 62 private SubroutineCreater(SubroutineCreater original, Cloner cloner) : base(original, cloner) { } 45 public SubroutineCreater() : base() { } 63 public SubroutineCreater() : base() { 64 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 65 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 66 } 46 67 47 68 public override IDeepCloneable Clone(Cloner cloner) { … … 51 72 public override sealed void ModifyArchitecture( 52 73 IRandom random, 53 SymbolicExpressionTree symbolicExpressionTree, 54 ISymbolicExpressionGrammar grammar, 55 IntValue maxTreeSize, IntValue maxTreeHeight, 56 IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, 57 out bool success) { 58 success = CreateSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value); 74 ISymbolicExpressionTree symbolicExpressionTree, 75 IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) { 76 CreateSubroutine(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value, maxFunctionDefinitions.Value, maxFunctionArguments.Value); 59 77 } 60 78 61 79 public static bool CreateSubroutine( 62 80 IRandom random, 63 SymbolicExpressionTree symbolicExpressionTree, 64 ISymbolicExpressionGrammar grammar, 65 int maxTreeSize, int maxTreeHeight, 66 int maxFunctionDefiningBranches, int maxFunctionArguments) { 81 ISymbolicExpressionTree symbolicExpressionTree, 82 int maxTreeLength, int maxTreeDepth, 83 int maxFunctionDefinitions, int maxFunctionArguments) { 67 84 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>(); 68 if (functionDefiningBranches.Count() >= maxFunctionDefini ngBranches)85 if (functionDefiningBranches.Count() >= maxFunctionDefinitions) 69 86 // allowed maximum number of ADF reached => abort 70 87 return false; 71 if (symbolicExpressionTree.Size + 4 > maxTree Size)88 if (symbolicExpressionTree.Size + 4 > maxTreeLength) 72 89 // defining a new function causes an size increase by 4 nodes (max) if the max tree size is reached => abort 73 90 return false; 74 string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefini ngBranches * 10 - 1)).ToString(); // >= 100 functions => ###75 var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefini ngBranches)91 string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ### 92 var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions) 76 93 select "ADF" + index.ToString(formatString); 77 94 … … 82 99 int r = random.Next(totalNumberOfBodyNodes); 83 100 int aggregatedNumberOfBodyNodes = 0; 84 SymbolicExpressionTreeNode selectedBody = null;101 ISymbolicExpressionTreeNode selectedBody = null; 85 102 foreach (var body in bodies) { 86 103 aggregatedNumberOfBodyNodes += body.Size; … … 94 111 var allCutPoints = from parent in selectedBody.IterateNodesPrefix() 95 112 from subtree in parent.SubTrees 96 select new { Parent = parent, ReplacedBranchIndex = parent. SubTrees.IndexOf(subtree), ReplacedBranch = subtree };113 select new { Parent = parent, ReplacedBranchIndex = parent.IndexOfSubTree(subtree), ReplacedBranch = subtree }; 97 114 if (allCutPoints.Count() == 0) 98 115 // no cut points => abort … … 101 118 var selectedCutPoint = allCutPoints.SelectRandom(random); 102 119 // select random branches as argument cut-off points (replaced by argument terminal nodes in the function) 103 List< SymbolicExpressionTreeNode> argumentBranches = SelectRandomArgumentBranches(selectedCutPoint.ReplacedBranch, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments);104 SymbolicExpressionTreeNode functionBody = selectedCutPoint.ReplacedBranch;120 List<ISymbolicExpressionTreeNode> argumentBranches = SelectRandomArgumentBranches(selectedCutPoint.ReplacedBranch, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments); 121 ISymbolicExpressionTreeNode functionBody = selectedCutPoint.ReplacedBranch; 105 122 // disconnect the function body from the tree 106 123 selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint.ReplacedBranchIndex); … … 120 137 symbolicExpressionTree.Root.AddSubTree(defunNode); 121 138 // the grammar in the newly defined function is a clone of the grammar of the originating branch 122 defunNode.SetGrammar((ISymbolicExpression Grammar)selectedBody.Grammar.Clone());139 defunNode.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBody.Grammar.Clone()); 123 140 // remove all argument symbols from grammar 124 141 var oldArgumentSymbols = defunNode.Grammar.Symbols.OfType<Argument>().ToList(); … … 164 181 } 165 182 166 private static SymbolicExpressionTreeNode DisconnectBranches(SymbolicExpressionTreeNode node, List<SymbolicExpressionTreeNode> argumentBranches) {183 private static ISymbolicExpressionTreeNode DisconnectBranches(ISymbolicExpressionTreeNode node, List<ISymbolicExpressionTreeNode> argumentBranches) { 167 184 if (argumentBranches.Contains(node)) { 168 185 var argumentIndex = argumentBranches.IndexOf(node); … … 171 188 } 172 189 // remove the subtrees so that we can clone only the root node 173 List< SymbolicExpressionTreeNode> subtrees = new List<SymbolicExpressionTreeNode>(node.SubTrees);174 while (node.SubTrees.Count > 0) node.RemoveSubTree(0);190 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.SubTrees); 191 while (node.SubTrees.Count() > 0) node.RemoveSubTree(0); 175 192 // recursively apply function for subtrees or append a argument terminal node 176 193 foreach (var subtree in subtrees) { … … 180 197 } 181 198 182 private static List< SymbolicExpressionTreeNode> SelectRandomArgumentBranches(SymbolicExpressionTreeNode selectedRoot,199 private static List<ISymbolicExpressionTreeNode> SelectRandomArgumentBranches(ISymbolicExpressionTreeNode selectedRoot, 183 200 IRandom random, 184 201 double cutProbability, … … 186 203 // breadth first determination of argument cut-off points 187 204 // we must make sure that we cut off all original argument nodes and that the number of new argument is smaller than the limit 188 List< SymbolicExpressionTreeNode> argumentBranches = new List<SymbolicExpressionTreeNode>();205 List<ISymbolicExpressionTreeNode> argumentBranches = new List<ISymbolicExpressionTreeNode>(); 189 206 if (selectedRoot is ArgumentTreeNode) { 190 207 argumentBranches.Add(selectedRoot); … … 202 219 } 203 220 // cut-off in the sub-trees in random order 204 var randomIndexes = (from index in Enumerable.Range(0, selectedRoot.SubTrees.Count )221 var randomIndexes = (from index in Enumerable.Range(0, selectedRoot.SubTrees.Count()) 205 222 select new { Index = index, OrderValue = random.NextDouble() }).OrderBy(x => x.OrderValue).Select(x => x.Index); 206 223 foreach (var subtreeIndex in randomIndexes) { 207 var subtree = selectedRoot. SubTrees[subtreeIndex];224 var subtree = selectedRoot.GetSubTree(subtreeIndex); 208 225 minNewArgumentsForSubtrees[subtreeIndex] = 0; 209 226 // => cut-off at 0..n points somewhere in the current sub-tree
Note: See TracChangeset
for help on using the changeset viewer.