- Timestamp:
- 02/02/09 12:47:48 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs
r832 r1197 36 36 /// W. B. Langdon and R. Poli. Foundations of Genetic Programming. Springer-Verlag, 2002. 37 37 /// </summary> 38 public class OnePointCrossOver : GPCrossoverBase {38 public class OnePointCrossOver : SizeConstrictedGPCrossoverBase { 39 39 // internal data structure to represent crossover points 40 40 private class CrossoverPoint { … … 45 45 public override string Description { 46 46 get { 47 return @" ";47 return @"One point crossover for trees as described in W. B. Langdon and R. Poli. Foundations of Genetic Programming. Springer-Verlag, 2002."; 48 48 } 49 49 } 50 50 51 internal override IFunctionTree Cross( IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {51 internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 52 52 List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>(); 53 GetCrossOverPoints(gardener, tree0, tree1, allowedCrossOverPoints);53 GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.Size, allowedCrossOverPoints); 54 54 if (allowedCrossOverPoints.Count > 0) { 55 55 CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)]; … … 62 62 } 63 63 64 private void GetCrossOverPoints(TreeGardener gardener, IFunctionTree branch0, IFunctionTree branch1, List<CrossoverPoint> crossoverPoints) {64 private void GetCrossOverPoints(TreeGardener gardener, IFunctionTree branch0, IFunctionTree branch1, int maxNewNodes, List<CrossoverPoint> crossoverPoints) { 65 65 if (branch0.SubTrees.Count != branch1.SubTrees.Count) return; 66 66 67 67 for (int i = 0; i < branch0.SubTrees.Count; i++) { 68 68 // if the current branch can be attached as a sub-tree to branch0 69 if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function)) { 69 if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) && 70 branch1.SubTrees[i].Size - branch0.SubTrees[i].Size <= maxNewNodes) { 70 71 CrossoverPoint p = new CrossoverPoint(); 71 72 p.childIndex = i; … … 74 75 crossoverPoints.Add(p); 75 76 } 76 GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i], crossoverPoints);77 GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i], maxNewNodes, crossoverPoints); 77 78 } 78 79 }
Note: See TracChangeset
for help on using the changeset viewer.