Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/02/09 12:45:08 (15 years ago)
Author:
gkronber
Message:

fixed #479 (Crossover operators create trees that are larger than the allowed max size)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP/Recombination/OnePointCrossOver.cs

    r832 r1196  
    3636  /// W. B. Langdon and R. Poli.  Foundations of Genetic Programming. Springer-Verlag, 2002.
    3737  /// </summary>
    38   public class OnePointCrossOver : GPCrossoverBase {
     38  public class OnePointCrossOver : SizeConstrictedGPCrossoverBase {
    3939    // internal data structure to represent crossover points
    4040    private class CrossoverPoint {
     
    4545    public override string Description {
    4646      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.";
    4848      }
    4949    }
    5050
    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) {
    5252      List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
    53       GetCrossOverPoints(gardener, tree0, tree1, allowedCrossOverPoints);
     53      GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.Size, allowedCrossOverPoints);
    5454      if (allowedCrossOverPoints.Count > 0) {
    5555        CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
     
    6262    }
    6363
    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) {
    6565      if (branch0.SubTrees.Count != branch1.SubTrees.Count) return;
    6666
    6767      for (int i = 0; i < branch0.SubTrees.Count; i++) {
    6868        // 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) {
    7071          CrossoverPoint p = new CrossoverPoint();
    7172          p.childIndex = i;
     
    7475          crossoverPoints.Add(p);
    7576        }
    76         GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i], crossoverPoints);
     77        GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i], maxNewNodes, crossoverPoints);
    7778      }
    7879    }
Note: See TracChangeset for help on using the changeset viewer.