Free cookie consent management tool by TermsFeed Policy Generator

Changeset 707


Ignore:
Timestamp:
11/02/08 22:28:35 (16 years ago)
Author:
gkronber
Message:

added type constraint check for one-point crossover (#305)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs

    r666 r707  
    4545      : base() {
    4646      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
     47      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
    4748      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
    4849      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.New));
     
    5253    public override IOperation Apply(IScope scope) {
    5354      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
     55      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
     56      TreeGardener gardener = new TreeGardener(random, opLibrary);
    5457
    5558      if((scope.SubScopes.Count % 2) != 0)
     
    6568        scope.RemoveSubScope(parent2);
    6669        IScope child = new Scope(i.ToString());
    67         IOperation childInitOperation = Cross(scope, random, parent1, parent2, child);
     70        IOperation childInitOperation = Cross(scope, random, gardener, parent1, parent2, child);
    6871        initOperations.AddOperation(childInitOperation);
    6972        scope.AddSubScope(child);
     
    7376    }
    7477
    75     private IOperation Cross(IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) {
    76       IFunctionTree newTree = Cross(random, parent1, parent2);
     78    private IOperation Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child) {
     79      IFunctionTree newTree = Cross(random, gardener, parent1, parent2);
    7780
    7881      int newTreeSize = newTree.Size;
     
    8689
    8790
    88     private IFunctionTree Cross(MersenneTwister random, IScope f, IScope g) {
     91    private IFunctionTree Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g) {
    8992      IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
    9093      int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
     
    9598      int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data;
    9699
    97       List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(null, tree0, tree1);
     100      List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(gardener, null, tree0, tree1);
    98101      if(allowedCrossOverPoints.Count == 0) {
    99102        if(random.NextDouble() < 0.5) return tree0; else return tree1;
     
    113116    }
    114117
    115     private List<IFunctionTree[]> GetCrossOverPoints(IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) {
     118    private List<IFunctionTree[]> GetCrossOverPoints(TreeGardener gardener, IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) {
    116119      List<IFunctionTree[]> results = new List<IFunctionTree[]>();
    117120      if(tree0.SubTrees.Count != tree1.SubTrees.Count) return results;
    118121      // invariant arity - number of subtrees is equal in both trees
    119 
    120       results.Add(new IFunctionTree[] { parent, tree0, tree1 });
    121122      for(int i = 0; i < tree0.SubTrees.Count; i++) {
    122         results.AddRange(GetCrossOverPoints(tree0, tree0.SubTrees[i], tree1.SubTrees[i]));
     123        if(gardener.GetAllowedSubFunctions(tree0.Function, i).Contains(tree1.SubTrees[i].Function)) {
     124          results.Add(new IFunctionTree[] { tree0, tree0.SubTrees[i], tree1.SubTrees[i]});
     125        }
     126        results.AddRange(GetCrossOverPoints(gardener, tree0, tree0.SubTrees[i], tree1.SubTrees[i]));
    123127      }
    124128      return results;
Note: See TracChangeset for help on using the changeset viewer.