Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/28/09 19:24:23 (15 years ago)
Author:
gkronber
Message:

Created a branch for #713

Location:
branches/GP-Refactoring-713
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/TreeGardener.cs

    r1529 r2202  
    3636  internal class TreeGardener {
    3737    private IRandom random;
    38     private GPOperatorLibrary funLibrary;
     38    private FunctionLibrary funLibrary;
    3939    private List<IFunction> functions;
    4040
     
    5050
    5151    #region constructors
    52     internal TreeGardener(IRandom random, GPOperatorLibrary funLibrary) {
     52    internal TreeGardener(IRandom random, FunctionLibrary funLibrary) {
    5353      this.random = random;
    5454      this.funLibrary = funLibrary;
     
    5757      functions = new List<IFunction>();
    5858      // init functions and terminals based on constraints
    59       foreach(IFunction fun in funLibrary.Group.Operators) {
     59      foreach(IFunction fun in funLibrary.Functions) {
    6060        if(fun.MaxArity == 0) {
    6161          terminals.Add(fun);
     
    112112      }
    113113      int actualArity = random.Next(minArity, maxArity + 1);
    114       totalListMinSize += GetMinimalTreeSize(root.Function) - 1;
     114      totalListMinSize += root.Function.MinTreeSize - 1;
    115115      for(int i = 0; i < actualArity; i++) {
    116116        // insert a dummy sub-tree and add the pending extension to the list
     
    134134        } else {
    135135          IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where(
    136             f => !IsTerminal(f) && GetMinimalTreeHeight(f) + (d - 1) <= maxDepth).ToArray());
     136            f => !IsTerminal(f) && f.MinTreeHeight + (d - 1) <= maxDepth).ToArray());
    137137          IFunctionTree newTree = selectedFunction.GetTreeNode();
    138138          parent.RemoveSubTree(a);
     
    152152            list.Add(new object[] { newTree, i, d + 1 });
    153153          }
    154           totalListMinSize += GetMinimalTreeSize(newTree.Function) - 1;
     154          totalListMinSize += newTree.Function.MinTreeSize - 1;
    155155        }
    156156      }
     
    178178    internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight) {
    179179      // get the minimal needed height based on allowed functions and extend the max-height if necessary
    180       int minTreeHeight = allowedFunctions.Select(f => GetMinimalTreeHeight(f)).Min();
     180      int minTreeHeight = allowedFunctions.Select(f => f.MinTreeHeight).Min();
    181181      if(minTreeHeight > maxTreeHeight)
    182182        maxTreeHeight = minTreeHeight;
    183183      // get the minimal needed size based on allowed functions and extend the max-size if necessary
    184       int minTreeSize = allowedFunctions.Select(f => GetMinimalTreeSize(f)).Min();
     184      int minTreeSize = allowedFunctions.Select(f => f.MinTreeSize).Min();
    185185      if(minTreeSize > maxTreeSize)
    186186        maxTreeSize = minTreeSize;
     
    191191
    192192      // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits
    193       IFunction[] possibleFunctions = allowedFunctions.Where(f => GetMinimalTreeHeight(f) <= treeHeight &&
    194         GetMinimalTreeSize(f) <= treeSize).ToArray();
     193      IFunction[] possibleFunctions = allowedFunctions.Where(f => f.MinTreeHeight <= treeHeight &&
     194        f.MinTreeSize <= treeSize).ToArray();
    195195      IFunction selectedFunction = RandomSelect(possibleFunctions);
    196196
     
    206206      Scope tempScope = new Scope("Temp. initialization scope");
    207207
    208       var parametricTrees = trees.Where(t => t.Function.GetVariable(FunctionBase.INITIALIZATION) != null);
     208      var parametricTrees = trees.Where(t => t.Initializer != null);
    209209      foreach(IFunctionTree tree in parametricTrees) {
    210210        // enqueue an initialization operation for each operator with local variables
    211         IOperator initialization = (IOperator)tree.Function.GetVariable(FunctionBase.INITIALIZATION).Value;
     211        IOperator initialization = tree.Initializer;
    212212        Scope initScope = new Scope();
    213213        // copy the local variables into a temporary scope used for initialization
     
    280280    internal bool IsValidTree(IFunctionTree tree) {
    281281      for(int i = 0; i < tree.SubTrees.Count; i++) {
    282         if(!tree.Function.AllowedSubFunctions(i).Contains(tree.SubTrees[i].Function)) return false;
     282        if(!tree.Function.GetAllowedSubFunctions(i).Contains(tree.SubTrees[i].Function)) return false;
    283283      }
    284284
     
    329329      // we only count those slots that can hold at least one of the children that we should combine
    330330      for(int slot = 0; slot < nSlots; slot++) {
    331         HashSet<IFunction> functionSet = new HashSet<IFunction>(f.AllowedSubFunctions(slot));
     331        HashSet<IFunction> functionSet = new HashSet<IFunction>(f.GetAllowedSubFunctions(slot));
    332332        if(functionSet.Count() > 0) {
    333333          slotSets.Add(functionSet);
     
    378378      return f.MinArity == 0 && f.MaxArity == 0;
    379379    }
    380     internal IList<IFunction> GetAllowedSubFunctions(IFunction f, int index) {
     380    internal ICollection<IFunction> GetAllowedSubFunctions(IFunction f, int index) {
    381381      if(f == null) {
    382382        return allFunctions;
    383383      } else {
    384         return f.AllowedSubFunctions(index);
     384        return f.GetAllowedSubFunctions(index);
    385385      }
    386386    }
     
    393393        return selectedTerminal;
    394394      } else {
    395         IFunction[] possibleFunctions = functions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
    396           GetMinimalTreeSize(f) <= maxTreeSize).ToArray();
     395        IFunction[] possibleFunctions = functions.Where(f => f.MinTreeHeight <= maxTreeHeight &&
     396          f.MinTreeSize <= maxTreeSize).ToArray();
    397397        IFunction selectedFunction = RandomSelect(possibleFunctions);
    398398        return selectedFunction;
     
    409409        IFunctionTree parentTree = parent.GetTreeNode();
    410410        for(int i = 0; i < actualArity; i++) {
    411           IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight).ToArray();
     411          IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => f.MinTreeHeight <= maxTreeHeight).ToArray();
    412412          IFunction selectedFunction = RandomSelect(possibleFunctions);
    413413          IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, maxTreeHeight - 1);
     
    431431        for(int i = 0; i < actualArity; i++) {
    432432          // first try to find a function that fits into the maxHeight limit
    433           IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
     433          IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => f.MinTreeHeight <= maxTreeHeight &&
    434434            !IsTerminal(f)).ToArray();
    435435          // no possible function found => extend function set to terminals
     
    450450    }
    451451
    452     private int GetMinimalTreeHeight(IOperator op) {
    453       return ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data;
    454     }
    455 
    456     private int GetMinimalTreeSize(IOperator op) {
    457       return ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data;
    458     }
    459 
    460452    private void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) {
    461453      action(tree);
     
    479471      // precalculate the slot-sizes
    480472      foreach(IFunction function in functionSet) {
    481         ticketAccumulator += ((DoubleData)function.GetVariable(GPOperatorLibrary.TICKETS).Value).Data;
     473        ticketAccumulator += function.Tickets;
    482474        accumulatedTickets[i] = ticketAccumulator;
    483475        i++;
Note: See TracChangeset for help on using the changeset viewer.