Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/02/09 12:47:48 (15 years ago)
Author:
gkronber
Message:

merged fix for #479 (Crossover operators create trees that are larger than the allowed max size) (r1196) to trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP/Recombination/StandardCrossOver.cs

    r1065 r1197  
    3333namespace HeuristicLab.GP {
    3434  public class StandardCrossOver : SizeConstrictedGPCrossoverBase {
    35     private const int MAX_RECOMBINATION_TRIES = 20;
     35    private int MaxRecombinationTries { get { return 20; } }
    3636
    3737    public override string Description {
     
    5252        // select a random crossover point in the first parent tree0
    5353        parent0 = null;
    54         while(parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
     54        while (parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
    5555        // select a random branch to replace
    5656        replacedChildIndex = random.Next(parent0.SubTrees.Count);
     
    6262        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex);
    6363        allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
    64       } while(allowedCrossoverPoints.Count == 0 && tries++ < MAX_RECOMBINATION_TRIES);
     64      } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries);
    6565
    66       if(allowedCrossoverPoints.Count > 0) {
     66      if (allowedCrossoverPoints.Count > 0) {
    6767        IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)];
    6868
     
    7676    private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
    7777      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)
    8080          crossoverPoints.Add(possiblePoint);
    8181      }
Note: See TracChangeset for help on using the changeset viewer.