Free cookie consent management tool by TermsFeed Policy Generator

Changeset 182 for trunk/sources


Ignore:
Timestamp:
04/24/08 15:03:57 (16 years ago)
Author:
gkronber
Message:

fixed #127

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs

    r181 r182  
    138138      IFunction[] possibleFunctions = allowedFunctions.Where(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight &&
    139139        ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray();
    140       IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];
     140      IFunction selectedFunction = RandomSelect(possibleFunctions);
    141141
    142142      // build the tree
     
    375375    private IFunctionTree CreateRandomRoot(int maxTreeSize, int maxTreeHeight) {
    376376      if(maxTreeHeight == 1 || maxTreeSize == 1) {
    377         IFunction selectedTerminal = terminals[random.Next(terminals.Count())];
     377        IFunction selectedTerminal = RandomSelect(terminals);
    378378        return new FunctionTree(selectedTerminal);
    379379      } else {
    380380        IFunction[] possibleFunctions = allFunctions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
    381381          GetMinimalTreeSize(f) <= maxTreeSize).ToArray();
    382         IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];
     382        IFunction selectedFunction = RandomSelect(possibleFunctions);
    383383        return new FunctionTree(selectedFunction);
    384384      }
     
    399399          IFunction[] possibleFunctions = GetAllowedSubFunctions(parent.Function, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
    400400            GetMinimalTreeSize(f) <= maxSubTreeSize).ToArray();
    401           IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];
     401          IFunction selectedFunction = RandomSelect(possibleFunctions);
    402402          FunctionTree newSubTree = new FunctionTree(selectedFunction);
    403403          MakeUnbalancedTree(newSubTree, maxSubTreeSize - 1, maxTreeHeight - 1);
     
    422422        for(int i = 0; i < actualArity; i++) {
    423423          if(maxTreeHeight == 1 || maxSubTreeSize == 1) {
    424             IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where(IsTerminal(f)).ToArray();
    425             IFunction selectedTerminal = possibleTerminals[random.Next(possibleTerminals.Length)];
     424            IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where(f => IsTerminal(f)).ToArray();
     425            IFunction selectedTerminal = RandomSelect(possibleTerminals);
    426426            IFunctionTree newTree = new FunctionTree(selectedTerminal);
    427427            parent.InsertSubTree(i, newTree);
     
    431431              GetMinimalTreeSize(f) <= maxSubTreeSize &&
    432432              !IsTerminal(f)).ToArray();
    433             IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];
     433            IFunction selectedFunction = RandomSelect(possibleFunctions);
    434434            FunctionTree newTree = new FunctionTree(selectedFunction);
    435435            parent.InsertSubTree(i, newTree);
     
    465465    }
    466466
     467    private IFunction RandomSelect(IList<IFunction> functionSet) {
     468      double[] accumulatedTickets = new double[functionSet.Count];
     469      double ticketAccumulator = 0;
     470      int i = 0;
     471      // precalculate the slot-sizes
     472      foreach(IFunction function in functionSet) {
     473        ticketAccumulator += ((DoubleData)function.GetVariable(GPOperatorLibrary.TICKETS).Value).Data;
     474        accumulatedTickets[i] = ticketAccumulator;
     475        i++;
     476      }
     477      // throw ball
     478      double r = random.NextDouble() * ticketAccumulator;
     479      // find the slot that has been hit
     480      for(i = 0; i < accumulatedTickets.Length; i++) {
     481        if(r < accumulatedTickets[i]) return functionSet[i];
     482      }
     483      // sanity check
     484      throw new InvalidProgramException(); // should never happen
     485    }
    467486
    468487    #endregion
Note: See TracChangeset for help on using the changeset viewer.