Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/21/08 15:31:41 (17 years ago)
Author:
gkronber
Message:

worked on #112 in the refactoring branch

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

Legend:

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

    r142 r143  
    8484        // size and height stays the same when changing a terminal so no need to update the variables
    8585        // schedule an operation to initialize the new terminal
    86         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newTerminal), scope);
     86        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTerminal), scope);
    8787      } else {
    8888        List<IFunctionTree> uninitializedBranches;
     
    147147      IList<IOperator> allowedSubOperators;
    148148      SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser();
    149       analyser.AllPossibleOperators = gardener.AllOperators;
     149      analyser.AllPossibleOperators = gardener.AllFunctions;
    150150      if (parent == null) {
    151         allowedSubOperators = gardener.AllOperators;
     151        allowedSubOperators = gardener.AllFunctions;
    152152      } else {
    153153        allowedSubOperators = analyser.GetAllowedOperators(parent, childIndex);
     
    203203            freshTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, false);
    204204          }
    205           freshSubTrees.AddRange(gardener.GetAllOperators(freshTree));
     205          freshSubTrees.AddRange(gardener.GetAllSubTrees(freshTree));
    206206
    207207          newTree.InsertSubTree(i, freshTree);
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/CutOutNodeManipulation.cs

    r23 r143  
    2727using HeuristicLab.Random;
    2828using System;
     29using HeuristicLab.Functions;
    2930
    3031namespace HeuristicLab.StructureIdentification {
     
    3233    public override string Description {
    3334      get {
    34         return @"Takes a tree, selects a random node of the tree and then tries to replace a random child
     35        return @"Takes a tree, selects a random node of the tree and then tries to replace a random sub-tree
    3536of that node with one of the childs of the selected child.
    3637
     
    5354      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
    5455      AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
    55       AddVariableInfo(new VariableInfo("OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In));
    56       AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
    57       AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In));
     56      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
     57      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
     58      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
    5859    }
    5960
    6061
    6162    public override IOperation Apply(IScope scope) {
    62       IOperator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, true);
     63      IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
    6364      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
    6465      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
     
    6869
    6970      TreeGardener gardener = new TreeGardener(random, library);
    70       IOperator parent = gardener.GetRandomParentNode(rootOperator);
     71      IFunctionTree parent = gardener.GetRandomParentNode(root);
    7172      // parent == null means we should cut out the root node
    72       // => return a random suboperator of the root
     73      // => return a random sub-tree of the root
    7374      if (parent == null) {
    74         // when there are suboperators then replace the old operator with a random suboperator
    75         if (rootOperator.SubOperators.Count > 0) {
    76           rootOperator = rootOperator.SubOperators[random.Next(rootOperator.SubOperators.Count)];
     75        // when there are sub-trees then replace the old tree with a random sub-tree
     76        if (root.SubTrees.Count > 0) {
     77          root = root.SubTrees[random.Next(root.SubTrees.Count)];
    7778
    78           GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
    79           GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
     79          GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
     80          GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
    8081
    81           // this is not really necessary (we can leave it in until the operator is stable)
    82           if (!gardener.IsValidTree(rootOperator)) {
     82          // this is not really necessary (we can leave it in until the tree is stable)
     83          if (!gardener.IsValidTree(root)) {
    8384            throw new InvalidProgramException();
    8485          }
    8586
    8687          // update the variable
    87           scope.GetVariable("OperatorTree").Value = rootOperator;
    88           if (!gardener.IsValidTree(rootOperator)) {
     88          scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root;
     89          if (!gardener.IsValidTree(root)) {
    8990            throw new InvalidProgramException();
    9091          }
    91 
    92 
    9392          // the tree is already initialized so we don't have to schedule initialization operations
    9493          return null;
    9594        } else {
    9695          // create a new random tree
    97           IOperator newOperator;
     96          IFunctionTree newTree;
    9897          if(random.NextDouble() <= balancedTreesRate) {
    99             newOperator = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, true);
     98            newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
    10099          } else {
    101             newOperator = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, false);
     100            newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);
    102101          }
    103102
    104           GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newOperator);
    105           GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newOperator);
     103          GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
     104          GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
    106105
    107           if (!gardener.IsValidTree(newOperator)) {
     106          // update the variable
     107          scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
     108
     109          if (!gardener.IsValidTree(newTree)) {
    108110            throw new InvalidProgramException();
    109111          }
    110112
    111           // update the variable
    112           scope.GetVariable("OperatorTree").Value = newOperator;
    113 
    114           if (!gardener.IsValidTree(newOperator)) {
    115             throw new InvalidProgramException();
    116           }
    117 
    118           // schedule an operation to initialize the whole operator graph
    119           return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperator), scope);
     113          // schedule an operation to initialize the whole tree
     114          return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
    120115        }
    121116      }
    122117
    123       int childIndex = random.Next(parent.SubOperators.Count);
    124       IOperator child = parent.SubOperators[childIndex];
     118      int childIndex = random.Next(parent.SubTrees.Count);
     119      IFunctionTree child = parent.SubTrees[childIndex];
    125120
    126       // match the suboperators of the child with the allowed suboperators of the parent
    127       IOperator[] possibleChilds = gardener.GetAllowedSubOperators(parent, childIndex).SelectMany(allowedOp => child.SubOperators
    128         .Where(subOp => ((StringData)subOp.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data ==
    129           ((StringData)allowedOp.GetVariable(GPOperatorLibrary.TYPE_ID).Value).Data)).ToArray();
    130 
     121      // match the sub-trees of the child with the allowed sub-trees of the parent
     122      IFunction[] possibleChilds = gardener.GetAllowedSubFunctions(parent.Function, childIndex).SelectMany(allowedFun => child.SubTrees
     123        .Where(subTree => allowedFun == subTree.Function)).Select(t=>t.Function).ToArray(); // Actually we should probably use OperatorEqualityComparer here (gkronber 21.04.08)
    131124
    132125      if (possibleChilds.Length > 0) {
    133126        // replace child with a random child of the child
    134         // make a clone to simplify removing obsolete operators from the operator-graph
    135         IOperator selectedChild = (IOperator)possibleChilds[random.Next(possibleChilds.Length)].Clone();       
    136         parent.RemoveSubOperator(childIndex);
    137         parent.AddSubOperator(selectedChild, childIndex);
     127        // make a clone to simplify removing obsolete branches from the function-tree
     128        IFunction selectedChild = possibleChilds[random.Next(possibleChilds.Length)];       
     129        parent.RemoveSubTree(childIndex);
     130        parent.InsertSubTree(childIndex, new FunctionTree(selectedChild));
    138131
    139         if (!gardener.IsValidTree(rootOperator)) {
     132        if (!gardener.IsValidTree(root)) {
    140133          throw new InvalidProgramException();
    141134        }
    142135
    143136        // update the size and height of our tree
    144         GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
    145         GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
     137        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
     138        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
    146139        // don't need to schedule initialization operations
    147140        return null;
    148141      } else {
    149142        // determine the level of the parent
    150         int parentLevel = gardener.GetNodeLevel(rootOperator, parent);
     143        int parentLevel = gardener.GetBranchLevel(root, parent);
    151144
    152145        // first remove the old child (first step essential!)
    153         parent.RemoveSubOperator(childIndex);
     146        parent.RemoveSubTree(childIndex);
    154147        // then determine the number of nodes left over after the child has been removed!
    155         int remainingNodes = gardener.GetTreeSize(rootOperator);
     148        int remainingNodes = gardener.GetTreeSize(root);
    156149
    157         IList<IOperator> allowedOperators = gardener.GetAllowedSubOperators(parent, childIndex);
    158         IOperator newOperatorTree = gardener.CreateRandomTree(allowedOperators, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, true);
     150        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
     151        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel, true);
    159152
    160         parent.AddSubOperator(newOperatorTree, childIndex);
     153        parent.InsertSubTree(childIndex, newFunctionTree);
    161154
    162         GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
    163         GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
     155        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
     156        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
    164157
    165         if (!gardener.IsValidTree(rootOperator)) {
     158        if (!gardener.IsValidTree(root)) {
    166159          throw new InvalidProgramException();
    167160        }
    168161
    169         // schedule an initialization operation for the new operator
    170         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope);
     162        // schedule an initialization operation for the new function-tree
     163        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);
    171164      }
    172165    }
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/DeleteSubTreeManipulation.cs

    r23 r143  
    2626using HeuristicLab.Operators;
    2727using HeuristicLab.Random;
     28using HeuristicLab.Functions;
    2829
    2930namespace HeuristicLab.StructureIdentification {
     
    4243      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
    4344      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
    44       AddVariableInfo(new VariableInfo("OperatorTree", "The tree to mutate", typeof(IOperator), VariableKind.In | VariableKind.Out));
     45      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
    4546      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
    4647      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
     
    4950
    5051    public override IOperation Apply(IScope scope) {
    51       IOperator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, true);
     52      IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
    5253      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
    5354      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
    54 
    5555      TreeGardener gardener = new TreeGardener(random, library);
    56 
    57       IOperator parent = gardener.GetRandomParentNode(rootOperator);
     56      IFunctionTree parent = gardener.GetRandomParentNode(root);
    5857
    5958      // parent==null means the whole tree should be deleted.
    6059      // => return a new minimal random tree
    6160      if(parent == null) {
    62         IOperator newTree = gardener.CreateRandomTree(1, 1, true);
     61        IFunctionTree newTree = gardener.CreateRandomTree(1, 1, true);
    6362
    6463        // check if the tree is ok
     
    7069        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
    7170        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
    72         scope.GetVariable("OperatorTree").Value = newTree;
     71        scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
    7372
    7473        // schedule an operation to initialize the newly created operator
    75         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newTree), scope);
     74        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
    7675      }
    7776
    78       int childIndex = random.Next(parent.SubOperators.Count);
     77      int childIndex = random.Next(parent.SubTrees.Count);
    7978
    8079      int min;
    8180      int max;
    82       gardener.GetMinMaxArity(parent, out min, out max);
    83       if(parent.SubOperators.Count > min) {
    84         parent.RemoveSubOperator(childIndex);
    85         // actually since the next suboperators are shifted in the place of the removed operator
    86         // it might be possible that these suboperators are not allowed in the place of the old operator
     81      gardener.GetMinMaxArity(parent.Function, out min, out max);
     82      if(parent.SubTrees.Count > min) {
     83        parent.RemoveSubTree(childIndex);
     84        // actually since the next sub-trees are shifted in the place of the removed branch
     85        // it might be possible that these sub-trees are not allowed in the place of the old branch
    8786        // we ignore this problem for now.
    88         // when this starts to become a problem a possible solution is to go through the shifted operators from the place of the shifted
     87        // when this starts to become a problem a possible solution is to go through the shifted branches from the place of the shifted
    8988        // and find the first one that doesn't fit. At this position we insert a new randomly initialized subtree of matching type (gkronber 25.12.07)
    9089
    91         if(!gardener.IsValidTree(rootOperator)) {
     90        if(!gardener.IsValidTree(root)) {
    9291          throw new InvalidOperationException();
    9392        }
    9493
    95         GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
    96         GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
    97         // root hasn't changed so don't need to update 'OperatorTree' variable
    98 
     94        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
     95        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
     96        // root hasn't changed so don't need to update 'FunctionTree' variable
    9997        return null;
    10098      } else {
    10199        // replace with a minimal random seedling
    102         parent.RemoveSubOperator(childIndex);
     100        parent.RemoveSubTree(childIndex);
    103101
    104         IList<IOperator> allowedOperators = gardener.GetAllowedSubOperators(parent, childIndex);
    105         IOperator newOperatorTree = gardener.CreateRandomTree(allowedOperators, 1, 1, true);
     102        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
     103        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, 1, 1, true);
    106104
    107         parent.AddSubOperator(newOperatorTree, childIndex);
     105        parent.InsertSubTree(childIndex, newFunctionTree);
    108106
    109         if(!gardener.IsValidTree(rootOperator)) {
     107        if(!gardener.IsValidTree(root)) {
    110108          throw new InvalidProgramException();
    111109        }
    112110
    113         GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
    114         GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
    115         // again the root hasn't changed so we don't need to update the 'OperatorTree' variable
    116 
    117         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope);
     111        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
     112        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
     113        // again the root hasn't changed so we don't need to update the 'FunctionTree' variable
     114        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);
    118115      }
    119116    }
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/FullTreeShaker.cs

    r88 r143  
    2929using HeuristicLab.Data;
    3030using HeuristicLab.Selection;
     31using HeuristicLab.Functions;
    3132
    3233namespace HeuristicLab.StructureIdentification {
     
    3839    public FullTreeShaker()
    3940      : base() {
     41      AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
    4042      AddVariableInfo(new VariableInfo("OperatorLibrary", "Operator library that defines operator mutations", typeof(GPOperatorLibrary), VariableKind.In));
    41       AddVariableInfo(new VariableInfo("OperatorTree", "The operator tree that should be mutated", typeof(IOperator), VariableKind.In));
    42       AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
    43       AddVariableInfo(new VariableInfo("ShakingFactor", "Variable that determines the force of the shakeing operation", typeof(DoubleData), VariableKind.In));
     43      AddVariableInfo(new VariableInfo("ShakingFactor", "Variable that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In));
     44      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
    4445    }
    4546
    4647    public override IOperation Apply(IScope scope) {
    4748      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
    48       IOperator op = GetVariableValue<IOperator>("OperatorTree", scope, false);
     49      IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false);
    4950      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
    5051
     
    5657
    5758      TreeGardener gardener = new TreeGardener(mt, library);
    58       var parametricNodes = gardener.GetAllOperators(op).Where(o => o.GetVariable(GPOperatorLibrary.MANIPULATION) != null);
    59       foreach(IOperator subOperator in parametricNodes) {
    60         IOperator mutation =(IOperator)subOperator.GetVariable(GPOperatorLibrary.MANIPULATION).Value;
     59      var parametricBranches = gardener.GetAllSubTrees(tree).Where(branch => branch.Function.GetVariable(GPOperatorLibrary.MANIPULATION) != null);
     60      foreach(IFunctionTree subTree in parametricBranches) {
     61        IOperator mutation =(IOperator)subTree.Function.GetVariable(GPOperatorLibrary.MANIPULATION).Value;
    6162
    6263        // store all local variables into a temporary scope
    6364        Scope mutationScope = new Scope();
    64         foreach(IVariableInfo info in subOperator.VariableInfos) {
    65           if(info.Local) {
    66             mutationScope.AddVariable(subOperator.GetVariable(info.FormalName));
    67           }
     65        foreach(IVariable variable in subTree.LocalVariables) {
     66          mutationScope.AddVariable(variable);
    6867        }
    6968
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/OnePointShaker.cs

    r88 r143  
    2929using HeuristicLab.Data;
    3030using HeuristicLab.Selection;
     31using HeuristicLab.Functions;
    3132
    3233namespace HeuristicLab.StructureIdentification {
     
    3940      : base() {
    4041      AddVariableInfo(new VariableInfo("OperatorLibrary", "Operator library that defines mutation operations for operators", typeof(GPOperatorLibrary), VariableKind.In));
    41       AddVariableInfo(new VariableInfo("OperatorTree", "The operator tree that should be mutated", typeof(IOperator), VariableKind.In));
    4242      AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
    4343      AddVariableInfo(new VariableInfo("ShakingFactor", "Factor that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In));
     44      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
    4445    }
    4546
    4647    public override IOperation Apply(IScope scope) {
    4748      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
    48       IOperator op = GetVariableValue<IOperator>("OperatorTree", scope, false);
     49      IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false);
    4950      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
    5051      TreeGardener gardener = new TreeGardener(mt, library);
    5152
    5253      // get all nodes for which a manipulation is defined
    53       var parametricNodes = gardener.GetAllOperators(op).Where(o => o.GetVariable(GPOperatorLibrary.MANIPULATION) != null);
    54       IOperator selectedOp = parametricNodes.ElementAt(mt.Next(parametricNodes.Count()));
    55 
    56       IOperator mutation = (IOperator)selectedOp.GetVariable(GPOperatorLibrary.MANIPULATION).Value;
    57 
     54      var parametricBranches = gardener.GetAllSubTrees(tree).Where(branch => branch.Function.GetVariable(GPOperatorLibrary.MANIPULATION) != null);
     55      IFunctionTree selectedBranch = parametricBranches.ElementAt(mt.Next(parametricBranches.Count()));
     56      IOperator mutation = (IOperator)selectedBranch.Function.GetVariable(GPOperatorLibrary.MANIPULATION).Value;
    5857      CompositeOperation next = new CompositeOperation();
    5958
    6059      // store all local variables into a temporary scope
    6160      Scope tempScope = new Scope("Temp. manipulation scope");
    62       // add aliases
    63       foreach(IVariableInfo variableInfo in VariableInfos)
    64         tempScope.AddAlias(variableInfo.FormalName, variableInfo.ActualName);
    65 
    66       foreach(IVariableInfo info in selectedOp.VariableInfos) {
    67         if(info.Local) {
    68           tempScope.AddVariable(selectedOp.GetVariable(info.FormalName));
    69         }
     61      foreach(IVariable variable in selectedBranch.LocalVariables) {
     62        tempScope.AddVariable(variable);
    7063      }
    7164
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs

    r23 r143  
    6969        IOperator newOperatorTree;
    7070        if(random.NextDouble() <= balancedTreesRate) {
    71           newOperatorTree = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, true);
     71          newOperatorTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
    7272        } else {
    73           newOperatorTree = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, false);
     73          newOperatorTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);
    7474        }
    7575
     
    8484       
    8585        // return a CompositeOperation that randomly initializes the new operator
    86         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope);
     86        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newOperatorTree), scope);
    8787      } else {
    8888        // determine a random child of the parent to be replaced
     
    9090
    9191        // get the list of allowed suboperators as the new child
    92         IList<IOperator> allowedOperators = gardener.GetAllowedSubOperators(parent, childIndex);
     92        IList<IOperator> allowedOperators = gardener.GetAllowedSubFunctions(parent, childIndex);
    9393
    9494        if(allowedOperators.Count == 0) {
     
    100100        // calculate the maximum size and height of the new sub-tree based on the location where
    101101        // it will be inserted
    102         int parentLevel = gardener.GetNodeLevel(rootOperator, parent);
     102        int parentLevel = gardener.GetBranchLevel(rootOperator, parent);
    103103
    104104        int maxSubTreeHeight = maxTreeHeight - parentLevel;
     
    127127
    128128        // return a CompositeOperation that randomly initializes all nodes of the new subtree
    129         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope);
     129        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newOperatorTree), scope);
    130130      }
    131131    }
Note: See TracChangeset for help on using the changeset viewer.