Changeset 144 for branches/FunctionsAndStructIdRefactoring
- Timestamp:
- 04/21/08 18:39:38 (17 years ago)
- Location:
- branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs
r143 r144 75 75 // no parent means the new child is the initial operator 76 76 // and we have to update the value in the variable 77 scope.GetVariable( "FunctionTree").Value = newTerminal;77 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTerminal; 78 78 } else { 79 79 parent.RemoveSubTree(selectedChildIndex); … … 92 92 // no parent means the new function is the initial operator 93 93 // and we have to update the value in the variable 94 scope.GetVariable( "FunctionTree").Value = newFunction;94 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newFunction; 95 95 root = newFunction; 96 96 } else { … … 124 124 private IFunctionTree ChangeTerminalType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random) { 125 125 126 IList<I Operator> allowedChildren;126 IList<IFunction> allowedChildren; 127 127 if (parent == null) { 128 128 allowedChildren = gardener.Terminals; 129 129 } else { 130 130 SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser(); 131 analyser.AllPossibleOperators = gardener.Terminals ;132 allowedChildren = analyser.GetAllowedOperators(parent , childIndex);131 analyser.AllPossibleOperators = gardener.Terminals.Cast<IOperator>().ToArray(); 132 allowedChildren = analyser.GetAllowedOperators(parent.Function, childIndex).Cast<IFunction>().ToList(); 133 133 } 134 134 135 135 // selecting from the terminals should always work since the current child was also a terminal 136 136 // so in the worst case we will just create a new terminal of the same type again. 137 return gardener.CreateRandomTree( (IOperator)allowedChildren[random.Next(allowedChildren.Count)].Clone(), 1, 1, false);137 return gardener.CreateRandomTree(allowedChildren[random.Next(allowedChildren.Count)], 1, 1, false); 138 138 } 139 139 … … 143 143 // and how many of the existing subtrees we can reuse 144 144 145 // let's choose the operatorwe want to use instead of the old child. For this we have to determine the146 // pool of allowed operators based on constraints of the parent if there is one.147 IList<I Operator> allowedSubOperators;145 // let's choose the function we want to use instead of the old child. For this we have to determine the 146 // pool of allowed functions based on constraints of the parent if there is one. 147 IList<IFunction> allowedFunctions; 148 148 SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser(); 149 analyser.AllPossibleOperators = gardener.AllFunctions ;149 analyser.AllPossibleOperators = gardener.AllFunctions.Cast<IOperator>().ToArray(); 150 150 if (parent == null) { 151 allowed SubOperators = gardener.AllFunctions;152 } else { 153 allowed SubOperators = analyser.GetAllowedOperators(parent, childIndex);151 allowedFunctions = gardener.AllFunctions; 152 } else { 153 allowedFunctions = analyser.GetAllowedOperators(parent.Function, childIndex).Cast<IFunction>().ToList(); 154 154 } 155 155 … … 160 160 int maxArity; 161 161 162 allowed SubOperators = allowedSubOperators.Where(f => {162 allowedFunctions = allowedFunctions.Where(f => { 163 163 gardener.GetMinMaxArity(f, out minArity, out maxArity); 164 164 return minArity <= actualArity; 165 165 }).ToList(); 166 166 167 IFunctionTree newTree = new FunctionTree( (IOperator)allowedSubOperators[random.Next(allowedSubOperators.Count)]);168 169 gardener.GetMinMaxArity(new Operator, out minArity, out maxArity);170 // if the old child had too many sub- operators then make the new child with the maximal arity167 IFunctionTree newTree = new FunctionTree(allowedFunctions[random.Next(allowedFunctions.Count)]); 168 169 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 arity 171 171 if (actualArity > maxArity) 172 172 actualArity = maxArity; … … 180 180 // create a list that holds old sub-trees that we can reuse in the new tree 181 181 List<IFunctionTree> availableSubTrees = new List<IFunctionTree>(child.SubTrees); 182 List<IFunctionTree> freshSubTrees = new List<IFunctionTree>() { new FunctionTree(newOperator)};183 184 // randomly select the sub operators that we keep182 List<IFunctionTree> freshSubTrees = new List<IFunctionTree>() { newTree }; 183 184 // randomly select the sub-trees that we keep 185 185 for (int i = 0; i < actualArity; i++) { 186 // fill all sub- operator slots of the new operator187 // if for a given slot i there are existing sub- operators that can be used in that slot188 // then use a random existing sub- operator. When there are no existing sub-operators186 // 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 slot 188 // then use a random existing sub-tree. When there are no existing sub-trees 189 189 // that fit in the given slot then create a new random tree and use it for the slot 190 IList<I Operator> allowedOperators = analyser.GetAllowedOperators(newOperator, i);190 IList<IFunction> allowedOperators = analyser.GetAllowedOperators(newTree.Function, i).Cast<IFunction>().ToList(); 191 191 var matchingSubTrees = availableSubTrees.Where(subTree => allowedOperators.Contains(subTree.Function)); 192 192 193 193 if (matchingSubTrees.Count() > 0) { 194 IFunctionTree selectedSubTree = matchingSubTrees.ElementAt(random.Next(matching Trees.Count()));195 // we can just add it as sub operator194 IFunctionTree selectedSubTree = matchingSubTrees.ElementAt(random.Next(matchingSubTrees.Count())); 195 // we can just add it as subtree 196 196 newTree.InsertSubTree(i, selectedSubTree); 197 197 availableSubTrees.Remove(selectedSubTree); // the branch shouldn't be available for the following slots -
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs
r143 r144 27 27 using HeuristicLab.Operators; 28 28 using HeuristicLab.Random; 29 using HeuristicLab.Functions; 29 30 30 31 namespace HeuristicLab.StructureIdentification { … … 42 43 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In)); 43 44 AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In)); 44 AddVariableInfo(new VariableInfo(" OperatorTree", "The tree to manipulate", typeof(IOperator), VariableKind.In));45 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In ));46 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In ));45 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to manipulate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out)); 46 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); 47 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out)); 47 48 } 48 49 49 50 public override IOperation Apply(IScope scope) { 50 IOperator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, true); 51 51 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 52 52 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 53 53 GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); … … 60 60 TreeGardener gardener = new TreeGardener(random, library); 61 61 62 I Operator parent = gardener.GetRandomParentNode(rootOperator);62 IFunctionTree parent = gardener.GetRandomParentNode(root); 63 63 if(parent == null) { 64 64 // parent == null means we should subsitute the whole tree 65 65 // => create a new random tree 66 66 67 // create a new random operator tree 68 69 IOperator newOperatorTree; 67 // create a new random function tree 68 IFunctionTree newTree; 70 69 if(random.NextDouble() <= balancedTreesRate) { 71 new OperatorTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);70 newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true); 72 71 } else { 73 new OperatorTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);72 newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false); 74 73 } 75 74 76 if(!gardener.IsValidTree(new OperatorTree)) {75 if(!gardener.IsValidTree(newTree)) { 77 76 throw new InvalidProgramException(); 78 77 } 79 78 80 79 // update the variables in the scope with the new values 81 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(new OperatorTree);82 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(new OperatorTree);83 scope.GetVariable( "OperatorTree").Value = newOperatorTree;80 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree); 81 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree); 82 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 84 83 85 // return a CompositeOperation that randomly initializes the new operator86 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(new OperatorTree), scope);84 // return a CompositeOperation that randomly initializes the new tree 85 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope); 87 86 } else { 88 87 // determine a random child of the parent to be replaced 89 int childIndex = random.Next(parent.SubOperators.Count); 90 91 // get the list of allowed suboperators as the new child 92 IList<IOperator> allowedOperators = gardener.GetAllowedSubFunctions(parent, childIndex); 93 94 if(allowedOperators.Count == 0) { 88 int childIndex = random.Next(parent.SubTrees.Count); 89 // get the list of allowed functions for the new sub-tree 90 IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 91 if(allowedFunctions.Count == 0) { 95 92 // don't change anything 96 93 // this shouldn't happen … … 100 97 // calculate the maximum size and height of the new sub-tree based on the location where 101 98 // it will be inserted 102 int parentLevel = gardener.GetBranchLevel(root Operator, parent);99 int parentLevel = gardener.GetBranchLevel(root, parent); 103 100 104 101 int maxSubTreeHeight = maxTreeHeight - parentLevel; 105 int maxSubTreeSize = maxTreeSize - (treeSize - gardener.GetTreeSize(parent.Sub Operators[childIndex]));102 int maxSubTreeSize = maxTreeSize - (treeSize - gardener.GetTreeSize(parent.SubTrees[childIndex])); 106 103 107 // get a random operatorTree108 I Operator newOperatorTree;104 // create a random function tree 105 IFunctionTree newTree; 109 106 if(random.NextDouble() <= balancedTreesRate) { 110 new OperatorTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, true);107 newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, true); 111 108 } else { 112 new OperatorTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, false);109 newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, false); 113 110 } 114 111 115 I Operator oldChild = parent.SubOperators[childIndex];116 parent.RemoveSub Operator(childIndex);117 parent. AddSubOperator(newOperatorTree, childIndex);112 IFunctionTree oldChild = parent.SubTrees[childIndex]; 113 parent.RemoveSubTree(childIndex); 114 parent.InsertSubTree(childIndex, newTree); 118 115 119 if(!gardener.IsValidTree(root Operator)) {116 if(!gardener.IsValidTree(root)) { 120 117 throw new InvalidProgramException(); 121 118 } 122 119 123 120 // update the values of treeSize and treeHeight 124 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator); 125 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator); 126 // the root operator hasn't changed so we don't need to update 127 121 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root); 122 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 123 // the root hasn't changed so we don't need to update 128 124 // return a CompositeOperation that randomly initializes all nodes of the new subtree 129 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(new OperatorTree), scope);125 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope); 130 126 } 131 127 }
Note: See TracChangeset
for help on using the changeset viewer.