Changeset 666 for trunk/sources/HeuristicLab.GP/Recombination
- Timestamp:
- 10/15/08 00:48:18 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs
r656 r666 45 45 : base() { 46 46 AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In)); 47 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));48 AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));49 AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));50 47 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New)); 51 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind. New));52 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind. New));48 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.New)); 49 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.New)); 53 50 } 54 51 55 52 public override IOperation Apply(IScope scope) { 56 53 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 57 GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);58 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;59 int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;60 61 TreeGardener gardener = new TreeGardener(random, opLibrary);62 54 63 55 if((scope.SubScopes.Count % 2) != 0) … … 73 65 scope.RemoveSubScope(parent2); 74 66 IScope child = new Scope(i.ToString()); 75 IOperation childInitOperation = Cross( gardener, maxTreeSize, maxTreeHeight,scope, random, parent1, parent2, child);67 IOperation childInitOperation = Cross(scope, random, parent1, parent2, child); 76 68 initOperations.AddOperation(childInitOperation); 77 69 scope.AddSubScope(child); … … 81 73 } 82 74 83 private IOperation Cross(TreeGardener gardener, int maxTreeSize, int maxTreeHeight, 84 IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) { 85 IFunctionTree newTree = Cross(gardener, parent1, parent2, 86 random, maxTreeSize, maxTreeHeight); 75 private IOperation Cross(IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) { 76 IFunctionTree newTree = Cross(random, parent1, parent2); 87 77 88 78 int newTreeSize = newTree.Size; … … 92 82 child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight))); 93 83 94 // check if the new tree is valid and if the height of is still in the allowed bounds (we are not so strict for the max-size)95 Debug.Assert(gardener.IsValidTree(newTree) && newTreeHeight <= maxTreeHeight && newTreeSize <= maxTreeSize);96 84 return null; 97 85 } 98 86 99 87 100 private IFunctionTree Cross( TreeGardener gardener, IScope f, IScope g, MersenneTwister random, int maxTreeSize, int maxTreeHeight) {88 private IFunctionTree Cross(MersenneTwister random, IScope f, IScope g) { 101 89 IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false); 102 90 int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data; … … 107 95 int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data; 108 96 109 if(tree0Size == 1 && tree1Size == 1) { 110 return CombineTerminals(gardener, tree0, tree1, random); 111 } else { 112 // we are going to insert tree1 into tree0 at a random place so we have to make sure that tree0 is not a terminal 113 // in case both trees are higher than 1 we swap the trees with probability 50% 114 if(tree0Height == 1 || (tree1Height > 1 && random.Next(2) == 0)) { 115 IFunctionTree tmp = tree0; tree0 = tree1; tree1 = tmp; 116 int tmpHeight = tree0Height; tree0Height = tree1Height; tree1Height = tmpHeight; 117 int tmpSize = tree0Size; tree0Size = tree1Size; tree1Size = tmpSize; 118 } 119 120 List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(null, tree0, tree1); 121 if(allowedCrossOverPoints.Count == 0) { 122 if(random.NextDouble() < 0.5) return tree0; else return tree1; 123 } 124 IFunctionTree[] crossOverPoints = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)]; 125 IFunctionTree parent = crossOverPoints[0]; 126 IFunctionTree replacedBranch = crossOverPoints[1]; 127 IFunctionTree insertedBranch = crossOverPoints[2]; 128 if(parent == null) return insertedBranch; 129 else { 130 int i = 0; 131 while(parent.SubTrees[i] != replacedBranch) i++; 132 parent.RemoveSubTree(i); 133 parent.InsertSubTree(i, insertedBranch); 134 return tree0; 135 } 97 List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(null, tree0, tree1); 98 if(allowedCrossOverPoints.Count == 0) { 99 if(random.NextDouble() < 0.5) return tree0; else return tree1; 100 } 101 IFunctionTree[] crossOverPoints = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)]; 102 IFunctionTree parent = crossOverPoints[0]; 103 IFunctionTree replacedBranch = crossOverPoints[1]; 104 IFunctionTree insertedBranch = crossOverPoints[2]; 105 if(parent == null) return insertedBranch; 106 else { 107 int i = 0; 108 while(parent.SubTrees[i] != replacedBranch) i++; 109 parent.RemoveSubTree(i); 110 parent.InsertSubTree(i, insertedBranch); 111 return tree0; 136 112 } 137 113 } … … 139 115 private List<IFunctionTree[]> GetCrossOverPoints(IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) { 140 116 List<IFunctionTree[]> results = new List<IFunctionTree[]>(); 141 if(tree0.Function != tree1.Function) return results; 117 if(tree0.SubTrees.Count != tree1.SubTrees.Count) return results; 118 // invariant arity - number of subtrees is equal in both trees 142 119 143 int maxArity = Math.Min(tree0.SubTrees.Count, tree1.SubTrees.Count);144 for(int i = 0; i < maxArity; i++) {120 results.Add(new IFunctionTree[] { parent, tree0, tree1 }); 121 for(int i = 0; i < tree0.SubTrees.Count; i++) { 145 122 results.AddRange(GetCrossOverPoints(tree0, tree0.SubTrees[i], tree1.SubTrees[i])); 146 123 } 147 if(results.Count == 0) results.Add(new IFunctionTree[] { parent, tree0, tree1 });148 124 return results; 149 }150 151 private IFunctionTree CombineTerminals(TreeGardener gardener, IFunctionTree f, IFunctionTree g, MersenneTwister random) {152 if(random.NextDouble() < 0.5) return f; else return g;153 125 } 154 126 }
Note: See TracChangeset
for help on using the changeset viewer.