Free cookie consent management tool by TermsFeed Policy Generator

Changeset 807


Ignore:
Timestamp:
11/23/08 19:29:26 (16 years ago)
Author:
gkronber
Message:

Changed UniformCrossover to create two new trees from two parents. This shouldn't alter the search behavior because UniformCrossover is symmetric. #382 (Uniform crossover operator for GP)

File:
1 edited

Legend:

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

    r803 r807  
    6262      CompositeOperation initOperations = new CompositeOperation();
    6363
    64       int children = scope.SubScopes.Count / 2;
    65       for (int i = 0; i < children; i++) {
     64      int crossoverEvents = scope.SubScopes.Count / 2;
     65      for (int i = 0; i < crossoverEvents; i++) {
    6666        IScope parent1 = scope.SubScopes[0];
    6767        scope.RemoveSubScope(parent1);
    6868        IScope parent2 = scope.SubScopes[0];
    6969        scope.RemoveSubScope(parent2);
    70         IScope child = new Scope(i.ToString());
    71         IOperation childInitOperation = Cross(scope, random, gardener, parent1, parent2, child);
    72         initOperations.AddOperation(childInitOperation);
    73         scope.AddSubScope(child);
    74       }
    75 
    76       return initOperations;
    77     }
    78 
    79     private IOperation Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child) {
    80       IFunctionTree newTree = Cross(random, gardener, parent1, parent2);
    81       Debug.Assert(gardener.IsValidTree(newTree));
    82       int newTreeSize = newTree.Size;
    83       int newTreeHeight = newTree.Height;
    84       child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree));
    85       child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTreeSize)));
    86       child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight)));
     70        IScope child0 = new Scope((i*2).ToString());
     71        IScope child1 = new Scope((i*2+1).ToString());
     72        Cross(scope, random, gardener, parent1, parent2, child0, child1);
     73        scope.AddSubScope(child0);
     74        scope.AddSubScope(child1);
     75      }
    8776
    8877      return null;
    8978    }
    9079
    91 
    92     private IFunctionTree Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g) {
     80    private void Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child0, IScope child1) {
     81      IFunctionTree childTree0;
     82      IFunctionTree childTree1;
     83      Cross(random, gardener, parent1, parent2, out childTree0, out childTree1);
     84      Debug.Assert(gardener.IsValidTree(childTree0));
     85      Debug.Assert(gardener.IsValidTree(childTree1));
     86      child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), childTree0));
     87      child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(childTree0.Size)));
     88      child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(childTree0.Height)));
     89
     90      child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), childTree1));
     91      child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(childTree1.Size)));
     92      child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(childTree1.Height)));
     93    }
     94
     95
     96    private void Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g, out IFunctionTree child0, out IFunctionTree child1) {
    9397      IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
    9498      int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
     
    155159        }
    156160      }
    157       if (random.NextDouble() < 0.5) return tree0; else return tree1;
     161      if (random.NextDouble() < 0.5) {
     162        child0 = tree0;
     163        child1 = tree1;
     164      } else {
     165        child0 = tree1;
     166        child1 = tree0;
     167      }
    158168    }
    159169
Note: See TracChangeset for help on using the changeset viewer.