Changeset 155 for trunk/sources/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs
- Timestamp:
- 04/22/08 18:05:14 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs
r23 r155 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.AllOperators, maxTreeSize, maxTreeHeight, true);70 newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true); 72 71 } else { 73 new OperatorTree = gardener.CreateRandomTree(gardener.AllOperators, 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.GetAll Operators(newOperatorTree), 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.GetAllowedSubOperators(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 ICollection<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.Get NodeLevel(rootOperator, 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 IOperator oldChild = parent.SubOperators[childIndex]; 116 parent.RemoveSubOperator(childIndex); 117 parent.AddSubOperator(newOperatorTree, childIndex); 112 parent.RemoveSubTree(childIndex); 113 parent.InsertSubTree(childIndex, newTree); 118 114 119 if(!gardener.IsValidTree(root Operator)) {115 if(!gardener.IsValidTree(root)) { 120 116 throw new InvalidProgramException(); 121 117 } 122 118 123 119 // 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 120 GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root); 121 GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root); 122 // the root hasn't changed so we don't need to update 128 123 // return a CompositeOperation that randomly initializes all nodes of the new subtree 129 return gardener.CreateInitializationOperation(gardener.GetAll Operators(newOperatorTree), scope);124 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope); 130 125 } 131 126 }
Note: See TracChangeset
for help on using the changeset viewer.