Changeset 2730 for trunk/sources
- Timestamp:
- 02/01/10 16:43:56 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.Operators/3.3/Initialization/ProbabilisticTreeCreator.cs
r2566 r2730 24 24 using HeuristicLab.Random; 25 25 using HeuristicLab.GP.Interfaces; 26 using System; 26 27 27 28 namespace HeuristicLab.GP.Operators { … … 58 59 public static IFunctionTree Create(IRandom random, FunctionLibrary funLib, int minSize, int maxSize, int maxHeight) { 59 60 int treeSize = random.Next(minSize, maxSize); 60 IFunctionTree root ;61 IFunctionTree root = null; 61 62 int tries = 0; 62 63 TreeGardener gardener = new TreeGardener(random, funLib); 63 64 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 } 65 73 if (tries++ >= MAX_TRIES) { 66 74 // try a different size … … 68 76 tries = 0; 69 77 } 70 } while (root .GetSize() > maxSize || root.GetHeight() > maxHeight);78 } while (root == null || root.GetSize() > maxSize || root.GetHeight() > maxHeight); 71 79 return root; 72 80 } -
trunk/sources/HeuristicLab.GP/3.3/TreeGardener.cs
r2675 r2730 483 483 484 484 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"); 485 488 double[] accumulatedTickets = new double[functionSet.Count]; 486 489 double ticketAccumulator = 0; … … 498 501 if (r < accumulatedTickets[i]) return functionSet[i]; 499 502 } 500 // sanity check 501 throw new InvalidProgramException(); // should never happen 503 throw new ArgumentException(); 502 504 } 503 505
Note: See TracChangeset
for help on using the changeset viewer.