(genome, **args)
return (sister, brother)
The crossover of Tree, Strict Single Point

   ..note:: This crossover method creates offspring with restriction of the
            *max_depth* parameter.

   Accepts the *max_attempt* parameter, *max_depth* (required), and
   the distr_leaft (>= 0.0 and <= 1.0), which represents the probability
   of leaf selection when findin random nodes for crossover.
The crossover of Tree, Strict Single Point
The crossover of Tree Strict Single Point
def GTreeCrossoverSinglePointStrict(genome, **args):
   """ The crossover of Tree, Strict Single Point

   ..note:: This crossover method creates offspring with restriction of the
            *max_depth* parameter.

   Accepts the *max_attempt* parameter, *max_depth* (required), and
   the distr_leaft (>= 0.0 and <= 1.0), which represents the probability
   of leaf selection when findin random nodes for crossover.

   """
   sister = None
   brother = None

   gMom = args["mom"].clone()
   gDad = args["dad"].clone()

   gMom.resetStats()
   gDad.resetStats()

   max_depth = gMom.getParam("max_depth", None)
   max_attempt = gMom.getParam("max_attempt", 10)
   distr_leaf = gMom.getParam("distr_leaf", None)

   if max_depth is None:
      Util.raiseException("You must specify the max_depth genome parameter !", ValueError)

   if max_depth < 0:
      Util.raiseException("The max_depth must be >= 1, if you want to use GTreeCrossoverSinglePointStrict crossover !", ValueError)

   momRandom = None
   dadRandom = None

   for i in xrange(max_attempt):

      if distr_leaf is None:
         dadRandom = gDad.getRandomNode()
         momRandom = gMom.getRandomNode()
      else:
         if Util.randomFlipCoin(distr_leaf):
            momRandom = gMom.getRandomNode(1)
         else:
            momRandom = gMom.getRandomNode(2)

         if Util.randomFlipCoin(distr_leaf):
            dadRandom = gDad.getRandomNode(1)
         else:
            dadRandom = gDad.getRandomNode(2)

      assert momRandom is not None
      assert dadRandom is not None

      # Optimize here
      mH = gMom.getNodeHeight(momRandom)
      dH = gDad.getNodeHeight(dadRandom)

      mD = gMom.getNodeDepth(momRandom)
      dD = gDad.getNodeDepth(dadRandom)

      # The depth of the crossover is greater than the max_depth
      if (dD + mH <= max_depth) and (mD + dH <= max_depth):
         break

   if i == (max_attempt - 1):
      assert gMom.getHeight() <= max_depth
      return (gMom, gDad)
   else:
      nodeMom, nodeDad = momRandom, dadRandom

   nodeMom_parent = nodeMom.getParent()
   nodeDad_parent = nodeDad.getParent()

   # Sister
   if args["count"] >= 1:
      sister = gMom
      nodeDad.setParent(nodeMom_parent)

      if nodeMom_parent is None:
         sister.setRoot(nodeDad)
      else:
         nodeMom_parent.replaceChild(nodeMom, nodeDad)
      sister.processNodes()
      assert sister.getHeight() <= max_depth

   # Brother
   if args["count"] == 2:
      brother = gDad
      nodeMom.setParent(nodeDad_parent)

      if nodeDad_parent is None:
         brother.setRoot(nodeMom)
      else:
         nodeDad_parent.replaceChild(nodeDad, nodeMom)
      brother.processNodes()
      assert brother.getHeight() <= max_depth

   return (sister, brother)
def GTreeCrossoverSinglePointStrict ( genome , * * args ) : sister = None brother = None gMom = args [ "mom" ] . clone ( ) gDad = args [ "dad" ] . clone ( ) gMom . resetStats ( ) gDad . resetStats ( ) max_depth = gMom . getParam ( "max_depth" , None ) max_attempt = gMom . getParam ( "max_attempt" , 10 ) distr_leaf = gMom . getParam ( "distr_leaf" , None ) if max_depth is None : Util . raiseException ( "You must specify the max_depth genome parameter !" , ValueError ) if max_depth < 0 : Util . raiseException ( "The max_depth must be >= 1, if you want to use GTreeCrossoverSinglePointStrict crossover !" , ValueError ) momRandom = None dadRandom = None for i in xrange ( max_attempt ) : if distr_leaf is None : dadRandom = gDad . getRandomNode ( ) momRandom = gMom . getRandomNode ( ) else : if Util . randomFlipCoin ( distr_leaf ) : momRandom = gMom . getRandomNode ( 1 ) else : momRandom = gMom . getRandomNode ( 2 ) if Util . randomFlipCoin ( distr_leaf ) : dadRandom = gDad . getRandomNode ( 1 ) else : dadRandom = gDad . getRandomNode ( 2 ) assert momRandom is not None assert dadRandom is not None # Optimize here mH = gMom . getNodeHeight ( momRandom ) dH = gDad . getNodeHeight ( dadRandom ) mD = gMom . getNodeDepth ( momRandom ) dD = gDad . getNodeDepth ( dadRandom ) # The depth of the crossover is greater than the max_depth if ( dD + mH <= max_depth ) and ( mD + dH <= max_depth ) : break if i == ( max_attempt - 1 ) : assert gMom . getHeight ( ) <= max_depth return ( gMom , gDad ) else : nodeMom , nodeDad = momRandom , dadRandom nodeMom_parent = nodeMom . getParent ( ) nodeDad_parent = nodeDad . getParent ( ) # Sister if args [ "count" ] >= 1 : sister = gMom nodeDad . setParent ( nodeMom_parent ) if nodeMom_parent is None : sister . setRoot ( nodeDad ) else : nodeMom_parent . replaceChild ( nodeMom , nodeDad ) sister . processNodes ( ) assert sister . getHeight ( ) <= max_depth # Brother if args [ "count" ] == 2 : brother = gDad nodeMom . setParent ( nodeDad_parent ) if nodeDad_parent is None : brother . setRoot ( nodeMom ) else : nodeDad_parent . replaceChild ( nodeDad , nodeMom ) brother . processNodes ( ) assert brother . getHeight ( ) <= max_depth return ( sister , brother )