Free cookie consent management tool by TermsFeed Policy Generator

Changeset 803


Ignore:
Timestamp:
11/22/08 16:39:32 (16 years ago)
Author:
gkronber
Message:

improved UniformCrossover #382 (Uniform crossover operator for GP)

File:
1 edited

Legend:

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

    r802 r803  
    7979    private IOperation Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child) {
    8080      IFunctionTree newTree = Cross(random, gardener, parent1, parent2);
    81 
     81      Debug.Assert(gardener.IsValidTree(newTree));
    8282      int newTreeSize = newTree.Size;
    8383      int newTreeHeight = newTree.Height;
     
    100100
    101101      List<CrossoverPoint> allowedCrossOverPoints = GetCrossOverPoints(gardener, tree0, tree1);
    102 
     102      foreach (CrossoverPoint p in allowedCrossOverPoints) {
     103        Debug.Assert(gardener.GetAllowedSubFunctions(p.parent0.Function, p.childIndex).Contains(p.parent1.SubTrees[p.childIndex].Function));
     104        Debug.Assert(gardener.GetAllowedSubFunctions(p.parent1.Function, p.childIndex).Contains(p.parent0.SubTrees[p.childIndex].Function));
     105      }
    103106      // iterate through the list of crossover points and swap nodes with p=0.5
    104107      foreach (CrossoverPoint crossoverPoint in allowedCrossOverPoints) {
     
    109112          IFunctionTree branch1 = crossoverPoint.parent1.SubTrees[crossoverPoint.childIndex];
    110113
     114          // if we are at an internal node of the common region swap only the node but not the subtrees
    111115          if (branch0.SubTrees.Count == branch1.SubTrees.Count) {
    112             // if we are at an internal node of the common region swap only the node but not the subtrees
    113116            if (parent0 != null) {
    114117              Debug.Assert(parent1 != null); Debug.Assert(branch0 != null); Debug.Assert(branch0 != null);
     118              Debug.Assert(gardener.GetAllowedSubFunctions(parent0.Function, crossoverPoint.childIndex).Contains(branch1.Function));
     119              Debug.Assert(gardener.GetAllowedSubFunctions(parent1.Function, crossoverPoint.childIndex).Contains(branch0.Function));
    115120              // we are not at the root => exchange the branches in the parent
    116121              parent0.RemoveSubTree(crossoverPoint.childIndex);
     
    124129            while (branch0.SubTrees.Count > 0) branch0.RemoveSubTree(0); // remove all children
    125130            while (branch1.SubTrees.Count > 0) branch1.RemoveSubTree(0);
    126             foreach (IFunctionTree subTree in branch1Children) branch0.AddSubTree(subTree); // append children of branch1 to branch0
    127             foreach (IFunctionTree subTree in branch0Children) branch1.AddSubTree(subTree); // and vice versa                                         
     131            foreach (IFunctionTree subTree in branch1Children) {
     132              Debug.Assert(gardener.GetAllowedSubFunctions(branch0.Function, branch0.SubTrees.Count).Contains(subTree.Function));
     133              branch0.AddSubTree(subTree); // append children of branch1 to branch0
     134            }
     135            foreach (IFunctionTree subTree in branch0Children) {
     136              Debug.Assert(gardener.GetAllowedSubFunctions(branch1.Function, branch1.SubTrees.Count).Contains(subTree.Function));
     137              branch1.AddSubTree(subTree); // and vice versa
     138            }
    128139          } else {
    129140            // If we are at a node at the border of the common region then exchange the whole branch.
     
    133144            // However if we are not at the root => exchange the branches in the parent
    134145            if (parent0 != null) {
    135               Debug.Assert(parent1 != null); Debug.Assert(branch0 != null); Debug.Assert(branch1 != null);             
     146              Debug.Assert(parent1 != null); Debug.Assert(branch0 != null); Debug.Assert(branch1 != null);
     147              Debug.Assert(gardener.GetAllowedSubFunctions(parent0.Function, crossoverPoint.childIndex).Contains(branch1.Function));
     148              Debug.Assert(gardener.GetAllowedSubFunctions(parent1.Function, crossoverPoint.childIndex).Contains(branch0.Function));
    136149              parent0.RemoveSubTree(crossoverPoint.childIndex);
    137150              parent1.RemoveSubTree(crossoverPoint.childIndex);
     
    156169
    157170      for (int i = 0; i < branch0.SubTrees.Count; i++) {
     171        // if the branches fit to the parent
    158172        if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) &&
    159173          gardener.GetAllowedSubFunctions(branch1.Function, i).Contains(branch0.SubTrees[i].Function)) {
    160           CrossoverPoint p = new CrossoverPoint();
    161           p.childIndex = i;
    162           p.parent0 = branch0;
    163           p.parent1 = branch1;
    164           results.Add(p);
     174          // if the point is at the border of the common region we don't care about the children
     175          // however if the point is not on the border of the common region we also have to check if
     176          // the children of the branches fit together
     177          bool fit = true;
     178          if (branch0.SubTrees[i].SubTrees.Count == branch1.SubTrees[i].SubTrees.Count) {
     179            for (int j = 0; j < branch0.SubTrees[i].SubTrees.Count; j++) {
     180              fit = fit & gardener.GetAllowedSubFunctions(branch0.SubTrees[i].Function, j).Contains(branch1.SubTrees[i].SubTrees[j].Function);
     181              fit = fit & gardener.GetAllowedSubFunctions(branch1.SubTrees[i].Function, j).Contains(branch0.SubTrees[i].SubTrees[j].Function);
     182            }
     183          }
     184          if (fit) {
     185            CrossoverPoint p = new CrossoverPoint();
     186            p.childIndex = i;
     187            p.parent0 = branch0;
     188            p.parent1 = branch1;
     189            results.Add(p);
     190          }
    165191        }
    166192        results.AddRange(GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i]));
Note: See TracChangeset for help on using the changeset viewer.