(genome, **args)
return (sister, brother)
The crossover for GTree, Single Point
The crossover for GTree, Single Point
The crossover for GTree Single Point
def GTreeCrossoverSinglePoint(genome, **args):
   """ The crossover for GTree, Single Point """
   sister = None
   brother = None
   gMom = args["mom"].clone()
   gDad = args["dad"].clone()

   gMom.resetStats()
   gDad.resetStats()

   node_mom_stack = []
   all_mom_nodes = []
   node_mom_tmp = None

   node_dad_stack = []
   all_dad_nodes = []
   node_dad_tmp = None

   node_mom_stack.append(gMom.getRoot())
   node_dad_stack.append(gDad.getRoot())

   while (len(node_mom_stack) > 0) and (len(node_dad_stack) > 0):
      node_mom_tmp = node_mom_stack.pop()
      node_dad_tmp = node_dad_stack.pop()

      if node_mom_tmp != gMom.getRoot():
         all_mom_nodes.append(node_mom_tmp)
         all_dad_nodes.append(node_dad_tmp)

      node_mom_stack.extend(node_mom_tmp.getChilds())
      node_dad_stack.extend(node_dad_tmp.getChilds())

   if len(all_mom_nodes) == 0 or len(all_dad_nodes) == 0:
      return (gMom, gDad)

   if len(all_dad_nodes) == 1:
      nodeDad = all_dad_nodes[0]
   else:
      nodeDad = rand_choice(all_dad_nodes)

   if len(all_mom_nodes) == 1:
      nodeMom = all_mom_nodes[0]
   else:
      nodeMom = rand_choice(all_mom_nodes)

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

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

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

   return (sister, brother)
def GTreeCrossoverSinglePoint ( genome , * * args ) : sister = None brother = None gMom = args [ "mom" ] . clone ( ) gDad = args [ "dad" ] . clone ( ) gMom . resetStats ( ) gDad . resetStats ( ) node_mom_stack = [ ] all_mom_nodes = [ ] node_mom_tmp = None node_dad_stack = [ ] all_dad_nodes = [ ] node_dad_tmp = None node_mom_stack . append ( gMom . getRoot ( ) ) node_dad_stack . append ( gDad . getRoot ( ) ) while ( len ( node_mom_stack ) > 0 ) and ( len ( node_dad_stack ) > 0 ) : node_mom_tmp = node_mom_stack . pop ( ) node_dad_tmp = node_dad_stack . pop ( ) if node_mom_tmp != gMom . getRoot ( ) : all_mom_nodes . append ( node_mom_tmp ) all_dad_nodes . append ( node_dad_tmp ) node_mom_stack . extend ( node_mom_tmp . getChilds ( ) ) node_dad_stack . extend ( node_dad_tmp . getChilds ( ) ) if len ( all_mom_nodes ) == 0 or len ( all_dad_nodes ) == 0 : return ( gMom , gDad ) if len ( all_dad_nodes ) == 1 : nodeDad = all_dad_nodes [ 0 ] else : nodeDad = rand_choice ( all_dad_nodes ) if len ( all_mom_nodes ) == 1 : nodeMom = all_mom_nodes [ 0 ] else : nodeMom = rand_choice ( all_mom_nodes ) nodeMom_parent = nodeMom . getParent ( ) nodeDad_parent = nodeDad . getParent ( ) # Sister if args [ "count" ] >= 1 : sister = gMom nodeDad . setParent ( nodeMom_parent ) nodeMom_parent . replaceChild ( nodeMom , nodeDad ) sister . processNodes ( ) # Brother if args [ "count" ] == 2 : brother = gDad nodeMom . setParent ( nodeDad_parent ) nodeDad_parent . replaceChild ( nodeDad , nodeMom ) brother . processNodes ( ) return ( sister , brother )