- Timestamp:
- 02/02/09 12:45:08 (16 years ago)
- Location:
- branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP/Recombination
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP/Recombination/OnePointCrossOver.cs
r832 r1196 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 } -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP/Recombination/SizeConstrictedGPCrossoverBase.cs
r835 r1196 32 32 33 33 namespace HeuristicLab.GP { 34 public abstract class SizeConstrictedGPCrossoverBase : GPCrossoverBase{ 34 public abstract class SizeConstrictedGPCrossoverBase : GPCrossoverBase { 35 36 private int MaxRecombinationTries { get { return 20; } } 37 35 38 public SizeConstrictedGPCrossoverBase() 36 39 : base() { … … 45 48 // when tree0 is terminal then try to cross into tree1, when tree1 is also terminal just return tree0 unchanged. 46 49 IFunctionTree newTree; 47 if (tree0.SubTrees.Count > 0) { 48 newTree = Cross(gardener, random, tree0, tree1, maxTreeSize, maxTreeHeight); 49 } else if (tree1.SubTrees.Count > 0) { 50 newTree = Cross(gardener, random, tree1, tree0, maxTreeSize, maxTreeHeight); 51 } else newTree = tree0; 52 53 // check if the size and height of the new tree are still within the allowed bounds 54 Debug.Assert(newTree.Height <= maxTreeHeight); 55 Debug.Assert(newTree.Size <= maxTreeSize); 50 int tries = 0; 51 do { 52 if (tree0.SubTrees.Count > 0) { 53 newTree = Cross(gardener, random, (IFunctionTree)tree0.Clone(), (IFunctionTree)tree1.Clone(), maxTreeSize, maxTreeHeight); 54 } else if (tree1.SubTrees.Count > 0) { 55 newTree = Cross(gardener, random, (IFunctionTree)tree1.Clone(), (IFunctionTree)tree0.Clone(), maxTreeSize, maxTreeHeight); 56 } else newTree = tree0; 57 if (tries++ > MaxRecombinationTries) 58 throw new InvalidOperationException("Couldn't recombine parents to create a valid child not larger than " + maxTreeSize + " and not higher than " + maxTreeHeight + "."); 59 } while (newTree.Size > maxTreeSize || newTree.Height > maxTreeHeight); 56 60 return newTree; 57 61 } -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP/Recombination/SizeFairCrossOver.cs
r835 r1196 39 39 /// </summary> 40 40 public class SizeFairCrossOver : SizeConstrictedGPCrossoverBase { 41 private const int MAX_RECOMBINATION_TRIES = 20;41 private int MaxRecombinationTries { get { return 20; } } 42 42 // private data structure for crossover points 43 43 protected class CrossoverPoint { … … 57 57 removedBranchIndex = random.Next(parent.SubTrees.Count); 58 58 insertedBranch = GetReplacementBranch(random, gardener, tree0, parent, removedBranchIndex, tree1, maxTreeSize, maxTreeHeight); 59 } while (insertedBranch == null && tries++ < M AX_RECOMBINATION_TRIES);59 } while (insertedBranch == null && tries++ < MaxRecombinationTries); 60 60 61 61 if (insertedBranch != null) { … … 139 139 for (int i = 0; i < root.SubTrees.Count; i++) { 140 140 GetTrail(root.SubTrees[i], branch, trail); 141 if (trail.Count >0) {141 if (trail.Count > 0) { 142 142 trail.Add(i); 143 143 return; -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP/Recombination/StandardCrossOver.cs
r1064 r1196 33 33 namespace HeuristicLab.GP { 34 34 public class StandardCrossOver : SizeConstrictedGPCrossoverBase { 35 private const int MAX_RECOMBINATION_TRIES = 20;35 private int MaxRecombinationTries { get { return 20; } } 36 36 37 37 public override string Description { … … 52 52 // select a random crossover point in the first parent tree0 53 53 parent0 = null; 54 while (parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);54 while (parent0 == null) parent0 = gardener.GetRandomParentNode(tree0); 55 55 // select a random branch to replace 56 56 replacedChildIndex = random.Next(parent0.SubTrees.Count); … … 62 62 IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex); 63 63 allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions); 64 } while (allowedCrossoverPoints.Count == 0 && tries++ < MAX_RECOMBINATION_TRIES);64 } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries); 65 65 66 if (allowedCrossoverPoints.Count > 0) {66 if (allowedCrossoverPoints.Count > 0) { 67 67 IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)]; 68 68 … … 76 76 private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) { 77 77 List<IFunctionTree> crossoverPoints = new List<IFunctionTree>(); 78 foreach (IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {79 if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight)78 foreach (IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) { 79 if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight) 80 80 crossoverPoints.Add(possiblePoint); 81 81 } -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP/Recombination/UniformCrossover.cs
r832 r1196 37 37 /// In Proceedings of Genetic Programming '98, Madison, Wisconsin, 1998. 38 38 /// </summary> 39 public class UniformCrossover : GPCrossoverBase {39 public class UniformCrossover : SizeConstrictedGPCrossoverBase { 40 40 // internal datastructure to represent crossover points 41 41 private class CrossoverPoint { … … 52 52 } 53 53 54 internal override IFunctionTree Cross( IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {54 internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 55 55 List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>(); 56 56 GetCrossOverPoints(gardener, tree0, tree1, allowedCrossOverPoints);
Note: See TracChangeset
for help on using the changeset viewer.