Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/21/08 18:39:38 (17 years ago)
Author:
gkronber
Message:

fixed compile errors (#112)

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

Legend:

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

    r143 r144  
    7575          // no parent means the new child is the initial operator
    7676          // and we have to update the value in the variable
    77           scope.GetVariable("FunctionTree").Value = newTerminal;
     77          scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTerminal;
    7878        } else {
    7979          parent.RemoveSubTree(selectedChildIndex);
     
    9292          // no parent means the new function is the initial operator
    9393          // and we have to update the value in the variable
    94           scope.GetVariable("FunctionTree").Value = newFunction;
     94          scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newFunction;
    9595          root = newFunction;
    9696        } else {
     
    124124    private IFunctionTree ChangeTerminalType(IFunctionTree parent, IFunctionTree child, int childIndex, TreeGardener gardener, MersenneTwister random) {
    125125
    126       IList<IOperator> allowedChildren;
     126      IList<IFunction> allowedChildren;
    127127      if (parent == null) {
    128128        allowedChildren = gardener.Terminals;
    129129      } else {
    130130        SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser();
    131         analyser.AllPossibleOperators = gardener.Terminals;
    132         allowedChildren = analyser.GetAllowedOperators(parent, childIndex);
     131        analyser.AllPossibleOperators = gardener.Terminals.Cast<IOperator>().ToArray();
     132        allowedChildren = analyser.GetAllowedOperators(parent.Function, childIndex).Cast<IFunction>().ToList();
    133133      }
    134134
    135135      // selecting from the terminals should always work since the current child was also a terminal
    136136      // so in the worst case we will just create a new terminal of the same type again.
    137       return gardener.CreateRandomTree((IOperator)allowedChildren[random.Next(allowedChildren.Count)].Clone(), 1, 1, false);
     137      return gardener.CreateRandomTree(allowedChildren[random.Next(allowedChildren.Count)], 1, 1, false);
    138138    }
    139139
     
    143143      // and how many of the existing subtrees we can reuse
    144144
    145       // let's choose the operator we want to use instead of the old child. For this we have to determine the
    146       // pool of allowed operators based on constraints of the parent if there is one.
    147       IList<IOperator> allowedSubOperators;
     145      // let's choose the function we want to use instead of the old child. For this we have to determine the
     146      // pool of allowed functions based on constraints of the parent if there is one.
     147      IList<IFunction> allowedFunctions;
    148148      SubOperatorsConstraintAnalyser analyser = new SubOperatorsConstraintAnalyser();
    149       analyser.AllPossibleOperators = gardener.AllFunctions;
     149      analyser.AllPossibleOperators = gardener.AllFunctions.Cast<IOperator>().ToArray();
    150150      if (parent == null) {
    151         allowedSubOperators = gardener.AllFunctions;
    152       } else {
    153         allowedSubOperators = analyser.GetAllowedOperators(parent, childIndex);
     151        allowedFunctions = gardener.AllFunctions;
     152      } else {
     153        allowedFunctions = analyser.GetAllowedOperators(parent.Function, childIndex).Cast<IFunction>().ToList();
    154154      }
    155155
     
    160160      int maxArity;
    161161
    162       allowedSubOperators = allowedSubOperators.Where(f => {
     162      allowedFunctions = allowedFunctions.Where(f => {
    163163        gardener.GetMinMaxArity(f, out minArity, out maxArity);
    164164        return minArity <= actualArity;
    165165      }).ToList();
    166166
    167       IFunctionTree newTree = new FunctionTree((IOperator)allowedSubOperators[random.Next(allowedSubOperators.Count)]);
    168 
    169       gardener.GetMinMaxArity(newOperator, out minArity, out maxArity);
    170       // if the old child had too many sub-operators then make the new child with the maximal arity
     167      IFunctionTree newTree = new FunctionTree(allowedFunctions[random.Next(allowedFunctions.Count)]);
     168
     169      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
    171171      if (actualArity > maxArity)
    172172        actualArity = maxArity;
     
    180180      // create a list that holds old sub-trees that we can reuse in the new tree
    181181      List<IFunctionTree> availableSubTrees = new List<IFunctionTree>(child.SubTrees);
    182       List<IFunctionTree> freshSubTrees = new List<IFunctionTree>() { new FunctionTree(newOperator) };
    183 
    184       // randomly select the suboperators that we keep
     182      List<IFunctionTree> freshSubTrees = new List<IFunctionTree>() { newTree };
     183
     184      // randomly select the sub-trees that we keep
    185185      for (int i = 0; i < actualArity; i++) {
    186         // fill all sub-operator slots of the new operator
    187         // if for a given slot i there are existing sub-operators that can be used in that slot
    188         // then use a random existing sub-operator. When there are no existing sub-operators
     186        // 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
     188        // then use a random existing sub-tree. When there are no existing sub-trees
    189189        // that fit in the given slot then create a new random tree and use it for the slot
    190         IList<IOperator> allowedOperators = analyser.GetAllowedOperators(newOperator, i);
     190        IList<IFunction> allowedOperators = analyser.GetAllowedOperators(newTree.Function, i).Cast<IFunction>().ToList();
    191191        var matchingSubTrees = availableSubTrees.Where(subTree => allowedOperators.Contains(subTree.Function));
    192192
    193193        if (matchingSubTrees.Count() > 0) {
    194           IFunctionTree selectedSubTree = matchingSubTrees.ElementAt(random.Next(matchingTrees.Count()));
    195           // we can just add it as suboperator
     194          IFunctionTree selectedSubTree = matchingSubTrees.ElementAt(random.Next(matchingSubTrees.Count()));
     195          // we can just add it as subtree
    196196          newTree.InsertSubTree(i, selectedSubTree);
    197197          availableSubTrees.Remove(selectedSubTree); // the branch shouldn't be available for the following slots
  • branches/FunctionsAndStructIdRefactoring/HeuristicLab.StructureIdentification/Manipulation/SubstituteSubTreeManipulation.cs

    r143 r144  
    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.AllFunctions, maxTreeSize, maxTreeHeight, true);
     70          newTree = gardener.CreateRandomTree(gardener.AllFunctions, maxTreeSize, maxTreeHeight, true);
    7271        } else {
    73           newOperatorTree = gardener.CreateRandomTree(gardener.AllFunctions, 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.GetAllSubTrees(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.GetAllowedSubFunctions(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        IList<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.GetBranchLevel(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        IFunctionTree oldChild = parent.SubTrees[childIndex];
     113        parent.RemoveSubTree(childIndex);
     114        parent.InsertSubTree(childIndex, newTree);
    118115
    119         if(!gardener.IsValidTree(rootOperator)) {
     116        if(!gardener.IsValidTree(root)) {
    120117          throw new InvalidProgramException();
    121118        }
    122119
    123120        // 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 
     121        GetVariableValue<IntData>("TreeSize", scope, true).Data = gardener.GetTreeSize(root);
     122        GetVariableValue<IntData>("TreeHeight", scope, true).Data = gardener.GetTreeHeight(root);
     123        // the root hasn't changed so we don't need to update
    128124        // return a CompositeOperation that randomly initializes all nodes of the new subtree
    129         return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newOperatorTree), scope);
     125        return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
    130126      }
    131127    }
Note: See TracChangeset for help on using the changeset viewer.