Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/22/08 18:05:14 (17 years ago)
Author:
gkronber
Message:

merged FunctionsAndStructIdRefactoring-branch (r142, r143, r144, r145, r146, r147, r148, r149, r152, r153) back into the trunk (ticket #112)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs

    r23 r155  
    2727using HeuristicLab.Operators;
    2828using HeuristicLab.Random;
     29using HeuristicLab.Functions;
    2930
    3031namespace HeuristicLab.StructureIdentification {
     
    4243      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
    4344      AddVariableInfo(new VariableInfo("BalancedTreesRate", "Determines how many trees should be balanced", typeof(DoubleData), VariableKind.In));
    44       AddVariableInfo(new VariableInfo("OperatorTree", "The tree to manipulate", typeof(IOperator), VariableKind.In));
    45       AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
    46       AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In));
     45      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to manipulate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
     46      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
     47      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
    4748    }
    4849
    4950    public override IOperation Apply(IScope scope) {
    50       IOperator rootOperator = GetVariableValue<IOperator>("OperatorTree", scope, true);
    51 
     51      IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
    5252      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
    5353      GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
     
    6060      TreeGardener gardener = new TreeGardener(random, library);
    6161
    62       IOperator parent = gardener.GetRandomParentNode(rootOperator);
     62      IFunctionTree parent = gardener.GetRandomParentNode(root);
    6363      if(parent == null) {
    6464        // parent == null means we should subsitute the whole tree
    6565        // => create a new random tree
    6666
    67         // create a new random operator tree
    68 
    69         IOperator newOperatorTree;
     67        // create a new random function tree
     68        IFunctionTree newTree;
    7069        if(random.NextDouble() <= balancedTreesRate) {
    71           newOperatorTree = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, true);
     70          newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
    7271        } else {
    73           newOperatorTree = gardener.CreateRandomTree(gardener.AllOperators, maxTreeSize, maxTreeHeight, false);
     72          newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, false);
    7473        }
    7574
    76         if(!gardener.IsValidTree(newOperatorTree)) {
     75        if(!gardener.IsValidTree(newTree)) {
    7776          throw new InvalidProgramException();
    7877        }
    7978
    8079        // update the variables in the scope with the new values
    81         GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newOperatorTree);
    82         GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newOperatorTree);
    83         scope.GetVariable("OperatorTree").Value = newOperatorTree;
     80        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(newTree);
     81        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(newTree);
     82        scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
    8483       
    85         // return a CompositeOperation that randomly initializes the new operator
    86         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope);
     84        // return a CompositeOperation that randomly initializes the new tree
     85        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
    8786      } else {
    8887        // determine a random child of the parent to be replaced
    89         int childIndex = random.Next(parent.SubOperators.Count);
    90 
    91         // get the list of allowed suboperators as the new child
    92         IList<IOperator> allowedOperators = gardener.GetAllowedSubOperators(parent, childIndex);
    93 
    94         if(allowedOperators.Count == 0) {
     88        int childIndex = random.Next(parent.SubTrees.Count);
     89        // get the list of allowed functions for the new sub-tree
     90        ICollection<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
     91        if(allowedFunctions.Count == 0) {
    9592          // don't change anything
    9693          // this shouldn't happen
     
    10097        // calculate the maximum size and height of the new sub-tree based on the location where
    10198        // it will be inserted
    102         int parentLevel = gardener.GetNodeLevel(rootOperator, parent);
     99        int parentLevel = gardener.GetBranchLevel(root, parent);
    103100
    104101        int maxSubTreeHeight = maxTreeHeight - parentLevel;
    105         int maxSubTreeSize = maxTreeSize - (treeSize - gardener.GetTreeSize(parent.SubOperators[childIndex]));
     102        int maxSubTreeSize = maxTreeSize - (treeSize - gardener.GetTreeSize(parent.SubTrees[childIndex]));
    106103
    107         // get a random operatorTree
    108         IOperator newOperatorTree;
     104        // create a random function tree
     105        IFunctionTree newTree;
    109106        if(random.NextDouble() <= balancedTreesRate) {
    110           newOperatorTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, true);
     107          newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, true);
    111108        } else {
    112           newOperatorTree = gardener.CreateRandomTree(allowedOperators, maxSubTreeSize, maxSubTreeHeight, false);
     109          newTree = gardener.CreateRandomTree(allowedFunctions, maxSubTreeSize, maxSubTreeHeight, false);
    113110        }
    114111
    115         IOperator oldChild = parent.SubOperators[childIndex];
    116         parent.RemoveSubOperator(childIndex);
    117         parent.AddSubOperator(newOperatorTree, childIndex);
     112        parent.RemoveSubTree(childIndex);
     113        parent.InsertSubTree(childIndex, newTree);
    118114
    119         if(!gardener.IsValidTree(rootOperator)) {
     115        if(!gardener.IsValidTree(root)) {
    120116          throw new InvalidProgramException();
    121117        }
    122118
    123119        // update the values of treeSize and treeHeight
    124         GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(rootOperator);
    125         GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(rootOperator);
    126         // the root operator hasn't changed so we don't need to update
    127 
     120        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
     121        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
     122        // the root hasn't changed so we don't need to update
    128123        // return a CompositeOperation that randomly initializes all nodes of the new subtree
    129         return gardener.CreateInitializationOperation(gardener.GetAllOperators(newOperatorTree), scope);
     124        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
    130125      }
    131126    }
Note: See TracChangeset for help on using the changeset viewer.