Free cookie consent management tool by TermsFeed Policy Generator

Changeset 149


Ignore:
Timestamp:
04/22/08 12:48:01 (16 years ago)
Author:
gkronber
Message:

minor changes in GP manipulation operators (#112)

Location:
branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs

    r144 r149  
    6666      } else {
    6767        selectedChildIndex = random.Next(parent.SubTrees.Count);
    68         selectedChild = parent.SubTrees [selectedChildIndex];
     68        selectedChild = parent.SubTrees[selectedChildIndex];
    6969      }
    7070
     
    123123
    124124    private IFunctionTree ChangeTerminalType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random) {
    125 
    126125      IList<IFunction> allowedChildren;
    127126      if (parent == null) {
     
    159158      int minArity;
    160159      int maxArity;
    161 
     160      // only allow functions where we can keep all existing sub-trees
     161      // we don't want to create new sub-trees here
     162      // this restriction can be removed if we add code that creates sub-trees where necessary (gkronber 22.04.08)
    162163      allowedFunctions = allowedFunctions.Where(f => {
    163164        gardener.GetMinMaxArity(f, out minArity, out maxArity);
     
    165166      }).ToList();
    166167
     168      // create a new tree-node for a randomly selected function
    167169      IFunctionTree newTree = new FunctionTree(allowedFunctions[random.Next(allowedFunctions.Count)]);
    168170
    169171      gardener.GetMinMaxArity(newTree.Function, out minArity, out maxArity);
    170       // if the old child had too many sub-trees then make the new child with the maximal arity
     172      // if the old child had too many sub-trees then the new child should keep as many sub-trees as possible
    171173      if (actualArity > maxArity)
    172174        actualArity = maxArity;
     
    185187      for (int i = 0; i < actualArity; i++) {
    186188        // fill all sub-tree slots of the new tree
    187         // if for a given slot i there are existing sub-trees that can be used in that slot
     189        // if for a given slot i there are multiple existing sub-trees that can be used in that slot
    188190        // then use a random existing sub-tree. When there are no existing sub-trees
    189191        // that fit in the given slot then create a new random tree and use it for the slot
    190         IList<IFunction> allowedOperators = analyser.GetAllowedOperators(newTree.Function, i).Cast<IFunction>().ToList();
    191         var matchingSubTrees = availableSubTrees.Where(subTree => allowedOperators.Contains(subTree.Function));
     192        IList<IFunction> allowedSubFunctions = analyser.GetAllowedOperators(newTree.Function, i).Cast<IFunction>().ToList();
     193        var matchingSubTrees = availableSubTrees.Where(subTree => allowedSubFunctions.Contains(subTree.Function));
    192194
    193195        if (matchingSubTrees.Count() > 0) {
     
    197199          availableSubTrees.Remove(selectedSubTree); // the branch shouldn't be available for the following slots
    198200        } else {
     201          // no existing matching tree found => create a new one
    199202          IFunctionTree freshTree;
    200203          if(random.NextDouble() <= balancedTreesRate) {
    201             freshTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, true);
     204            freshTree = gardener.CreateRandomTree(allowedSubFunctions, maxSubTreeSize, maxSubTreeHeight, true);
    202205          } else {
    203             freshTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, false);
     206            freshTree = gardener.CreateRandomTree(allowedSubFunctions, maxSubTreeSize, maxSubTreeHeight, false);
    204207          }
    205208          freshSubTrees.AddRange(gardener.GetAllSubTrees(freshTree));
     
    208211        }
    209212      }
    210 
    211213      uninitializedBranches = freshSubTrees;
    212214      return newTree;
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/CutOutNodeManipulation.cs

    r148 r149  
    8080          GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
    8181
    82           // this is not really necessary (we can leave it in until the tree is stable)
    83           if (!gardener.IsValidTree(root)) {
    84             throw new InvalidProgramException();
    85           }
    86 
    8782          // update the variable
    8883          scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root;
     
    9085            throw new InvalidProgramException();
    9186          }
    92           // the tree is already initialized so we don't have to schedule initialization operations
     87          // we reused a sub-tree so we don't have to schedule initialization operations
    9388          return null;
    9489        } else {
    95           // create a new random tree
     90          // we want to cut the root node and there are no sub-trees => create a new random terminal
    9691          IFunctionTree newTree;
    97           if(random.NextDouble() <= balancedTreesRate) {
    98             newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
    99           } else {
    100             newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);
    101           }
    102 
     92          newTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1, false);
    10393          GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
    10494          GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
    105 
    10695          // update the variable
    10796          scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
    108 
    10997          if (!gardener.IsValidTree(newTree)) {
    11098            throw new InvalidProgramException();
    11199          }
    112 
    113100          // schedule an operation to initialize the whole tree
    114101          return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
     
    116103      }
    117104
     105      // select a child to cut away
    118106      int childIndex = random.Next(parent.SubTrees.Count);
    119107      IFunctionTree child = parent.SubTrees[childIndex];
    120108
    121109      // match the sub-trees of the child with the allowed sub-trees of the parent
    122       IFunctionTree[] possibleChilds = gardener.GetAllowedSubFunctions(parent.Function, childIndex).SelectMany(allowedFun => child.SubTrees
    123         .Where(subTree => allowedFun == subTree.Function)).ToArray(); // Actually we should probably use OperatorEqualityComparer here (gkronber 21.04.08)
     110      IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
     111      IFunctionTree[] possibleChilds = child.SubTrees.Where(t => allowedFunctions.Contains(t.Function)).ToArray();
    124112
    125113      if (possibleChilds.Length > 0) {
    126         // replace child with a random child of the child
    127         // make a clone to simplify removing obsolete branches from the function-tree
     114        // replace child with a random child of that child
    128115        IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)];       
    129116        parent.RemoveSubTree(childIndex);
     
    140127        return null;
    141128      } else {
     129        // can't reuse an existing branch => create a new tree
    142130        // determine the level of the parent
    143131        int parentLevel = gardener.GetBranchLevel(root, parent);
     
    148136        int remainingNodes = gardener.GetTreeSize(root);
    149137
    150         IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
    151         IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, true);
     138        allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
     139        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, false);
    152140
    153141        parent.InsertSubTree(childIndex, newFunctionTree);
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/DeleteSubTreeManipulation.cs

    r143 r149  
    5959      // => return a new minimal random tree
    6060      if(parent == null) {
    61         IFunctionTree newTree = gardener.CreateRandomTree(1, 1, true);
     61        IFunctionTree newTree = gardener.CreateRandomTree(1, 1, false);
    6262
    6363        // check if the tree is ok
     
    7575      }
    7676
     77      // select a branch to prune
    7778      int childIndex = random.Next(parent.SubTrees.Count);
    78 
    7979      int min;
    8080      int max;
     
    112112        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
    113113        // again the root hasn't changed so we don't need to update the 'FunctionTree' variable
     114        // but we have to return an initialization operation for the newly created tree
    114115        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);
    115116      }
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs

    r144 r149  
    110110        }
    111111
    112         IFunctionTree oldChild = parent.SubTrees[childIndex];
    113112        parent.RemoveSubTree(childIndex);
    114113        parent.InsertSubTree(childIndex, newTree);
Note: See TracChangeset for help on using the changeset viewer.