Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2730 for trunk/sources


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)

Location:
trunk/sources
Files:
2 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    }
  • trunk/sources/HeuristicLab.GP/3.3/TreeGardener.cs

    r2675 r2730  
    483483
    484484    public static IFunction RandomSelect(IRandom random, IList<IFunction> functionSet) {
     485      if (random == null || functionSet == null) throw new ArgumentNullException();
     486      if (functionSet.Count == 0) throw new ArgumentException("Empty function set");
     487      if (functionSet.Select(x => x.Tickets).Sum() <= 0) throw new ArgumentException("All functions in set have 0 tickets");
    485488      double[] accumulatedTickets = new double[functionSet.Count];
    486489      double ticketAccumulator = 0;
     
    498501        if (r < accumulatedTickets[i]) return functionSet[i];
    499502      }
    500       // sanity check
    501       throw new InvalidProgramException(); // should never happen
     503      throw new ArgumentException();
    502504    }
    503505
Note: See TracChangeset for help on using the changeset viewer.