Changeset 5510 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators
- Timestamp:
- 02/17/11 13:51:04 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators
- Files:
-
- 8 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)); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDeleter.cs
r5499 r5510 30 30 /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 112 31 31 /// </summary> 32 [Item("ArgumentDeleter", "Manipulates a symbolic expression by deleting an argument from an existing function defining branch. ")]32 [Item("ArgumentDeleter", "Manipulates a symbolic expression by deleting an argument from an existing function defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 112")] 33 33 [StorableClass] 34 34 public sealed class ArgumentDeleter : SymbolicExpressionTreeArchitectureManipulator { … … 40 40 public override sealed void ModifyArchitecture( 41 41 IRandom random, 42 SymbolicExpressionTree symbolicExpressionTree, 43 ISymbolicExpressionGrammar grammar, 44 IntValue maxTreeSize, IntValue maxTreeHeight, 45 IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, 46 out bool success) { 47 success = DeleteArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value); 42 ISymbolicExpressionTree symbolicExpressionTree, 43 IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) { 44 DeleteArgument(random, symbolicExpressionTree, maxFunctionDefinitions.Value, maxFunctionArguments.Value); 48 45 } 49 46 … … 54 51 public static bool DeleteArgument( 55 52 IRandom random, 56 SymbolicExpressionTree symbolicExpressionTree, 57 ISymbolicExpressionGrammar grammar, 58 int maxTreeSize, int maxTreeHeight, 59 int maxFunctionDefiningBranches, int maxFunctionArguments) { 53 ISymbolicExpressionTree symbolicExpressionTree, 54 int maxFunctionDefinitions, int maxFunctionArguments) { 60 55 61 56 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>(); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDuplicater.cs
r5499 r5510 33 33 /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 94 34 34 /// </summary> 35 [Item("ArgumentDuplicater", "Manipulates a symbolic expression by duplicating an existing argument node of a function-defining branch. ")]35 [Item("ArgumentDuplicater", "Manipulates a symbolic expression by duplicating an existing argument node of a function-defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 94")] 36 36 [StorableClass] 37 37 public sealed class ArgumentDuplicater : SymbolicExpressionTreeArchitectureManipulator { … … 43 43 public override sealed void ModifyArchitecture( 44 44 IRandom random, 45 SymbolicExpressionTree symbolicExpressionTree, 46 ISymbolicExpressionGrammar grammar, 47 IntValue maxTreeSize, IntValue maxTreeHeight, 48 IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, 49 out bool success) { 50 success = DuplicateArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value); 45 ISymbolicExpressionTree symbolicExpressionTree, 46 IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) { 47 DuplicateArgument(random, symbolicExpressionTree, maxFunctionDefinitions.Value, maxFunctionArguments.Value); 51 48 } 52 49 … … 57 54 public static bool DuplicateArgument( 58 55 IRandom random, 59 SymbolicExpressionTree symbolicExpressionTree, 60 ISymbolicExpressionGrammar grammar, 61 int maxTreeSize, int maxTreeHeight, 62 int maxFunctionDefiningBranches, int maxFunctionArguments) { 56 ISymbolicExpressionTree symbolicExpressionTree, 57 int maxFunctionDefinitions, int maxFunctionArguments) { 63 58 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>(); 64 59 … … 91 86 var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>() 92 87 where node.Symbol.FunctionName == selectedDefunBranch.FunctionName 93 where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments88 where node.SubTrees.Count() == selectedDefunBranch.NumberOfArguments 94 89 select node).ToList(); 95 90 // do this repeatedly until no matching invocations are found 96 91 while (invocationNodes.Count() > 0) { 97 List< SymbolicExpressionTreeNode> newlyAddedBranches = new List<SymbolicExpressionTreeNode>();92 List<ISymbolicExpressionTreeNode> newlyAddedBranches = new List<ISymbolicExpressionTreeNode>(); 98 93 foreach (var invokeNode in invocationNodes) { 99 94 // check that the invocation node really has the correct number of arguments 100 if (invokeNode.SubTrees.Count != selectedDefunBranch.NumberOfArguments) throw new InvalidOperationException();101 var argumentBranch = invokeNode. SubTrees[selectedArgumentSymbol.ArgumentIndex];102 var clonedArgumentBranch = ( SymbolicExpressionTreeNode)argumentBranch.Clone();95 if (invokeNode.SubTrees.Count() != selectedDefunBranch.NumberOfArguments) throw new InvalidOperationException(); 96 var argumentBranch = invokeNode.GetSubTree(selectedArgumentSymbol.ArgumentIndex); 97 var clonedArgumentBranch = (ISymbolicExpressionTreeNode)argumentBranch.Clone(); 103 98 invokeNode.InsertSubTree(newArgumentIndex, clonedArgumentBranch); 104 99 newlyAddedBranches.Add(clonedArgumentBranch); … … 107 102 from node in newlyAddedBranch.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>() 108 103 where node.Symbol.FunctionName == selectedDefunBranch.FunctionName 109 where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments104 where node.SubTrees.Count() == selectedDefunBranch.NumberOfArguments 110 105 select node).ToList(); 111 106 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/MultiSymbolicExpressionTreeArchitectureManipulator.cs
r5499 r5510 35 35 [Item("MultiSymbolicExpressionTreeArchitectureManipulator", "Randomly selects and applies one of its architecture manipulators every time it is called.")] 36 36 [StorableClass] 37 public sealed class MultiSymbolicExpressionTreeArchitectureManipulator : StochasticMultiBranch<ISymbolicExpressionTreeArchitectureManipulator>, ISymbolicExpressionTreeArchitectureManipulator, IStochasticOperator { 38 private const string MaxTreeSizeParameterName = "MaxTreeSize"; 39 private const string MaxTreeHeightParameterName = "MaxTreeHeight"; 40 private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar"; 37 public sealed class MultiSymbolicExpressionTreeArchitectureManipulator : StochasticMultiBranch<ISymbolicExpressionTreeArchitectureManipulator>, 38 ISymbolicExpressionTreeArchitectureManipulator, 39 ISymbolicExpressionTreeSizeConstraintOperator, 40 IStochasticOperator { 41 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 42 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 41 43 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 42 private const string Max FunctionArgumentsParameterName = "MaxFunctionArguments";43 private const string Max FunctionDefiningBranchesParameterName = "MaxFunctionDefiningBranches";44 private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments"; 45 private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions"; 44 46 45 47 public override bool CanChangeName { … … 49 51 get { return true; } 50 52 } 51 #region ISymbolicExpressionTreeArchitectureManipulator Members52 public I ValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter {53 get { return (I ValueLookupParameter<IntValue>)Parameters[MaxFunctionDefiningBranchesParameterName]; }53 #region Parameter properties 54 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 55 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 54 56 } 55 public IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter { 56 get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; } 57 public IValueLookupParameter<IntValue> MaximumFunctionDefinitionsParameter { 58 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; } 59 } 60 public IValueLookupParameter<IntValue> MaximumFunctionArgumentsParameter { 61 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; } 62 } 63 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 64 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 65 } 66 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 67 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 57 68 } 58 69 #endregion 59 60 #region ISymbolicExpressionTreeManipulator Members 61 public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter { 62 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 70 #region Parameter Properties 71 public IntValue MaximumFunctionDefinitions { 72 get { return MaximumFunctionDefinitionsParameter.ActualValue; } 63 73 } 64 #endregion 65 66 #region ISymbolicExpressionTreeOperator Members 67 public IValueLookupParameter<IntValue> MaxTreeSizeParameter { 68 get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; } 74 public IntValue MaximumFunctionArguments { 75 get { return MaximumFunctionArgumentsParameter.ActualValue; } 69 76 } 70 public I ValueLookupParameter<IntValue> MaxTreeHeightParameter{71 get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }77 public IntValue MaximumSymbolicExpressionTreeLength { 78 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 72 79 } 73 public I LookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter{74 get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }80 public IntValue MaximumSymbolicExpressionTreeDepth { 81 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 75 82 } 76 83 #endregion … … 82 89 public MultiSymbolicExpressionTreeArchitectureManipulator() 83 90 : base() { 84 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 85 Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionDefiningBranchesParameterName, "The maximal allowed number of function defining branches.")); 86 Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionArgumentsParameterName, "The maximal allowed number of arguments of a newly created function.")); 87 Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree.")); 88 Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0).")); 89 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees.")); 91 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 92 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionDefinitionsParameterName, "The maximal allowed number of automatically defined functions.")); 93 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionArgumentsParameterName, "The maximal allowed number of arguments of a automatically defined functions.")); 94 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 95 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 90 96 91 97 foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeArchitectureManipulator))) { … … 111 117 private void ParameterizeManipulators() { 112 118 foreach (ISymbolicExpressionTreeArchitectureManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) { 113 manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name; 114 manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name; 115 manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name; 119 manipulator.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name; 120 manipulator.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name; 121 } 122 foreach(ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) { 123 manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name; 124 manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name; 125 } 126 foreach(ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) { 116 127 manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name; 117 manipulator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;118 manipulator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;119 128 } 120 121 129 foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) { 122 130 manipulator.RandomParameter.ActualName = RandomParameter.Name; 123 131 } 124 132 } 125 126 #region ISymbolicExpressionTreeArchitectureManipulator Members127 public void ModifyArchitecture(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, out bool success) {128 var op = Operators.SelectRandom(random);129 op.ModifyArchitecture(random, symbolicExpressionTree, grammar, maxTreeSize, maxTreeHeight, maxFunctionDefiningBranches, maxFunctionArguments, out success);130 }131 #endregion132 133 } 133 134 } -
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 -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDeleter.cs
r5499 r5510 33 33 /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 108 34 34 /// </summary> 35 [Item("SubroutineDeleter", "Manipulates a symbolic expression by deleting a preexisting function-defining branch. ")]35 [Item("SubroutineDeleter", "Manipulates a symbolic expression by deleting a preexisting function-defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 108")] 36 36 [StorableClass] 37 37 public sealed class SubroutineDeleter : SymbolicExpressionTreeArchitectureManipulator { … … 47 47 public override sealed void ModifyArchitecture( 48 48 IRandom random, 49 SymbolicExpressionTree symbolicExpressionTree, 50 ISymbolicExpressionGrammar grammar, 51 IntValue maxTreeSize, IntValue maxTreeHeight, 52 IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, 53 out bool success) { 54 success = DeleteSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value); 49 ISymbolicExpressionTree symbolicExpressionTree, 50 IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) { 51 DeleteSubroutine(random, symbolicExpressionTree, maxFunctionDefinitions.Value, maxFunctionArguments.Value); 55 52 } 56 53 57 54 public static bool DeleteSubroutine( 58 55 IRandom random, 59 SymbolicExpressionTree symbolicExpressionTree, 60 ISymbolicExpressionGrammar grammar, 61 int maxTreeSize, int maxTreeHeight, 62 int maxFunctionDefiningBranches, int maxFunctionArguments) { 56 ISymbolicExpressionTree symbolicExpressionTree, 57 int maxFunctionDefinitions, int maxFunctionArguments) { 63 58 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>(); 64 59 … … 68 63 var selectedDefunBranch = functionDefiningBranches.SelectRandom(random); 69 64 // remove the selected defun 70 int defunSubtreeIndex = symbolicExpressionTree.Root. SubTrees.IndexOf(selectedDefunBranch);65 int defunSubtreeIndex = symbolicExpressionTree.Root.IndexOfSubTree(selectedDefunBranch); 71 66 symbolicExpressionTree.Root.RemoveSubTree(defunSubtreeIndex); 72 67 … … 85 80 } 86 81 87 private static void DeletionByRandomRegeneration(IRandom random, SymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch) {82 private static void DeletionByRandomRegeneration(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch) { 88 83 // find first invocation and replace it with a randomly generated tree 89 84 // can't find all invocations in one step because once we replaced a top level invocation … … 92 87 from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>() 93 88 where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName 94 select new { Parent = node, ReplacedChildIndex = node. SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();89 select new { Parent = node, ReplacedChildIndex = node.IndexOfSubTree(subtree), ReplacedChild = subtree }).FirstOrDefault(); 95 90 while (invocationCutPoint != null) { 96 91 // deletion by random regeneration 97 SymbolicExpressionTreeNode replacementTree = null;92 ISymbolicExpressionTreeNode replacementTree = null; 98 93 var allowedSymbolsList = invocationCutPoint.Parent.GetAllowedSymbols(invocationCutPoint.ReplacedChildIndex).ToList(); 99 94 var weights = allowedSymbolsList.Select(s => s.InitialFrequency); … … 115 110 from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>() 116 111 where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName 117 select new { Parent = node, ReplacedChildIndex = node. SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();112 select new { Parent = node, ReplacedChildIndex = node.IndexOfSubTree(subtree), ReplacedChild = subtree }).FirstOrDefault(); 118 113 } 119 114 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs
r5499 r5510 34 34 /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 88 35 35 /// </summary> 36 [Item("SubroutineDuplicater", "Manipulates a symbolic expression by duplicating a preexisting function-defining branch. ")]36 [Item("SubroutineDuplicater", "Manipulates a symbolic expression by duplicating a preexisting function-defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 88")] 37 37 [StorableClass] 38 38 public sealed class SubroutineDuplicater : SymbolicExpressionTreeArchitectureManipulator { … … 50 50 public override sealed void ModifyArchitecture( 51 51 IRandom random, 52 SymbolicExpressionTree symbolicExpressionTree, 53 ISymbolicExpressionGrammar grammar, 54 IntValue maxTreeSize, IntValue maxTreeHeight, 55 IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, 56 out bool success) { 57 success = DuplicateSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value); 52 ISymbolicExpressionTree symbolicExpressionTree, 53 IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) { 54 DuplicateSubroutine(random, symbolicExpressionTree, maxFunctionDefinitions.Value, maxFunctionArguments.Value); 58 55 } 59 56 60 57 public static bool DuplicateSubroutine( 61 58 IRandom random, 62 SymbolicExpressionTree symbolicExpressionTree, 63 ISymbolicExpressionGrammar grammar, 64 int maxTreeSize, int maxTreeHeight, 65 int maxFunctionDefiningBranches, int maxFunctionArguments) { 59 ISymbolicExpressionTree symbolicExpressionTree, 60 int maxFunctionDefinitions, int maxFunctionArguments) { 66 61 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>(); 67 if (functionDefiningBranches.Count() == 0 || functionDefiningBranches.Count() == maxFunctionDefini ngBranches)62 if (functionDefiningBranches.Count() == 0 || functionDefiningBranches.Count() == maxFunctionDefinitions) 68 63 // no function defining branches to duplicate or already reached the max number of ADFs 69 64 return false; 70 65 71 string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefini ngBranches) + 1).ToString(); // >= 100 functions => ###72 var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefini ngBranches)66 string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions) + 1).ToString(); // >= 100 functions => ### 67 var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions) 73 68 select "ADF" + index.ToString(formatString); 74 69 var selectedBranch = functionDefiningBranches.SelectRandom(random); … … 77 72 duplicatedDefunBranch.FunctionName = newFunctionName; 78 73 symbolicExpressionTree.Root.AddSubTree(duplicatedDefunBranch); 79 duplicatedDefunBranch.SetGrammar((ISymbolicExpression Grammar)selectedBranch.Grammar.Clone());74 duplicatedDefunBranch.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBranch.Grammar.Clone()); 80 75 // add an invoke symbol for each branch that is allowed to invoke the original function 81 76 foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) { … … 104 99 } 105 100 106 private static IEnumerable<string> UsedFunctionNames( SymbolicExpressionTree symbolicExpressionTree) {101 private static IEnumerable<string> UsedFunctionNames(ISymbolicExpressionTree symbolicExpressionTree) { 107 102 return from node in symbolicExpressionTree.IterateNodesPrefix() 108 103 where node.Symbol is Defun -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SymbolicExpressionTreeArchitectureManipulator.cs
r5499 r5510 62 62 } 63 63 64 protected override sealed void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree) {64 protected override sealed void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { 65 65 ModifyArchitecture(random, symbolicExpressionTree, MaximumFunctionDefinitions, MaximumFunctionArguments); 66 66 } … … 68 68 public abstract void ModifyArchitecture( 69 69 IRandom random, 70 SymbolicExpressionTree tree,70 ISymbolicExpressionTree tree, 71 71 IntValue maxFunctionDefinitions, 72 72 IntValue maxFunctionArguments);
Note: See TracChangeset
for help on using the changeset viewer.