#NEWLINE# def heuristic_crossover( random , candidates , args ) : #NEWLINE# #INDENT# "Return the offspring of heuristic crossover on the candidates.\n\n    It performs heuristic crossover (HX), which is similar to the \n    update rule used in particle swarm optimization. This function \n    also makes use of the bounder function as specified in the EC's \n    ``evolve`` method.\n\n    .. note::\n    \n       This function assumes that candidates can be pickled (for hashing \n       as keys to a dictionary).\n\n    .. Arguments:\n       random -- the random number generator object\n       candidates -- the candidate solutions\n       args -- a dictionary of keyword arguments\n\n    Optional keyword arguments in args:\n    \n    - *crossover_rate* -- the rate at which crossover is performed \n      (default 1.0)\n    \n    " #NEWLINE# crossover_rate = args . setdefault ( 'crossover_rate' , 1.0 ) #NEWLINE# bounder = args [ '_ec' ] . bounder #NEWLINE# if ( ( len ( candidates ) % 2 ) == 1 ) : #NEWLINE# #INDENT# candidates = candidates [ : ( - 1 ) ] #NEWLINE# #UNINDENT# population = list ( args [ '_ec' ] . population ) #NEWLINE# lookup = dict ( zip ( [ pickle . dumps ( p . candidate , 1 ) for p in population ] , population ) ) #NEWLINE# moms = random [ : : 2 ] #NEWLINE# dads = candidates [ 1 : : 2 ] #NEWLINE# children = [ ] #NEWLINE# for ( mom , dad ) in zip ( moms , dads ) : #NEWLINE# #INDENT# if ( random . random ( ) < crossover_rate ) : #NEWLINE# #INDENT# bro = copy . copy ( dad ) #NEWLINE# sis = copy . copy ( mom ) #NEWLINE# mom_is_better = ( lookup [ pickle . dumps ( mom , 1 ) ] > lookup [ pickle . dumps ( dad , 1 ) ] ) #NEWLINE# for ( i , ( m , d ) ) in enumerate ( zip ( mom , dad ) ) : #NEWLINE# #INDENT# negpos = ( 1 if mom_is_better else ( - 1 ) ) #NEWLINE# val = ( d if mom_is_better else m ) #NEWLINE# bro [ i ] = ( val + ( ( random . random ( ) * negpos ) * ( m - d ) ) ) #NEWLINE# sis [ i ] = ( val + ( ( random . random ( ) * negpos ) * ( m - d ) ) ) #NEWLINE# #UNINDENT# bro = bounder ( bro , args ) #NEWLINE# sis = bounder ( sis , args ) #NEWLINE# children . append ( bro ) #NEWLINE# children . append ( sis ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# children . append ( mom ) #NEWLINE# children . append ( dad ) #NEWLINE# #UNINDENT# #UNINDENT# return children