Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/01/10 16:43:56 (15 years ago)
Author:
gkronber
Message:

Made PTC2 operator more robust. #859 (PTC2 initialization operator fails if it is used with restrictive function libraries)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.Operators/3.3/Initialization/ProbabilisticTreeCreator.cs

    r2566 r2730  
    2424using HeuristicLab.Random;
    2525using HeuristicLab.GP.Interfaces;
     26using System;
    2627
    2728namespace HeuristicLab.GP.Operators {
     
    5859    public static IFunctionTree Create(IRandom random, FunctionLibrary funLib, int minSize, int maxSize, int maxHeight) {
    5960      int treeSize = random.Next(minSize, maxSize);
    60       IFunctionTree root;
     61      IFunctionTree root = null;
    6162      int tries = 0;
    6263      TreeGardener gardener = new TreeGardener(random, funLib);
    6364      do {
    64         root = gardener.PTC2(treeSize, maxHeight);
     65        try {
     66          root = gardener.PTC2(treeSize, maxHeight);
     67        }
     68        catch (ArgumentException) {
     69          // try a different size
     70          treeSize = random.Next(minSize, maxSize);
     71          tries = 0;
     72        }
    6573        if (tries++ >= MAX_TRIES) {
    6674          // try a different size
     
    6876          tries = 0;
    6977        }
    70       } while (root.GetSize() > maxSize || root.GetHeight() > maxHeight);
     78      } while (root == null || root.GetSize() > maxSize || root.GetHeight() > maxHeight);
    7179      return root;
    7280    }
Note: See TracChangeset for help on using the changeset viewer.