Changeset 2210 for branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/CutOutNodeManipulation.cs
- Timestamp:
- 07/29/09 18:28:45 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/CutOutNodeManipulation.cs
r2202 r2210 28 28 using System; 29 29 using System.Diagnostics; 30 using HeuristicLab.GP.Interfaces; 30 31 31 32 namespace HeuristicLab.GP { 32 public class CutOutNodeManipulation : OperatorBase {33 public class CutOutNodeManipulation : GPManipulatorBase { 33 34 public override string Description { 34 35 get { … … 49 50 public CutOutNodeManipulation() 50 51 : base() { 51 AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));52 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));53 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));54 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));55 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));56 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));57 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));58 52 } 59 53 60 54 61 public override IOperation Apply(IScope scope) { 62 IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); 63 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 64 FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true); 65 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data; 66 int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data; 55 internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) { 67 56 TreeGardener gardener = new TreeGardener(random, library); 68 IFunctionTree parent = gardener.GetRandomParentNode( root);57 IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree); 69 58 // parent == null means we should cut out the root node 70 59 // => return a random sub-tree of the root 71 60 if (parent == null) { 72 61 // when there are sub-trees then replace the old tree with a random sub-tree 73 if (root.SubTrees.Count > 0) { 74 root = root.SubTrees[random.Next(root.SubTrees.Count)]; 75 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size; 76 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height; 77 // update the variable 78 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root; 79 Debug.Assert(gardener.IsValidTree(root)); 62 if (gpModel.FunctionTree.SubTrees.Count > 0) { 63 gpModel.FunctionTree = gpModel.FunctionTree.SubTrees[random.Next(gpModel.FunctionTree.SubTrees.Count)]; 64 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 80 65 // we reused a sub-tree so we don't have to schedule initialization operations 81 66 return null; 82 67 } else { 83 68 // we want to cut the root node and there are no sub-trees => create a new random terminal 84 IFunctionTree newTree; 85 newTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1); 86 GetVariableValue<IntData>("TreeSize", scope, true).Data = newTree.Size; 87 GetVariableValue<IntData>("TreeHeight", scope, true).Data = newTree.Height; 88 // update the variable 89 scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree; 90 Debug.Assert(gardener.IsValidTree(newTree)); 69 gpModel.FunctionTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1); 70 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 71 91 72 // schedule an operation to initialize the whole tree 92 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);73 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(gpModel.FunctionTree), scope); 93 74 } 94 75 } 76 95 77 // select a child to cut away 96 78 int childIndex = random.Next(parent.SubTrees.Count); … … 101 83 if (possibleChilds.Length > 0) { 102 84 // replace child with a random child of that child 103 IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)]; 85 IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)]; 104 86 parent.RemoveSubTree(childIndex); 105 87 parent.InsertSubTree(childIndex, selectedChild); 106 Debug.Assert(gardener.IsValidTree( root));107 // update the size and height of our tree108 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;109 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;88 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 89 // recalculate the size and height of our tree 90 gpModel.Size = gpModel.FunctionTree.GetSize(); 91 gpModel.Height = gpModel.FunctionTree.GetHeight(); 110 92 // don't need to schedule initialization operations 111 93 return null; … … 113 95 // can't reuse an existing branch => create a new tree 114 96 // determine the level of the parent 115 int parentLevel = gardener.GetBranchLevel( root, parent);97 int parentLevel = gardener.GetBranchLevel(gpModel.FunctionTree, parent); 116 98 // first remove the old child (first step essential!) 117 99 parent.RemoveSubTree(childIndex); 118 100 // then determine the number of nodes left over after the child has been removed! 119 int remainingNodes = root.Size;101 int remainingNodes = gpModel.FunctionTree.GetSize(); 120 102 allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex); 121 103 IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel); 122 104 parent.InsertSubTree(childIndex, newFunctionTree); 123 GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size; 124 GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height; 125 Debug.Assert(gardener.IsValidTree(root)); 105 Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree)); 106 // recalculate size and height 107 gpModel.Size = gpModel.FunctionTree.GetSize(); 108 gpModel.Height = gpModel.FunctionTree.GetHeight(); 126 109 // schedule an initialization operation for the new function-tree 127 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);110 return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope); 128 111 } 129 112 }
Note: See TracChangeset
for help on using the changeset viewer.