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
Location:
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/GPCrossoverBase.cs

    r2202 r2210  
    3131using System.Diagnostics;
    3232using HeuristicLab.Evolutionary;
     33using HeuristicLab.GP.Interfaces;
    3334
    3435namespace HeuristicLab.GP {
     
    3637    public GPCrossoverBase()
    3738      : base() {
    38       AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));
    39       AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
    40       AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New));
    41       AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New));
     39      AddVariableInfo(new VariableInfo("FunctionLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In));
     40      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to crossover", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.New));
    4241    }
    4342
     
    4544
    4645    protected override void Cross(IScope scope, IRandom random) {
    47       FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("OperatorLibrary", scope, true);
     46      FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
    4847      TreeGardener gardener = new TreeGardener(random, opLibrary);
    4948
     
    5150        throw new InvalidOperationException("Number of parents must be exactly two.");
    5251
    53       IFunctionTree parent0 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);
    54       IFunctionTree parent1 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[1], false);
     52      IGeneticProgrammingModel parent0 = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope.SubScopes[0], false);
     53      IGeneticProgrammingModel parent1 = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope.SubScopes[1], false);
    5554
    5655      // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection)
    5756      if (random.NextDouble() < 0.5) {
    58         IFunctionTree tmp = parent0;
     57        IGeneticProgrammingModel tmp = parent0;
    5958        parent0 = parent1;
    6059        parent1 = tmp;
    6160      }
    6261
    63       IFunctionTree child = Cross(scope, gardener, random, parent0, parent1);
     62      IFunctionTree child = Cross(scope, gardener, random, parent0.FunctionTree, parent1.FunctionTree);
    6463      Debug.Assert(gardener.IsValidTree(child));
    65       scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), child));
    66       scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(child.Size)));
    67       scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(child.Height)));
     64      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(child)));
    6865    }
    6966  }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/LangdonHomologousCrossOver.cs

    r835 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/OnePointCrossOver.cs

    r1286 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    5152    internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {
    5253      List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
    53       GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.Size, allowedCrossOverPoints);
     54      GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.GetSize(), allowedCrossOverPoints);
    5455      if (allowedCrossOverPoints.Count > 0) {
    5556        CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
     
    6869        // if the current branch can be attached as a sub-tree to branch0
    6970        if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) &&
    70            branch1.SubTrees[i].Size - branch0.SubTrees[i].Size <= maxNewNodes) {
     71           branch1.SubTrees[i].GetSize() - branch0.SubTrees[i].GetSize() <= maxNewNodes) {
    7172          CrossoverPoint p = new CrossoverPoint();
    7273          p.childIndex = i;
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeConstrictedGPCrossoverBase.cs

    r1286 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    5859          newTree = Cross(gardener, random, (IFunctionTree)tree1.Clone(), (IFunctionTree)tree0.Clone(), maxTreeSize, maxTreeHeight);
    5960        } else newTree = tree0;
    60       } while (newTree.Size > maxTreeSize || newTree.Height > maxTreeHeight);
     61      } while (newTree.GetSize() > maxTreeSize || newTree.GetHeight() > maxTreeHeight);
    6162      return newTree;
    6263    }
    6364
    6465    internal abstract IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight);
    65 
    66     //private IFunctionTree TakeNextParent(IScope scope) {
    67     //  IFunctionTree parent = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);
    68     //  scope.RemoveSubScope(scope.SubScopes[0]);
    69     //  return parent;
    70     //}
    7166  }
    7267}
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeFairCrossOver.cs

    r2202 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    6970    private IFunctionTree GetReplacementBranch(MersenneTwister random, TreeGardener gardener, IFunctionTree intoTree, IFunctionTree parent, int replacedBranchIndex, IFunctionTree fromTree, int maxTreeSize, int maxTreeHeight) {
    7071      IList<IFunction> allowedFunctions = new List<IFunction>(gardener.GetAllowedSubFunctions(parent.Function, replacedBranchIndex));
    71       int removedBranchSize = parent.SubTrees[replacedBranchIndex].Size;
    72       int maxBranchSize = maxTreeSize - (intoTree.Size - removedBranchSize);
     72      int removedBranchSize = parent.SubTrees[replacedBranchIndex].GetSize();
     73      int maxBranchSize = maxTreeSize - (intoTree.GetSize() - removedBranchSize);
    7374      int maxBranchHeight = maxTreeHeight - gardener.GetBranchLevel(intoTree, parent);  // returns 1 if intoTree==parent and 2 if parent is a child of intoTree
    7475      List<int> replacedTrail = GetTrail(intoTree, parent);
     
    8485        double pEqualLength = equalLengthBranches.Count > 0 ? 1.0 / removedBranchSize : 0.0;
    8586        double pLonger;
    86         if (parent.Size == maxTreeSize) {
     87        if (parent.GetSize() == maxTreeSize) {
    8788          pLonger = 0.0;
    8889        } else {
     
    112113    private void FindPossibleBranches(IFunctionTree tree, IList<IFunction> allowedFunctions, int maxBranchSize, int maxBranchHeight, int removedBranchSize,
    113114      List<CrossoverPoint> shorterBranches, List<CrossoverPoint> equalLengthBranches, List<CrossoverPoint> longerBranches, List<int> trail) {
    114       int treeSize = tree.Size;
    115       if (allowedFunctions.Contains(tree.Function) && treeSize <= maxBranchSize && tree.Height <= maxBranchHeight) {
     115      int treeSize = tree.GetSize();
     116      if (allowedFunctions.Contains(tree.Function) && treeSize <= maxBranchSize && tree.GetHeight() <= maxBranchHeight) {
    116117        CrossoverPoint p = new CrossoverPoint();
    117118        p.branchSize = treeSize;
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/StandardCrossOver.cs

    r2202 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
     
    5758
    5859        // calculate the max size and height that the inserted branch can have
    59         int maxInsertedBranchSize = maxTreeSize - (tree0.Size - parent0.SubTrees[replacedChildIndex].Size);
     60        int maxInsertedBranchSize = maxTreeSize - (tree0.GetSize() - parent0.SubTrees[replacedChildIndex].GetSize());
    6061        int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0
    6162
    6263        IList<IFunction> allowedFunctions = new List<IFunction>(gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex));
    63         allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
     64        allowedCrossoverPoints = GetPossibleCrossoverPoints(tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
    6465      } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries);
    6566
     
    7475    }
    7576
    76     private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
     77    private List<IFunctionTree> GetPossibleCrossoverPoints(IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
    7778      List<IFunctionTree> crossoverPoints = new List<IFunctionTree>();
    78       foreach (IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {
    79         if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight)
     79      foreach (IFunctionTree possiblePoint in TreeGardener.GetAllSubTrees(tree)) {
     80        if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.GetSize() <= maxInsertedBranchSize && possiblePoint.GetHeight() <= maxInsertedBranchHeight)
    8081          crossoverPoints.Add(possiblePoint);
    8182      }
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/UniformCrossover.cs

    r1286 r2210  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.GP.Interfaces;
    3233
    3334namespace HeuristicLab.GP {
Note: See TracChangeset for help on using the changeset viewer.