Changeset 149
- Timestamp:
- 04/22/08 12:48:01 (17 years ago)
- Location:
- branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs
r144 r149 66 66 } else { 67 67 selectedChildIndex = random.Next(parent.SubTrees.Count); 68 selectedChild = parent.SubTrees 68 selectedChild = parent.SubTrees[selectedChildIndex]; 69 69 } 70 70 … … 123 123 124 124 private IFunctionTree ChangeTerminalType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random) { 125 126 125 IList<IFunction> allowedChildren; 127 126 if (parent == null) { … … 159 158 int minArity; 160 159 int maxArity; 161 160 // only allow functions where we can keep all existing sub-trees 161 // we don't want to create new sub-trees here 162 // this restriction can be removed if we add code that creates sub-trees where necessary (gkronber 22.04.08) 162 163 allowedFunctions = allowedFunctions.Where(f => { 163 164 gardener.GetMinMaxArity(f, out minArity, out maxArity); … … 165 166 }).ToList(); 166 167 168 // create a new tree-node for a randomly selected function 167 169 IFunctionTree newTree = new FunctionTree(allowedFunctions[random.Next(allowedFunctions.Count)]); 168 170 169 171 gardener.GetMinMaxArity(newTree.Function, out minArity, out maxArity); 170 // if the old child had too many sub-trees then make the new child with the maximal arity172 // if the old child had too many sub-trees then the new child should keep as many sub-trees as possible 171 173 if (actualArity > maxArity) 172 174 actualArity = maxArity; … … 185 187 for (int i = 0; i < actualArity; i++) { 186 188 // fill all sub-tree slots of the new tree 187 // if for a given slot i there are existing sub-trees that can be used in that slot189 // if for a given slot i there are multiple existing sub-trees that can be used in that slot 188 190 // then use a random existing sub-tree. When there are no existing sub-trees 189 191 // that fit in the given slot then create a new random tree and use it for the slot 190 IList<IFunction> allowed Operators = analyser.GetAllowedOperators(newTree.Function, i).Cast<IFunction>().ToList();191 var matchingSubTrees = availableSubTrees.Where(subTree => allowed Operators.Contains(subTree.Function));192 IList<IFunction> allowedSubFunctions = analyser.GetAllowedOperators(newTree.Function, i).Cast<IFunction>().ToList(); 193 var matchingSubTrees = availableSubTrees.Where(subTree => allowedSubFunctions.Contains(subTree.Function)); 192 194 193 195 if (matchingSubTrees.Count() > 0) { … … 197 199 availableSubTrees.Remove(selectedSubTree); // the branch shouldn't be available for the following slots 198 200 } else { 201 // no existing matching tree found => create a new one 199 202 IFunctionTree freshTree; 200 203 if(random.NextDouble() <= balancedTreesRate) { 201 freshTree = gardener.CreateRandomTree(allowed Operators, maxSubTreeSize, maxSubTreeHeight, true);204 freshTree = gardener.CreateRandomTree(allowedSubFunctions, maxSubTreeSize, maxSubTreeHeight, true); 202 205 } else { 203 freshTree = gardener.CreateRandomTree(allowed Operators, maxSubTreeSize, maxSubTreeHeight, false);206 freshTree = gardener.CreateRandomTree(allowedSubFunctions, maxSubTreeSize, maxSubTreeHeight, false); 204 207 } 205 208 freshSubTrees.AddRange(gardener.GetAllSubTrees(freshTree)); … … 208 211 } 209 212 } 210 211 213 uninitializedBranches = freshSubTrees; 212 214 return newTree; -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/CutOutNodeManipulation.cs
r148 r149 80 80 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 81 81 82 // this is not really necessary (we can leave it in until the tree is stable)83 if (!gardener.IsValidTree(root)) {84 throw new InvalidProgramException();85 }86 87 82 // update the variable 88 83 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root; … … 90 85 throw new InvalidProgramException(); 91 86 } 92 // the tree is already initializedso we don't have to schedule initialization operations87 // we reused a sub-tree so we don't have to schedule initialization operations 93 88 return null; 94 89 } else { 95 // create a new random tree90 // we want to cut the root node and there are no sub-trees => create a new random terminal 96 91 IFunctionTree newTree; 97 if(random.NextDouble() <= balancedTreesRate) { 98 newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true); 99 } else { 100 newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false); 101 } 102 92 newTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1, false); 103 93 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree); 104 94 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree); 105 106 95 // update the variable 107 96 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 108 109 97 if (!gardener.IsValidTree(newTree)) { 110 98 throw new InvalidProgramException(); 111 99 } 112 113 100 // schedule an operation to initialize the whole tree 114 101 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope); … … 116 103 } 117 104 105 // select a child to cut away 118 106 int childIndex = random.Next(parent.SubTrees.Count); 119 107 IFunctionTree child = parent.SubTrees[childIndex]; 120 108 121 109 // match the sub-trees of the child with the allowed sub-trees of the parent 122 I FunctionTree[] possibleChilds = gardener.GetAllowedSubFunctions(parent.Function, childIndex).SelectMany(allowedFun => child.SubTrees123 .Where(subTree => allowedFun == subTree.Function)).ToArray(); // Actually we should probably use OperatorEqualityComparer here (gkronber 21.04.08)110 IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 111 IFunctionTree[] possibleChilds = child.SubTrees.Where(t => allowedFunctions.Contains(t.Function)).ToArray(); 124 112 125 113 if (possibleChilds.Length > 0) { 126 // replace child with a random child of the child 127 // make a clone to simplify removing obsolete branches from the function-tree 114 // replace child with a random child of that child 128 115 IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)]; 129 116 parent.RemoveSubTree(childIndex); … … 140 127 return null; 141 128 } else { 129 // can't reuse an existing branch => create a new tree 142 130 // determine the level of the parent 143 131 int parentLevel = gardener.GetBranchLevel(root, parent); … … 148 136 int remainingNodes = gardener.GetTreeSize(root); 149 137 150 IList<IFunction>allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);151 IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, true);138 allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 139 IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, false); 152 140 153 141 parent.InsertSubTree(childIndex, newFunctionTree); -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/DeleteSubTreeManipulation.cs
r143 r149 59 59 // => return a new minimal random tree 60 60 if(parent == null) { 61 IFunctionTree newTree = gardener.CreateRandomTree(1, 1, true);61 IFunctionTree newTree = gardener.CreateRandomTree(1, 1, false); 62 62 63 63 // check if the tree is ok … … 75 75 } 76 76 77 // select a branch to prune 77 78 int childIndex = random.Next(parent.SubTrees.Count); 78 79 79 int min; 80 80 int max; … … 112 112 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 113 113 // again the root hasn't changed so we don't need to update the 'FunctionTree' variable 114 // but we have to return an initialization operation for the newly created tree 114 115 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope); 115 116 } -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs
r144 r149 110 110 } 111 111 112 IFunctionTree oldChild = parent.SubTrees[childIndex];113 112 parent.RemoveSubTree(childIndex); 114 113 parent.InsertSubTree(childIndex, newTree);
Note: See TracChangeset
for help on using the changeset viewer.