Changeset 5510
- Timestamp:
- 02/17/11 13:51:04 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 37 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/MinAverageMaxSymbolicExpressionTreeLengthAnalyzer.cs ¶
r5499 r5510 45 45 46 46 #region parameter properties 47 public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {48 get { return ( ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }47 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 48 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 49 49 } 50 50 public ScopeTreeLookupParameter<DoubleValue> SymbolicExpressionTreeLengthParameter { … … 73 73 public MinAverageMaxSymbolicExpressionTreeLengthAnalyzer() 74 74 : base() { 75 Parameters.Add(new ScopeTreeLookupParameter< SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated."));75 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated.")); 76 76 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeLengthParameterName, "The length of the symbolic expression tree.")); 77 77 Parameters.Add(new ValueLookupParameter<DataTable>(SymbolicExpressionTreeLengthsParameterName, "The data table to store the symbolic expression tree lengths.")); -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs ¶
r5499 r5510 42 42 43 43 #region parameter properties 44 public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {45 get { return ( ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }44 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 45 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 46 46 } 47 47 public ILookupParameter<DataTable> SymbolFrequenciesParameter { … … 64 64 public SymbolicExpressionSymbolFrequencyAnalyzer() 65 65 : base() { 66 Parameters.Add(new ScopeTreeLookupParameter< SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));66 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze.")); 67 67 Parameters.Add(new ValueLookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies.")); 68 68 Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the symbol frequencies should be stored.")); … … 73 73 74 74 public override IOperation Apply() { 75 ItemArray< SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;75 ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue; 76 76 ResultCollection results = ResultsParameter.ActualValue; 77 77 … … 102 102 } 103 103 104 public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable< SymbolicExpressionTree> trees) {104 public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees) { 105 105 Dictionary<string, double> symbolFrequencies = new Dictionary<string, double>(); 106 106 int totalNumberOfSymbols = 0; -
TabularUnified 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)); -
TabularUnified 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>(); -
TabularUnified 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 } -
TabularUnified 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 } -
TabularUnified 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 -
TabularUnified 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 } -
TabularUnified 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 -
TabularUnified 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); -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs ¶
r5499 r5510 43 43 if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue)."); 44 44 entryPoint[branch.FunctionName] = (ushort)code.Count; 45 code.AddRange(Compile(branch. SubTrees[0], opCodeMapper));45 code.AddRange(Compile(branch.GetSubTree(0), opCodeMapper)); 46 46 } 47 47 // address of all functions is fixed now -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs ¶
r5499 r5510 92 92 } 93 93 94 protected override SymbolicExpressionTree Create(IRandom random) {94 protected override ISymbolicExpressionTree Create(IRandom random) { 95 95 return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value, 96 96 MaximumFunctionDefinitions.Value, MaximumFunctionArguments.Value); 97 97 } 98 98 99 public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar,99 public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar, 100 100 int maxTreeSize, int maxTreeHeight, 101 101 int maxFunctionDefinitions, int maxFunctionArguments -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/SymbolicExpressionTreeCreator.cs ¶
r5499 r5510 35 35 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 36 36 #region Parameter Properties 37 public ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter {38 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }37 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 38 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 39 39 } 40 40 #endregion 41 41 42 42 #region Propeties 43 public SymbolicExpressionTree SymbolicExpressionTree {43 public ISymbolicExpressionTree SymbolicExpressionTree { 44 44 get { return SymbolicExpressionTreeParameter.ActualValue; } 45 45 set { SymbolicExpressionTreeParameter.ActualValue = value; } … … 52 52 protected SymbolicExpressionTreeCreator() 53 53 : base() { 54 Parameters.Add(new LookupParameter< SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree that should be created."));54 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree that should be created.")); 55 55 } 56 56 … … 60 60 } 61 61 62 protected abstract SymbolicExpressionTree Create(IRandom random);62 protected abstract ISymbolicExpressionTree Create(IRandom random); 63 63 } 64 64 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/CrossoverPoint.cs ¶
r5499 r5510 23 23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 24 24 internal class CrossoverPoint { 25 public SymbolicExpressionTreeNode Parent { get; set; }26 public SymbolicExpressionTreeNode Child { get; set; }25 public ISymbolicExpressionTreeNode Parent { get; set; } 26 public ISymbolicExpressionTreeNode Child { get; set; } 27 27 public int SubtreeIndex { 28 get { return Parent. SubTrees.IndexOf(Child); }28 get { return Parent.IndexOfSubTree(Child); } 29 29 } 30 public CrossoverPoint( SymbolicExpressionTreeNode parent,SymbolicExpressionTreeNode child) {30 public CrossoverPoint(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) { 31 31 this.Parent = parent; 32 32 this.Child = child; -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs ¶
r5499 r5510 78 78 } 79 79 80 protected override SymbolicExpressionTree Cross(IRandom random,81 SymbolicExpressionTree parent0,SymbolicExpressionTree parent1) {80 protected override ISymbolicExpressionTree Cross(IRandom random, 81 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 82 82 return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value, 83 83 MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 84 84 } 85 85 86 public static SymbolicExpressionTree Cross(IRandom random,87 SymbolicExpressionTree parent0,SymbolicExpressionTree parent1,86 public static ISymbolicExpressionTree Cross(IRandom random, 87 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 88 88 double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight) { 89 89 // select a random crossover point in the first parent 90 SymbolicExpressionTreeNode crossoverPoint0;90 ISymbolicExpressionTreeNode crossoverPoint0; 91 91 int replacedSubtreeIndex; 92 92 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeSize, maxTreeHeight, out crossoverPoint0, out replacedSubtreeIndex); 93 93 94 94 // calculate the max size and height that the inserted branch can have 95 int maxInsertedBranchSize = maxTreeSize - (parent0.Size - crossoverPoint0. SubTrees[replacedSubtreeIndex].GetSize());95 int maxInsertedBranchSize = maxTreeSize - (parent0.Size - crossoverPoint0.GetSubTree(replacedSubtreeIndex).GetSize()); 96 96 int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0); 97 97 98 List< SymbolicExpressionTreeNode> allowedBranches = new List<SymbolicExpressionTreeNode>();98 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 99 99 parent1.Root.ForEachNodePostfix((n) => { 100 100 if (n.GetSize() <= maxInsertedBranchSize && … … 117 117 } 118 118 119 private static bool IsMatchingPointType( SymbolicExpressionTreeNode parent, int replacedSubtreeIndex,SymbolicExpressionTreeNode branch) {119 private static bool IsMatchingPointType(ISymbolicExpressionTreeNode parent, int replacedSubtreeIndex, ISymbolicExpressionTreeNode branch) { 120 120 // check syntax constraints of direct parent - child relation 121 121 if (!parent.Grammar.ContainsSymbol(branch.Symbol) || … … 128 128 result && 129 129 parent.Grammar.ContainsSymbol(n.Symbol) && 130 n.SubTrees.Count >= parent.Grammar.GetMinSubtreeCount(n.Symbol) &&131 n.SubTrees.Count <= parent.Grammar.GetMaxSubtreeCount(n.Symbol);130 n.SubTrees.Count() >= parent.Grammar.GetMinSubtreeCount(n.Symbol) && 131 n.SubTrees.Count() <= parent.Grammar.GetMaxSubtreeCount(n.Symbol); 132 132 }); 133 133 return result; 134 134 } 135 135 136 private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, outSymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {136 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 137 137 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 138 138 List<CrossoverPoint> internalCrossoverPoints = new List<CrossoverPoint>(); 139 139 List<CrossoverPoint> leafCrossoverPoints = new List<CrossoverPoint>(); 140 140 parent0.Root.ForEachNodePostfix((n) => { 141 if (n.SubTrees.Count > 0 && n != parent0.Root) {141 if (n.SubTrees.Count() > 0 && n != parent0.Root) { 142 142 foreach (var child in n.SubTrees) { 143 143 if (child.GetSize() <= maxBranchSize && 144 144 child.GetHeight() <= maxBranchHeight) { 145 if (child.SubTrees.Count > 0)145 if (child.SubTrees.Count() > 0) 146 146 internalCrossoverPoints.Add(new CrossoverPoint(n, child)); 147 147 else … … 178 178 } 179 179 180 private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<SymbolicExpressionTreeNode> branches, double internalNodeProbability) {180 private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability) { 181 181 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 182 List< SymbolicExpressionTreeNode> allowedInternalBranches;183 List< SymbolicExpressionTreeNode> allowedLeafBranches;182 List<ISymbolicExpressionTreeNode> allowedInternalBranches; 183 List<ISymbolicExpressionTreeNode> allowedLeafBranches; 184 184 if (random.NextDouble() < internalNodeProbability) { 185 185 // select internal node if possible 186 186 allowedInternalBranches = (from branch in branches 187 where branch.SubTrees.Count > 0187 where branch.SubTrees.Count() > 0 188 188 select branch).ToList(); 189 189 if (allowedInternalBranches.Count > 0) { … … 192 192 // no internal nodes allowed => select leaf nodes 193 193 allowedLeafBranches = (from branch in branches 194 where branch.SubTrees.Count == 0194 where branch.SubTrees.Count() == 0 195 195 select branch).ToList(); 196 196 return allowedLeafBranches.SelectRandom(random); … … 199 199 // select leaf node if possible 200 200 allowedLeafBranches = (from branch in branches 201 where branch.SubTrees.Count == 0201 where branch.SubTrees.Count() == 0 202 202 select branch).ToList(); 203 203 if (allowedLeafBranches.Count > 0) { … … 205 205 } else { 206 206 allowedInternalBranches = (from branch in branches 207 where branch.SubTrees.Count > 0207 where branch.SubTrees.Count() > 0 208 208 select branch).ToList(); 209 209 return allowedInternalBranches.SelectRandom(random); -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs ¶
r5499 r5510 37 37 private const string ChildParameterName = "Child"; 38 38 #region Parameter Properties 39 public ILookupParameter<ItemArray< SymbolicExpressionTree>> ParentsParameter {40 get { return (ScopeTreeLookupParameter< SymbolicExpressionTree>)Parameters[ParentsParameterName]; }39 public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter { 40 get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; } 41 41 } 42 public ILookupParameter< SymbolicExpressionTree> ChildParameter {43 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters[ChildParameterName]; }42 public ILookupParameter<ISymbolicExpressionTree> ChildParameter { 43 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; } 44 44 } 45 45 #endregion 46 46 #region Properties 47 public ItemArray< SymbolicExpressionTree> Parents {47 public ItemArray<ISymbolicExpressionTree> Parents { 48 48 get { return ParentsParameter.ActualValue; } 49 49 } 50 public SymbolicExpressionTree Child {50 public ISymbolicExpressionTree Child { 51 51 get { return ChildParameter.ActualValue; } 52 52 set { ChildParameter.ActualValue = value; } … … 58 58 protected SymbolicExpressionTreeCrossover() 59 59 : base() { 60 Parameters.Add(new ScopeTreeLookupParameter< SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));61 Parameters.Add(new LookupParameter< SymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));60 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed.")); 61 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover.")); 62 62 } 63 63 … … 66 66 throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators."); 67 67 68 SymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]);68 ISymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]); 69 69 70 70 Child = result; … … 72 72 } 73 73 74 protected abstract SymbolicExpressionTree Cross(IRandom random,75 SymbolicExpressionTree parent0,SymbolicExpressionTree parent1);74 protected abstract ISymbolicExpressionTree Cross(IRandom random, 75 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 76 76 } 77 77 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/DefaultSymbolicExpressionGrammar.cs ¶
r5499 r5510 74 74 } 75 75 [Storable] 76 private IEnumerable<KeyValuePair<string, Symbol>> AllSymbols {76 private IEnumerable<KeyValuePair<string, ISymbol>> AllSymbols { 77 77 get { return allSymbols.AsEnumerable(); } 78 78 set { allSymbols = value.ToDictionary(x => x.Key, x => x.Value); } … … 83 83 private Dictionary<string, int> maxSubTreeCount; 84 84 private Dictionary<string, List<List<string>>> allowedChildSymbols; 85 private Dictionary<string, Symbol> allSymbols;86 [Storable] 87 private Symbol startSymbol;85 private Dictionary<string, ISymbol> allSymbols; 86 [Storable] 87 private ISymbol startSymbol; 88 88 89 89 [StorableConstructor] … … 103 103 maxSubTreeCount = new Dictionary<string, int>(original.maxSubTreeCount); 104 104 105 allSymbols = new Dictionary<string, Symbol>();106 foreach ( Symbol symbol in original.allSymbols.Values.Select(s => cloner.Clone(s)))105 allSymbols = new Dictionary<string, ISymbol>(); 106 foreach (ISymbol symbol in original.allSymbols.Values.Select(s => cloner.Clone(s))) 107 107 allSymbols.Add(symbol.Name, symbol); 108 108 109 startSymbol = cloner.Clone< Symbol>(original.startSymbol);109 startSymbol = cloner.Clone<ISymbol>(original.startSymbol); 110 110 allowedChildSymbols = new Dictionary<string, List<List<string>>>(); 111 111 foreach (var entry in original.allowedChildSymbols) { … … 121 121 this.maxSubTreeCount = new Dictionary<string, int>(); 122 122 this.allowedChildSymbols = new Dictionary<string, List<List<string>>>(); 123 this.allSymbols = new Dictionary<string, Symbol>();123 this.allSymbols = new Dictionary<string, ISymbol>(); 124 124 this.cachedMinExpressionLength = new Dictionary<string, int>(); 125 125 this.cachedMaxExpressionLength = new Dictionary<string, int>(); … … 142 142 this.maxSubTreeCount = new Dictionary<string, int>(); 143 143 this.allowedChildSymbols = new Dictionary<string, List<List<string>>>(); 144 this.allSymbols = new Dictionary<string, Symbol>();145 146 this.StartSymbol = ( Symbol)cloner.Clone(grammar.StartSymbol);147 148 foreach ( Symbol symbol in grammar.Symbols) {149 Symbol clonedSymbol = (Symbol)cloner.Clone(symbol);144 this.allSymbols = new Dictionary<string, ISymbol>(); 145 146 this.StartSymbol = (ISymbol)cloner.Clone(grammar.StartSymbol); 147 148 foreach (ISymbol symbol in grammar.Symbols) { 149 ISymbol clonedSymbol = (ISymbol)cloner.Clone(symbol); 150 150 this.AddSymbol(clonedSymbol); 151 151 this.SetMinSubtreeCount(clonedSymbol, grammar.GetMinSubtreeCount(symbol)); … … 153 153 } 154 154 155 foreach ( Symbol parent in grammar.Symbols) {155 foreach (ISymbol parent in grammar.Symbols) { 156 156 for (int i = 0; i < grammar.GetMaxSubtreeCount(parent); i++) { 157 foreach ( Symbol child in grammar.Symbols) {157 foreach (ISymbol child in grammar.Symbols) { 158 158 if (grammar.IsAllowedChild(parent, child, i)) { 159 this.SetAllowedChild(( Symbol)cloner.Clone(parent), (Symbol)cloner.Clone(child), i);159 this.SetAllowedChild((ISymbol)cloner.Clone(parent), (ISymbol)cloner.Clone(child), i); 160 160 } 161 161 } … … 181 181 182 182 #region ISymbolicExpressionGrammar Members 183 public Symbol StartSymbol {183 public ISymbol StartSymbol { 184 184 get { return startSymbol; } 185 185 set { startSymbol = value; } 186 186 } 187 187 188 public void AddSymbol( Symbol symbol) {188 public void AddSymbol(ISymbol symbol) { 189 189 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined."); 190 190 allSymbols.Add(symbol.Name, symbol); … … 193 193 } 194 194 195 public void RemoveSymbol( Symbol symbol) {195 public void RemoveSymbol(ISymbol symbol) { 196 196 foreach (var parent in Symbols) { 197 197 for (int i = 0; i < GetMaxSubtreeCount(parent); i++) … … 206 206 } 207 207 208 public IEnumerable< Symbol> Symbols {208 public IEnumerable<ISymbol> Symbols { 209 209 get { return allSymbols.Values.AsEnumerable(); } 210 210 } 211 211 212 public bool ContainsSymbol( Symbol symbol) {212 public bool ContainsSymbol(ISymbol symbol) { 213 213 return allSymbols.ContainsKey(symbol.Name); 214 214 } 215 215 216 public void SetAllowedChild( Symbol parent,Symbol child, int argumentIndex) {216 public void SetAllowedChild(ISymbol parent, ISymbol child, int argumentIndex) { 217 217 if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent"); 218 218 if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child"); … … 222 222 } 223 223 224 public bool IsAllowedChild( Symbol parent,Symbol child, int argumentIndex) {224 public bool IsAllowedChild(ISymbol parent, ISymbol child, int argumentIndex) { 225 225 if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent"); 226 226 if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child"); … … 230 230 231 231 private Dictionary<string, int> cachedMinExpressionLength; 232 public int GetMinExpressionLength( Symbol symbol) {232 public int GetMinExpressionLength(ISymbol symbol) { 233 233 if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol); 234 234 if (!cachedMinExpressionLength.ContainsKey(symbol.Name)) { … … 246 246 247 247 private Dictionary<string, int> cachedMaxExpressionLength; 248 public int GetMaxExpressionLength( Symbol symbol) {248 public int GetMaxExpressionLength(ISymbol symbol) { 249 249 if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol); 250 250 if (!cachedMaxExpressionLength.ContainsKey(symbol.Name)) { … … 262 262 263 263 private Dictionary<string, int> cachedMinExpressionDepth; 264 public int GetMinExpressionDepth( Symbol symbol) {264 public int GetMinExpressionDepth(ISymbol symbol) { 265 265 if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol); 266 266 if (!cachedMinExpressionDepth.ContainsKey(symbol.Name)) { … … 275 275 } 276 276 277 public void SetMaxSubtreeCount( Symbol symbol, int nSubTrees) {277 public void SetMaxSubtreeCount(ISymbol symbol, int nSubTrees) { 278 278 if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol); 279 279 maxSubTreeCount[symbol.Name] = nSubTrees; … … 286 286 } 287 287 288 public void SetMinSubtreeCount( Symbol symbol, int nSubTrees) {288 public void SetMinSubtreeCount(ISymbol symbol, int nSubTrees) { 289 289 if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol); 290 290 minSubTreeCount[symbol.Name] = nSubTrees; … … 292 292 } 293 293 294 public int GetMinSubtreeCount( Symbol symbol) {294 public int GetMinSubtreeCount(ISymbol symbol) { 295 295 if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol); 296 296 return minSubTreeCount[symbol.Name]; 297 297 } 298 298 299 public int GetMaxSubtreeCount( Symbol symbol) {299 public int GetMaxSubtreeCount(ISymbol symbol) { 300 300 if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol); 301 301 return maxSubTreeCount[symbol.Name]; … … 313 313 maxSubTreeCount = new Dictionary<string, int>(original.maxSubTreeCount); 314 314 315 allSymbols = new Dictionary<string, Symbol>(original.allSymbols);315 allSymbols = new Dictionary<string, ISymbol>(original.allSymbols); 316 316 startSymbol = original.startSymbol; 317 317 allowedChildSymbols = new Dictionary<string, List<List<string>>>(original.allowedChildSymbols.Count); … … 323 323 } 324 324 } 325 326 325 } 327 326 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeStringFormatter.cs ¶
r5499 r5510 46 46 } 47 47 48 public string Format( SymbolicExpressionTree symbolicExpressionTree) {48 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 49 49 return FormatRecursively(symbolicExpressionTree.Root, 0); 50 50 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/GlobalSymbolicExpressionGrammar.cs ¶
r5499 r5510 79 79 } 80 80 81 public GlobalSymbolicExpressionGrammar(ISymbolicExpression Grammar mainBranchGrammar)81 public GlobalSymbolicExpressionGrammar(ISymbolicExpressionTreeGrammar mainBranchGrammar) 82 82 : base(mainBranchGrammar) { 83 83 maxFunctionArguments = 3; … … 121 121 122 122 [Obsolete] 123 private void Initialize(ISymbolicExpression Grammar mainBranchGrammar) {123 private void Initialize(ISymbolicExpressionTreeGrammar mainBranchGrammar) { 124 124 base.Clear(); 125 125 -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj ¶
r5499 r5510 109 109 </ItemGroup> 110 110 <ItemGroup> 111 <Compile Include="Analyzers\MinAverageMaxSymbolicExpressionTreeLengthAnalyzer.cs" /> 111 112 <Compile Include="Analyzers\SymbolicExpressionSymbolFrequencyAnalyzer.cs" /> 112 <Compile Include="Analyzers\MinAverageMaxSymbolicExpressionTreeSizeAnalyzer.cs" /> 113 <Compile Include="Analyzers\SymbolicExpressionTreeSizeCalculator.cs" /> 113 <Compile Include="Analyzers\SymbolicExpressionTreeLengthCalculator.cs" /> 114 114 <Compile Include="ArchitectureManipulators\ArgumentCreater.cs" /> 115 115 <Compile Include="ArchitectureManipulators\ArgumentDeleter.cs" /> -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs ¶
r5499 r5510 25 25 public interface ISymbol : INamedItem { 26 26 ISymbolicExpressionTreeNode CreateTreeNode(); 27 double InitialFrequency ;27 double InitialFrequency { get; } 28 28 } 29 29 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTree.cs ¶
r5499 r5510 25 25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 26 26 public interface ISymbolicExpressionTree : IItem { 27 ISymbolicExpressionTreeNode Root { get; }27 ISymbolicExpressionTreeNode Root { get; set; } 28 28 int Size { get; } 29 29 int Height { get; } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs ¶
r5499 r5510 22 22 using HeuristicLab.Core; 23 23 using System; 24 using HeuristicLab.Common; 24 25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 25 public interface ISymbolicExpressionTreeNode : I Item{26 public interface ISymbolicExpressionTreeNode : IDeepCloneable { 26 27 ISymbolicExpressionTreeGrammar Grammar { get; } 27 28 ISymbolicExpressionTreeNode Parent { get; set; } 28 29 ISymbol Symbol { get; } 29 30 bool HasLocalParameters { get; } … … 37 38 IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix(); 38 39 IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix(); 39 void ForEachNodePostfix(Action< SymbolicExpressionTreeNode> a);40 void ForEachNodePrefix(Action< SymbolicExpressionTreeNode> a);40 void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a); 41 void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a); 41 42 42 43 IEnumerable<ISymbolicExpressionTreeNode> SubTrees { get; } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeManipulator.cs ¶
r5494 r5510 28 28 /// </summary> 29 29 public interface ISymbolicExpressionTreeManipulator : ISymbolicExpressionTreeOperator, IManipulator { 30 ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter { get; }30 ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; } 31 31 } 32 32 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs ¶
r5499 r5510 41 41 } 42 42 43 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree) {43 protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { 44 44 ChangeNodeType(random, symbolicExpressionTree); 45 45 } 46 46 47 public static void ChangeNodeType(IRandom random, SymbolicExpressionTree symbolicExpressionTree) {47 public static void ChangeNodeType(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { 48 48 49 49 // select any node as parent (except the root node) -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/FullTreeShaker.cs ¶
r5499 r5510 54 54 } 55 55 56 protected override void Manipulate(IRandom random, SymbolicExpressionTree tree) {56 protected override void Manipulate(IRandom random, ISymbolicExpressionTree tree) { 57 57 tree.Root.ForEachNodePostfix(node => { 58 58 if (node.HasLocalParameters) { -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/OnePointShaker.cs ¶
r5499 r5510 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 27 using HeuristicLab.Parameters; 28 using System.Collections.Generic; 28 29 29 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 54 55 } 55 56 56 protected override void Manipulate(IRandom random, SymbolicExpressionTree tree) { 57 var parametricNodes = from node in tree.IterateNodesPrefix() 58 where node.HasLocalParameters 59 select node; 60 if (parametricNodes.Count() > 0) { 57 protected override void Manipulate(IRandom random, ISymbolicExpressionTree tree) { 58 List<ISymbolicExpressionTreeNode> parametricNodes = new List<ISymbolicExpressionTreeNode>(); 59 tree.Root.ForEachNodePostfix(n => { 60 if (n.HasLocalParameters) parametricNodes.Add(n); 61 }); 62 if (parametricNodes.Count > 0) { 61 63 var selectedPoint = parametricNodes.SelectRandom(random); 62 63 64 selectedPoint.ShakeLocalParameters(random, ShakingFactor.Value); 64 65 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/SymbolicExpressionTreeManipulator.cs ¶
r5499 r5510 36 36 37 37 #region Parameter Properties 38 public ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter {39 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }38 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 39 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 40 40 } 41 41 #endregion 42 42 43 43 #region Properties 44 public SymbolicExpressionTree SymbolicExpressionTree {44 public ISymbolicExpressionTree SymbolicExpressionTree { 45 45 get { return SymbolicExpressionTreeParameter.ActualValue; } 46 46 } … … 52 52 public SymbolicExpressionTreeManipulator() 53 53 : base() { 54 Parameters.Add(new LookupParameter< SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));54 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 55 55 } 56 56 57 57 public sealed override IOperation Apply() { 58 SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;58 ISymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue; 59 59 Manipulate(RandomParameter.ActualValue, tree); 60 60 … … 62 62 } 63 63 64 protected abstract void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree);64 protected abstract void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree); 65 65 } 66 66 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs ¶
r5499 r5510 31 31 public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode { 32 32 [Storable] 33 private IList< SymbolicExpressionTreeNode> subTrees;33 private IList<ISymbolicExpressionTreeNode> subTrees; 34 34 [Storable] 35 private Symbol symbol;35 private ISymbol symbol; 36 36 37 37 // cached values to prevent unnecessary tree iterations … … 39 39 private ushort height; 40 40 41 public Symbol Symbol {41 public ISymbol Symbol { 42 42 get { return symbol; } 43 43 protected set { symbol = value; } … … 45 45 46 46 // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree) 47 private SymbolicExpressionTreeNode parent;48 internalSymbolicExpressionTreeNode Parent {47 private ISymbolicExpressionTreeNode parent; 48 public ISymbolicExpressionTreeNode Parent { 49 49 get { return parent; } 50 50 set { parent = value; } … … 56 56 : base() { 57 57 symbol = original.symbol; // symbols are reused 58 subTrees = new List< SymbolicExpressionTreeNode>(original.SubTrees.Count);59 foreach (var subtree in original. SubTrees) {58 subTrees = new List<ISymbolicExpressionTreeNode>(original.subTrees.Count); 59 foreach (var subtree in original.subTrees) { 60 60 var clonedSubTree = cloner.Clone(subtree); 61 61 subTrees.Add(clonedSubTree); … … 73 73 } 74 74 75 public SymbolicExpressionTreeNode( Symbol symbol)75 public SymbolicExpressionTreeNode(ISymbol symbol) 76 76 : base() { 77 subTrees = new List< SymbolicExpressionTreeNode>(3);77 subTrees = new List<ISymbolicExpressionTreeNode>(3); 78 78 this.symbol = symbol; 79 79 } … … 82 82 [StorableHook(HookType.AfterDeserialization)] 83 83 private void AfterDeserialization() { 84 foreach (var subtree in SubTrees) {84 foreach (var subtree in subTrees) { 85 85 subtree.Parent = this; 86 86 } … … 91 91 } 92 92 93 public virtual I List<SymbolicExpressionTreeNode> SubTrees {93 public virtual IEnumerable<ISymbolicExpressionTreeNode> SubTrees { 94 94 get { return subTrees; } 95 95 } … … 103 103 else { 104 104 size = 1; 105 if ( SubTrees != null) {106 for (int i = 0; i < SubTrees.Count; i++) {107 checked { size += (ushort) SubTrees[i].GetSize(); }105 if (subTrees != null) { 106 for (int i = 0; i < subTrees.Count; i++) { 107 checked { size += (ushort)subTrees[i].GetSize(); } 108 108 } 109 109 } … … 115 115 if (height > 0) return height; 116 116 else { 117 if ( SubTrees != null) {118 for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (ushort)SubTrees[i].GetHeight());117 if (subTrees != null) { 118 for (int i = 0; i < subTrees.Count; i++) height = Math.Max(height, (ushort)subTrees[i].GetHeight()); 119 119 } 120 120 height++; … … 126 126 public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { } 127 127 128 public virtual void AddSubTree(SymbolicExpressionTreeNode tree) { 129 SubTrees.Add(tree); 128 public virtual ISymbolicExpressionTreeNode GetSubTree(int index) { 129 return subTrees[index]; 130 } 131 public virtual int IndexOfSubTree(ISymbolicExpressionTreeNode tree) { 132 return subTrees.IndexOf(tree); 133 } 134 public virtual void AddSubTree(ISymbolicExpressionTreeNode tree) { 135 subTrees.Add(tree); 130 136 tree.Parent = this; 131 137 ResetCachedValues(); 132 138 } 133 134 public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) { 135 SubTrees.Insert(index, tree); 139 public virtual void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) { 140 subTrees.Insert(index, tree); 136 141 tree.Parent = this; 137 142 ResetCachedValues(); 138 143 } 139 140 144 public virtual void RemoveSubTree(int index) { 141 SubTrees[index].Parent = null;142 SubTrees.RemoveAt(index);145 subTrees[index].Parent = null; 146 subTrees.RemoveAt(index); 143 147 ResetCachedValues(); 144 148 } 145 149 146 public IEnumerable< SymbolicExpressionTreeNode> IterateNodesPrefix() {147 List< SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();150 public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() { 151 List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>(); 148 152 ForEachNodePrefix((n) => list.Add(n)); 149 153 return list; 150 154 } 151 155 152 public void ForEachNodePrefix(Action< SymbolicExpressionTreeNode> a) {156 public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) { 153 157 a(this); 154 158 if (SubTrees != null) { 155 for (int i = 0; i < SubTrees.Count; i++) {156 SubTrees[i].ForEachNodePrefix(a);157 } 158 } 159 } 160 161 public IEnumerable< SymbolicExpressionTreeNode> IterateNodesPostfix() {162 List< SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();159 foreach (var subtree in SubTrees) { 160 subtree.ForEachNodePrefix(a); 161 } 162 } 163 } 164 165 public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() { 166 List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>(); 163 167 ForEachNodePostfix((n) => list.Add(n)); 164 168 return list; 165 169 } 166 170 167 public void ForEachNodePostfix(Action< SymbolicExpressionTreeNode> a) {171 public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) { 168 172 if (SubTrees != null) { 169 for (int i = 0; i < SubTrees.Count; i++) {170 SubTrees[i].ForEachNodePostfix(a);173 foreach (var subtree in SubTrees) { 174 subtree.ForEachNodePostfix(a); 171 175 } 172 176 } … … 190 194 private void ResetCachedValues() { 191 195 size = 0; height = 0; 192 if (parent != null) parent.ResetCachedValues(); 196 SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode; 197 if (parentNode != null) parentNode.ResetCachedValues(); 193 198 } 194 199 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeSizeConstraintOperator.cs ¶
r5499 r5510 52 52 53 53 #region Properties 54 public IRandom Random {55 get { return RandomParameter.ActualValue; }56 }57 54 public IntValue MaximumSymbolicExpressionTreeLength { 58 55 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTerminalNode.cs ¶
r5499 r5510 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using System.Linq; 26 27 27 28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 28 29 [StorableClass] 29 30 public abstract class SymbolicExpressionTreeTerminalNode : SymbolicExpressionTreeNode { 30 private static List<SymbolicExpressionTreeNode> emptyList = new List<SymbolicExpressionTreeNode>(); 31 public override IList<SymbolicExpressionTreeNode> SubTrees { 31 public override IEnumerable<ISymbolicExpressionTreeNode> SubTrees { 32 32 get { 33 return SymbolicExpressionTreeTerminalNode.emptyList;33 return Enumerable.Empty<ISymbolicExpressionTreeNode>(); 34 34 } 35 35 } … … 37 37 [StorableConstructor] 38 38 protected SymbolicExpressionTreeTerminalNode(bool deserializing) : base(deserializing) { } 39 // don't call storableconstructor of base to prevent allocation of sub-trees list in base!39 // don't call cloning constructor of base to prevent allocation of sub-trees list in base! 40 40 protected SymbolicExpressionTreeTerminalNode(SymbolicExpressionTreeTerminalNode original, Cloner cloner) 41 41 : base() { … … 51 51 } 52 52 53 public override void AddSubTree(SymbolicExpressionTreeNode tree) { 53 public override int IndexOfSubTree(ISymbolicExpressionTreeNode tree) { 54 return -1; 55 } 56 public override ISymbolicExpressionTreeNode GetSubTree(int index) { 54 57 throw new NotSupportedException(); 55 58 } 56 public override void InsertSubTree(int index, SymbolicExpressionTreeNode tree) { 59 public override void AddSubTree(ISymbolicExpressionTreeNode tree) { 60 throw new NotSupportedException(); 61 } 62 public override void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) { 57 63 throw new NotSupportedException(); 58 64 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Argument.cs ¶
r5499 r5510 55 55 } 56 56 57 public override SymbolicExpressionTreeNode CreateTreeNode() {57 public override ISymbolicExpressionTreeNode CreateTreeNode() { 58 58 return new ArgumentTreeNode(this); 59 59 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Defun.cs ¶
r5499 r5510 42 42 } 43 43 44 public override SymbolicExpressionTreeNode CreateTreeNode() {44 public override ISymbolicExpressionTreeNode CreateTreeNode() { 45 45 return new DefunTreeNode(this); 46 46 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/InvokeFunction.cs ¶
r5499 r5510 60 60 } 61 61 62 public override SymbolicExpressionTreeNode CreateTreeNode() {62 public override ISymbolicExpressionTreeNode CreateTreeNode() { 63 63 return new InvokeFunctionTreeNode(this); 64 64 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/ProgramRootSymbol.cs ¶
r5499 r5510 38 38 return new ProgramRootSymbol(this, cloner); 39 39 } 40 public override SymbolicExpressionTreeNode CreateTreeNode() {40 public override ISymbolicExpressionTreeNode CreateTreeNode() { 41 41 return new SymbolicExpressionTreeTopLevelNode(this); 42 42 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/StartSymbol.cs ¶
r5499 r5510 39 39 } 40 40 41 public override SymbolicExpressionTreeNode CreateTreeNode() {41 public override ISymbolicExpressionTreeNode CreateTreeNode() { 42 42 return new SymbolicExpressionTreeTopLevelNode(this); 43 43 } -
TabularUnified branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs ¶
r5499 r5510 64 64 65 65 66 public virtual SymbolicExpressionTreeNode CreateTreeNode() {66 public virtual ISymbolicExpressionTreeNode CreateTreeNode() { 67 67 return new SymbolicExpressionTreeNode(this); 68 68 }
Note: See TracChangeset
for help on using the changeset viewer.