Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/29/09 18:28:45 (15 years ago)
Author:
gkronber
Message:

GP Refactoring #713

  • introduced a plugin for GP interfaces
  • created a new interface IGeneticProgrammingModel which represents GP models in HL scopes instead of IFunctionTree
  • changed interfaces IFunction and IFunctionTree
  • moved some files to new directories (general housekeeping)
  • changed all GP operators and engines to work with IGeneticProgrammingModels
  • removed parameters TreeSize and TreeHeight in all GP operators
  • changed parameter OperatorLibrary to FunctionLibrary in all GP operators
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/CutOutNodeManipulation.cs

    r2202 r2210  
    2828using System;
    2929using System.Diagnostics;
     30using HeuristicLab.GP.Interfaces;
    3031
    3132namespace HeuristicLab.GP {
    32   public class CutOutNodeManipulation : OperatorBase {
     33  public class CutOutNodeManipulation : GPManipulatorBase {
    3334    public override string Description {
    3435      get {
     
    4950    public CutOutNodeManipulation()
    5051      : base() {
    51       AddVariableInfo(new VariableInfo("Random", "Uniform random number generator", typeof(MersenneTwister), VariableKind.In));
    52       AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));
    53       AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
    54       AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
    55       AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
    56       AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
    57       AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.Out));
    5852    }
    5953
    6054
    61     public override IOperation Apply(IScope scope) {
    62       IFunctionTree root = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
    63       MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
    64       FunctionLibrary library = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);
    65       int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
    66       int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
     55    internal override IOperation Manipulate(MersenneTwister random, IGeneticProgrammingModel gpModel, FunctionLibrary library, int maxTreeSize, int maxTreeHeight, IScope scope) {
    6756      TreeGardener gardener = new TreeGardener(random, library);
    68       IFunctionTree parent = gardener.GetRandomParentNode(root);
     57      IFunctionTree parent = gardener.GetRandomParentNode(gpModel.FunctionTree);
    6958      // parent == null means we should cut out the root node
    7059      // => return a random sub-tree of the root
    7160      if (parent == null) {
    7261        // when there are sub-trees then replace the old tree with a random sub-tree
    73         if (root.SubTrees.Count > 0) {
    74           root = root.SubTrees[random.Next(root.SubTrees.Count)];
    75           GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;
    76           GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;
    77           // update the variable
    78           scope.GetVariable(scope.TranslateName("FunctionTree")).Value = root;
    79           Debug.Assert(gardener.IsValidTree(root));
     62        if (gpModel.FunctionTree.SubTrees.Count > 0) {
     63          gpModel.FunctionTree = gpModel.FunctionTree.SubTrees[random.Next(gpModel.FunctionTree.SubTrees.Count)];
     64          Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
    8065          // we reused a sub-tree so we don't have to schedule initialization operations
    8166          return null;
    8267        } else {
    8368          // we want to cut the root node and there are no sub-trees => create a new random terminal
    84           IFunctionTree newTree;
    85           newTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1);
    86           GetVariableValue<IntData>("TreeSize", scope, true).Data = newTree.Size;
    87           GetVariableValue<IntData>("TreeHeight", scope, true).Data = newTree.Height;
    88           // update the variable
    89           scope.GetVariable(scope.TranslateName("FunctionTree")).Value = newTree;
    90           Debug.Assert(gardener.IsValidTree(newTree));
     69          gpModel.FunctionTree = gardener.CreateRandomTree(gardener.Terminals, 1, 1);
     70          Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
     71
    9172          // schedule an operation to initialize the whole tree
    92           return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newTree), scope);
     73          return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(gpModel.FunctionTree), scope);
    9374        }
    9475      }
     76
    9577      // select a child to cut away
    9678      int childIndex = random.Next(parent.SubTrees.Count);
     
    10183      if (possibleChilds.Length > 0) {
    10284        // replace child with a random child of that child
    103         IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)];       
     85        IFunctionTree selectedChild = possibleChilds[random.Next(possibleChilds.Length)];
    10486        parent.RemoveSubTree(childIndex);
    10587        parent.InsertSubTree(childIndex, selectedChild);
    106         Debug.Assert(gardener.IsValidTree(root));
    107         // update the size and height of our tree
    108         GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;
    109         GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;
     88        Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
     89        // recalculate the size and height of our tree
     90        gpModel.Size = gpModel.FunctionTree.GetSize();
     91        gpModel.Height = gpModel.FunctionTree.GetHeight();
    11092        // don't need to schedule initialization operations
    11193        return null;
     
    11395        // can't reuse an existing branch => create a new tree
    11496        // determine the level of the parent
    115         int parentLevel = gardener.GetBranchLevel(root, parent);
     97        int parentLevel = gardener.GetBranchLevel(gpModel.FunctionTree, parent);
    11698        // first remove the old child (first step essential!)
    11799        parent.RemoveSubTree(childIndex);
    118100        // then determine the number of nodes left over after the child has been removed!
    119         int remainingNodes = root.Size;
     101        int remainingNodes = gpModel.FunctionTree.GetSize();
    120102        allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, childIndex);
    121103        IFunctionTree newFunctionTree = gardener.CreateRandomTree(allowedFunctions, maxTreeSize - remainingNodes, maxTreeHeight - parentLevel);
    122104        parent.InsertSubTree(childIndex, newFunctionTree);
    123         GetVariableValue<IntData>("TreeSize", scope, true).Data = root.Size;
    124         GetVariableValue<IntData>("TreeHeight", scope, true).Data = root.Height;
    125         Debug.Assert(gardener.IsValidTree(root));
     105        Debug.Assert(gardener.IsValidTree(gpModel.FunctionTree));
     106        // recalculate size and height
     107        gpModel.Size = gpModel.FunctionTree.GetSize();
     108        gpModel.Height = gpModel.FunctionTree.GetHeight();
    126109        // schedule an initialization operation for the new function-tree
    127         return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(newFunctionTree), scope);
     110        return TreeGardener.CreateInitializationOperation(TreeGardener.GetAllSubTrees(newFunctionTree), scope);
    128111      }
    129112    }
Note: See TracChangeset for help on using the changeset viewer.