Changeset 5686 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs
- Timestamp:
- 03/15/11 13:34:38 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs
r5549 r5686 27 27 using HeuristicLab.Core; 28 28 using HeuristicLab.Data; 29 using HeuristicLab.Parameters; 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Parameters;31 31 32 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 61 61 private SubroutineCreater(bool deserializing) : base(deserializing) { } 62 62 private SubroutineCreater(SubroutineCreater original, Cloner cloner) : base(original, cloner) { } 63 public SubroutineCreater() : base() { 63 public SubroutineCreater() 64 : base() { 64 65 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 65 66 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); … … 109 110 110 111 // select a random cut point in the selected branch 111 var allCutPoints = from parent in selectedBody.IterateNodesPrefix()112 from subtree in parent.SubTrees113 select new { Parent = parent, ReplacedBranchIndex = parent.IndexOfSubTree(subtree), ReplacedBranch = subtree };112 var allCutPoints = (from parent in selectedBody.IterateNodesPrefix() 113 from subtree in parent.SubTrees 114 select new CutPoint(parent, subtree)).ToList(); 114 115 if (allCutPoints.Count() == 0) 115 116 // no cut points => abort … … 118 119 var selectedCutPoint = allCutPoints.SelectRandom(random); 119 120 // select random branches as argument cut-off points (replaced by argument terminal nodes in the function) 120 List< ISymbolicExpressionTreeNode> argumentBranches = SelectRandomArgumentBranches(selectedCutPoint.ReplacedBranch, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments);121 ISymbolicExpressionTreeNode functionBody = selectedCutPoint. ReplacedBranch;121 List<CutPoint> argumentCutPoints = SelectRandomArgumentBranches(selectedCutPoint.Child, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments); 122 ISymbolicExpressionTreeNode functionBody = selectedCutPoint.Child; 122 123 // disconnect the function body from the tree 123 selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint. ReplacedBranchIndex);124 selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint.ChildIndex); 124 125 // disconnect the argument branches from the function 125 functionBody = DisconnectBranches(functionBody, argument Branches);126 functionBody = DisconnectBranches(functionBody, argumentCutPoints); 126 127 // insert a function invocation symbol instead 127 128 var invokeNode = (InvokeFunctionTreeNode)(new InvokeFunction(newFunctionName)).CreateTreeNode(); 128 selectedCutPoint.Parent.InsertSubTree(selectedCutPoint. ReplacedBranchIndex, invokeNode);129 selectedCutPoint.Parent.InsertSubTree(selectedCutPoint.ChildIndex, invokeNode); 129 130 // add the branches selected as argument as subtrees of the function invocation node 130 foreach (var argument Branch in argumentBranches)131 invokeNode.AddSubTree(argument Branch);131 foreach (var argumentCutPoint in argumentCutPoints) 132 invokeNode.AddSubTree(argumentCutPoint.Child); 132 133 133 134 // insert a new function defining branch … … 138 139 // the grammar in the newly defined function is a clone of the grammar of the originating branch 139 140 defunNode.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBody.Grammar.Clone()); 140 // remove all argument symbols from grammar 141 var oldArgumentSymbols = defunNode.Grammar.Symbols.OfType<Argument>().ToList();141 // remove all argument symbols from grammar except that one contained in cutpoints 142 var oldArgumentSymbols = selectedBody.Grammar.Symbols.OfType<Argument>().ToList(); 142 143 foreach (var oldArgSymb in oldArgumentSymbols) 143 144 defunNode.Grammar.RemoveSymbol(oldArgSymb); … … 146 147 select node.Symbol.ArgumentIndex).Distinct(); 147 148 // add argument symbols to grammar of function defining branch 148 GrammarModifier.Add DynamicArguments(defunNode.Grammar, defunNode.Symbol, newArgumentIndexes);149 GrammarModifier.AddArgumentSymbol(selectedBody.Grammar, defunNode.Grammar, newArgumentIndexes, argumentCutPoints); 149 150 defunNode.NumberOfArguments = newArgumentIndexes.Count(); 150 if (defunNode.NumberOfArguments != argument Branches.Count) throw new InvalidOperationException();151 if (defunNode.NumberOfArguments != argumentCutPoints.Count) throw new InvalidOperationException(); 151 152 // add invoke symbol for newly defined function to the original branch 152 var allowedParents = from symb in selectedBody.Grammar.Symbols 153 where !(symb is ProgramRootSymbol) 154 select symb; 155 var allowedChildren = from symb in selectedBody.Grammar.Symbols 156 where selectedBody.Grammar.IsAllowedChild(selectedBody.Symbol, symb, 0) 157 select symb; 158 GrammarModifier.AddDynamicSymbol(selectedBody.Grammar, selectedBody.Symbol, defunNode.FunctionName, defunNode.NumberOfArguments); 153 GrammarModifier.AddInvokeSymbol(selectedBody.Grammar, defunNode.FunctionName, defunNode.NumberOfArguments, selectedCutPoint, argumentCutPoints); 159 154 160 155 // when the new function body was taken from another function definition … … 168 163 // when the original branch can be invoked from the subtree then also allow invocation of the function 169 164 if (originalBranchInvokeSymbol != null) { 170 allowedParents = from symb in subtree.Grammar.Symbols 171 where !(symb is ProgramRootSymbol) 172 select symb; 173 allowedChildren = from symb in subtree.Grammar.Symbols 174 where subtree.Grammar.IsAllowedChild(subtree.Symbol, symb, 0) 175 select symb; 176 GrammarModifier.AddDynamicSymbol(subtree.Grammar, subtree.Symbol, defunNode.FunctionName, defunNode.NumberOfArguments); 165 GrammarModifier.AddInvokeSymbol(subtree.Grammar, defunNode.FunctionName, defunNode.NumberOfArguments, selectedCutPoint, argumentCutPoints); 177 166 } 178 167 } … … 181 170 } 182 171 183 private static ISymbolicExpressionTreeNode DisconnectBranches(ISymbolicExpressionTreeNode node, List< ISymbolicExpressionTreeNode> argumentBranches) {184 i f (argumentBranches.Contains(node)) {185 var argumentIndex = argumentBranches.IndexOf(node);172 private static ISymbolicExpressionTreeNode DisconnectBranches(ISymbolicExpressionTreeNode node, List<CutPoint> argumentCutPoints) { 173 int argumentIndex = argumentCutPoints.FindIndex(x => x.Child == node); 174 if (argumentIndex != -1) { 186 175 var argSymbol = new Argument(argumentIndex); 187 176 return argSymbol.CreateTreeNode(); … … 192 181 // recursively apply function for subtrees or append a argument terminal node 193 182 foreach (var subtree in subtrees) { 194 node.AddSubTree(DisconnectBranches(subtree, argument Branches));183 node.AddSubTree(DisconnectBranches(subtree, argumentCutPoints)); 195 184 } 196 185 return node; 197 186 } 198 187 199 private static List< ISymbolicExpressionTreeNode> SelectRandomArgumentBranches(ISymbolicExpressionTreeNode selectedRoot,188 private static List<CutPoint> SelectRandomArgumentBranches(ISymbolicExpressionTreeNode selectedRoot, 200 189 IRandom random, 201 190 double cutProbability, … … 203 192 // breadth first determination of argument cut-off points 204 193 // we must make sure that we cut off all original argument nodes and that the number of new argument is smaller than the limit 205 List< ISymbolicExpressionTreeNode> argumentBranches = new List<ISymbolicExpressionTreeNode>();194 List<CutPoint> argumentBranches = new List<CutPoint>(); 206 195 if (selectedRoot is ArgumentTreeNode) { 207 argumentBranches.Add( selectedRoot);196 argumentBranches.Add(new CutPoint(selectedRoot.Parent, selectedRoot)); 208 197 return argumentBranches; 209 198 } else { … … 213 202 select nArgumentsInTree).ToList(); 214 203 // determine the minimal number of new argument nodes for each sub-tree 204 //if we exceed the maxArguments return the same cutpoint as the start cutpoint to create a ADF that returns only its argument 215 205 var minNewArgumentsForSubtrees = numberOfArgumentsInSubtrees.Select(x => x > 0 ? 1 : 0).ToList(); 216 206 if (minNewArgumentsForSubtrees.Sum() > maxArguments) { 217 argumentBranches.Add( selectedRoot);207 argumentBranches.Add(new CutPoint(selectedRoot.Parent, selectedRoot)); 218 208 return argumentBranches; 219 209 } 220 210 // cut-off in the sub-trees in random order 221 211 var randomIndexes = (from index in Enumerable.Range(0, selectedRoot.SubTrees.Count()) 222 select new { Index = index, OrderValue = random.NextDouble() }).OrderBy(x => x.OrderValue).Select(x => x.Index); 212 select new { Index = index, OrderValue = random.NextDouble() }) 213 .OrderBy(x => x.OrderValue) 214 .Select(x => x.Index); 223 215 foreach (var subtreeIndex in randomIndexes) { 224 216 var subtree = selectedRoot.GetSubTree(subtreeIndex); … … 237 229 } else { 238 230 // cut-off at current sub-tree 239 argumentBranches.Add( subtree);231 argumentBranches.Add(new CutPoint(subtree.Parent, subtree)); 240 232 } 241 233 }
Note: See TracChangeset
for help on using the changeset viewer.