Free cookie consent management tool by TermsFeed Policy Generator

Changeset 444 for trunk/sources


Ignore:
Timestamp:
08/05/08 11:34:33 (16 years ago)
Author:
gkronber
Message:

fixed #227

Location:
trunk/sources/HeuristicLab.StructureIdentification
Files:
3 edited

Legend:

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

    r324 r444  
    2727using HeuristicLab.Random;
    2828using HeuristicLab.Functions;
     29using System.Diagnostics;
    2930
    3031namespace HeuristicLab.StructureIdentification {
     
    6566      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(actualTreeHeight)));
    6667
    67       if(!gardener.IsValidTree(root)) { throw new InvalidProgramException(); }
    68 
    69       if(actualTreeHeight > maxTreeHeight) {
    70         throw new InvalidProgramException();
    71       }
     68      Debug.Assert(gardener.IsValidTree(root) && actualTreeHeight<=maxTreeHeight);
    7269
    7370      return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope);
  • trunk/sources/HeuristicLab.StructureIdentification/Recombination/SizeFairCrossOver.cs

    r442 r444  
    9696
    9797      // check if the new tree is valid and if the height of is still in the allowed bounds (we are not so strict for the max-size)
    98       Debug.Assert(gardener.IsValidTree(newTree) && newTreeHeight <= maxTreeHeight);
     98      Debug.Assert(gardener.IsValidTree(newTree) && newTreeHeight <= maxTreeHeight && newTreeSize <= maxTreeSize);
    9999      return gardener.CreateInitializationOperation(newBranches, child);
    100100    }
  • trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs

    r433 r444  
    100100
    101101    internal IFunctionTree PTC2(IRandom random, int size, int maxDepth) {
    102       if(size == 1) return RandomSelect(terminals).GetTreeNode();
     102      if(size <= 1 || maxDepth<=1) return RandomSelect(terminals).GetTreeNode();
    103103      List<object[]> list = new List<object[]>();
    104104      IFunctionTree root = GetRandomRoot(size, maxDepth).GetTreeNode();
     105
    105106      int currentSize = 1;
     107      int totalListMinSize = 0;
    106108      int minArity;
    107109      int maxArity;
     
    111113      }
    112114      int actualArity = random.Next(minArity, maxArity + 1);
    113       for(int i=0;i<actualArity;i++) {
     115      totalListMinSize += GetMinimalTreeSize(root.Function) - 1;
     116      for(int i = 0; i < actualArity; i++) {
    114117        // insert a dummy sub-tree and add the pending extension to the list
    115118        root.AddSubTree(null);
    116         list.Add(new object[] {root, i, 2});
    117       }
    118 
    119       while(list.Count > 0 && list.Count + currentSize < size) {
     119        list.Add(new object[] { root, i, 2 });
     120      }
     121
     122      while(list.Count > 0 && totalListMinSize + currentSize < size) {
    120123        int randomIndex = random.Next(list.Count);
    121124        object[] nextExtension = list[randomIndex];
     
    126129        if(d == maxDepth) {
    127130          parent.RemoveSubTree(a);
    128           parent.InsertSubTree(a, RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where(f => IsTerminal(f)).ToArray()).GetTreeNode());
     131          IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1);
     132          parent.InsertSubTree(a, branch); // insert a smallest possible tree
     133          currentSize += branch.Size;
     134          totalListMinSize-=branch.Size;
    129135        } else {
    130           IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where( 
    131             f => !IsTerminal(f) && GetMinimalTreeHeight(f) + (d-1) <= maxDepth).ToArray());
     136          IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where(
     137            f => !IsTerminal(f) && GetMinimalTreeHeight(f) + (d - 1) <= maxDepth).ToArray());
    132138          IFunctionTree newTree = selectedFunction.GetTreeNode();
    133139          parent.RemoveSubTree(a);
    134140          parent.InsertSubTree(a, newTree);
     141          currentSize++;
     142          totalListMinSize--;
    135143
    136144          GetMinMaxArity(selectedFunction, out minArity, out maxArity);
     
    144152            list.Add(new object[] { newTree, i, d + 1 });
    145153          }
    146         }
    147         currentSize++;
     154          totalListMinSize += GetMinimalTreeSize(newTree.Function) - 1;
     155        }
    148156      }
    149157      while(list.Count > 0) {
     
    184192    internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight, bool balanceTrees) {
    185193      // get the minimal needed height based on allowed functions and extend the max-height if necessary
    186       int minTreeHeight = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data).Min();
     194      int minTreeHeight = allowedFunctions.Select(f => GetMinimalTreeSize(f)).Min();
    187195      if(minTreeHeight > maxTreeHeight)
    188196        maxTreeHeight = minTreeHeight;
    189197      // get the minimal needed size based on allowed functions and extend the max-size if necessary
    190       int minTreeSize = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data).Min();
     198      int minTreeSize = allowedFunctions.Select(f => GetMinimalTreeSize(f)).Min();
    191199      if(minTreeSize > maxTreeSize)
    192200        maxTreeSize = minTreeSize;
     
    197205
    198206      // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits
    199       IFunction[] possibleFunctions = allowedFunctions.Where(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight &&
    200         ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray();
     207      IFunction[] possibleFunctions = allowedFunctions.Where(f => GetMinimalTreeHeight(f) <= treeHeight &&
     208        GetMinimalTreeSize(f) <= treeSize).ToArray();
    201209      IFunction selectedFunction = RandomSelect(possibleFunctions);
    202210
     
    519527      List<IFunctionTree> branches = new List<IFunctionTree>();
    520528      foreach(IFunctionTree subTree in tree.SubTrees) {
    521         if(subTree.Height>=level-1)
    522         branches.AddRange(GetBranchesAtLevel(subTree, level - 1));
     529        if(subTree.Height >= level - 1)
     530          branches.AddRange(GetBranchesAtLevel(subTree, level - 1));
    523531      }
    524532      return branches;
Note: See TracChangeset for help on using the changeset viewer.