Changeset 803
- Timestamp:
- 11/22/08 16:39:32 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/Recombination/UniformCrossover.cs
r802 r803 79 79 private IOperation Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child) { 80 80 IFunctionTree newTree = Cross(random, gardener, parent1, parent2); 81 81 Debug.Assert(gardener.IsValidTree(newTree)); 82 82 int newTreeSize = newTree.Size; 83 83 int newTreeHeight = newTree.Height; … … 100 100 101 101 List<CrossoverPoint> allowedCrossOverPoints = GetCrossOverPoints(gardener, tree0, tree1); 102 102 foreach (CrossoverPoint p in allowedCrossOverPoints) { 103 Debug.Assert(gardener.GetAllowedSubFunctions(p.parent0.Function, p.childIndex).Contains(p.parent1.SubTrees[p.childIndex].Function)); 104 Debug.Assert(gardener.GetAllowedSubFunctions(p.parent1.Function, p.childIndex).Contains(p.parent0.SubTrees[p.childIndex].Function)); 105 } 103 106 // iterate through the list of crossover points and swap nodes with p=0.5 104 107 foreach (CrossoverPoint crossoverPoint in allowedCrossOverPoints) { … … 109 112 IFunctionTree branch1 = crossoverPoint.parent1.SubTrees[crossoverPoint.childIndex]; 110 113 114 // if we are at an internal node of the common region swap only the node but not the subtrees 111 115 if (branch0.SubTrees.Count == branch1.SubTrees.Count) { 112 // if we are at an internal node of the common region swap only the node but not the subtrees113 116 if (parent0 != null) { 114 117 Debug.Assert(parent1 != null); Debug.Assert(branch0 != null); Debug.Assert(branch0 != null); 118 Debug.Assert(gardener.GetAllowedSubFunctions(parent0.Function, crossoverPoint.childIndex).Contains(branch1.Function)); 119 Debug.Assert(gardener.GetAllowedSubFunctions(parent1.Function, crossoverPoint.childIndex).Contains(branch0.Function)); 115 120 // we are not at the root => exchange the branches in the parent 116 121 parent0.RemoveSubTree(crossoverPoint.childIndex); … … 124 129 while (branch0.SubTrees.Count > 0) branch0.RemoveSubTree(0); // remove all children 125 130 while (branch1.SubTrees.Count > 0) branch1.RemoveSubTree(0); 126 foreach (IFunctionTree subTree in branch1Children) branch0.AddSubTree(subTree); // append children of branch1 to branch0 127 foreach (IFunctionTree subTree in branch0Children) branch1.AddSubTree(subTree); // and vice versa 131 foreach (IFunctionTree subTree in branch1Children) { 132 Debug.Assert(gardener.GetAllowedSubFunctions(branch0.Function, branch0.SubTrees.Count).Contains(subTree.Function)); 133 branch0.AddSubTree(subTree); // append children of branch1 to branch0 134 } 135 foreach (IFunctionTree subTree in branch0Children) { 136 Debug.Assert(gardener.GetAllowedSubFunctions(branch1.Function, branch1.SubTrees.Count).Contains(subTree.Function)); 137 branch1.AddSubTree(subTree); // and vice versa 138 } 128 139 } else { 129 140 // If we are at a node at the border of the common region then exchange the whole branch. … … 133 144 // However if we are not at the root => exchange the branches in the parent 134 145 if (parent0 != null) { 135 Debug.Assert(parent1 != null); Debug.Assert(branch0 != null); Debug.Assert(branch1 != null); 146 Debug.Assert(parent1 != null); Debug.Assert(branch0 != null); Debug.Assert(branch1 != null); 147 Debug.Assert(gardener.GetAllowedSubFunctions(parent0.Function, crossoverPoint.childIndex).Contains(branch1.Function)); 148 Debug.Assert(gardener.GetAllowedSubFunctions(parent1.Function, crossoverPoint.childIndex).Contains(branch0.Function)); 136 149 parent0.RemoveSubTree(crossoverPoint.childIndex); 137 150 parent1.RemoveSubTree(crossoverPoint.childIndex); … … 156 169 157 170 for (int i = 0; i < branch0.SubTrees.Count; i++) { 171 // if the branches fit to the parent 158 172 if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) && 159 173 gardener.GetAllowedSubFunctions(branch1.Function, i).Contains(branch0.SubTrees[i].Function)) { 160 CrossoverPoint p = new CrossoverPoint(); 161 p.childIndex = i; 162 p.parent0 = branch0; 163 p.parent1 = branch1; 164 results.Add(p); 174 // if the point is at the border of the common region we don't care about the children 175 // however if the point is not on the border of the common region we also have to check if 176 // the children of the branches fit together 177 bool fit = true; 178 if (branch0.SubTrees[i].SubTrees.Count == branch1.SubTrees[i].SubTrees.Count) { 179 for (int j = 0; j < branch0.SubTrees[i].SubTrees.Count; j++) { 180 fit = fit & gardener.GetAllowedSubFunctions(branch0.SubTrees[i].Function, j).Contains(branch1.SubTrees[i].SubTrees[j].Function); 181 fit = fit & gardener.GetAllowedSubFunctions(branch1.SubTrees[i].Function, j).Contains(branch0.SubTrees[i].SubTrees[j].Function); 182 } 183 } 184 if (fit) { 185 CrossoverPoint p = new CrossoverPoint(); 186 p.childIndex = i; 187 p.parent0 = branch0; 188 p.parent1 = branch1; 189 results.Add(p); 190 } 165 191 } 166 192 results.AddRange(GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i]));
Note: See TracChangeset
for help on using the changeset viewer.