<s> """<STR_LIT>""" <EOL> import copy <EOL> import functools <EOL> def mutator ( mutate ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( mutate ) <EOL> def inspyred_mutator ( random , candidates , args ) : <EOL> mutants = [ ] <EOL> for i , cs in enumerate ( candidates ) : <EOL> mutants . append ( mutate ( random , cs , args ) ) <EOL> return mutants <EOL> inspyred_mutator . single_mutation = mutate <EOL> return inspyred_mutator <EOL> @ mutator <EOL> def bit_flip_mutation ( random , candidate , args ) : <EOL> """<STR_LIT>""" <EOL> rate = args . setdefault ( '<STR_LIT>' , <NUM_LIT:0.1> ) <EOL> mutant = copy . copy ( candidate ) <EOL> if len ( mutant ) == len ( [ x for x in mutant if x in [ <NUM_LIT:0> , <NUM_LIT:1> ] ] ) : <EOL> for i , m in enumerate ( mutant ) : <EOL> if random . random ( ) < rate : <EOL> mutant [ i ] = ( m + <NUM_LIT:1> ) % <NUM_LIT:2> <EOL> return mutant <EOL> @ mutator <EOL> def random_reset_mutation ( random , candidate , args ) : <EOL> """<STR_LIT>""" <EOL> bounder = args [ '<STR_LIT>' ] . bounder <EOL> try : <EOL> values = bounder . values <EOL> except AttributeError : <EOL> values = None <EOL> if values is not None : <EOL> rate = args . setdefault ( '<STR_LIT>' , <NUM_LIT:0.1> ) <EOL> mutant = copy . copy ( candidate ) <EOL> for i , m in enumerate ( mutant ) : <EOL> if random . random ( ) < rate : <EOL> mutant [ i ] = random . choice ( values ) <EOL> return mutant <EOL> else : <EOL> return candidate <EOL> @ mutator <EOL> def scramble_mutation ( random , candidate , args ) : <EOL> """<STR_LIT>""" <EOL> rate = args . setdefault ( '<STR_LIT>' , <NUM_LIT:0.1> ) <EOL> if random . random ( ) < rate : <EOL> size = len ( candidate ) <EOL> p = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> q = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> p , q = min ( p , q ) , max ( p , q ) <EOL> s = candidate [ p : q + <NUM_LIT:1> ] <EOL> random . shuffle ( s ) <EOL> return candidate [ : p ] + s [ : : - <NUM_LIT:1> ] + candidate [ q + <NUM_LIT:1> : ] <EOL> else : <EOL> return candidate <EOL> @ mutator <EOL> def inversion_mutation ( random , candidate , args ) : <EOL> """<STR_LIT>""" <EOL> rate = args . setdefault ( '<STR_LIT>' , <NUM_LIT:0.1> ) <EOL> if random . random ( ) < rate : <EOL> size = len ( candidate ) <EOL> p = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> q = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> p , q = min ( p , q ) , max ( p , q ) <EOL> s = candidate [ p : q + <NUM_LIT:1> ] <EOL> return candidate [ : p ] + s [ : : - <NUM_LIT:1> ] + candidate [ q + <NUM_LIT:1> : ] <EOL> else : <EOL> return candidate <EOL> @ mutator <EOL> def gaussian_mutation ( random , candidate , args ) : <EOL> """<STR_LIT>""" <EOL> mut_rate = args . setdefault ( '<STR_LIT>' , <NUM_LIT:0.1> ) <EOL> mean = args . setdefault ( '<STR_LIT>' , <NUM_LIT:0.0> ) <EOL> stdev = args . setdefault ( '<STR_LIT>' , <NUM_LIT:1.0> ) <EOL> bounder = args [ '<STR_LIT>' ] . bounder <EOL> mutant = copy . copy ( candidate ) <EOL> for i , m in enumerate ( mutant ) : <EOL> if random . random ( ) < mut_rate : <EOL> mutant [ i ] += random . gauss ( mean , stdev ) <EOL> mutant = bounder ( mutant , args ) <EOL> return mutant <EOL> @ mutator <EOL> def nonuniform_mutation ( random , candidate , args ) : <EOL> """<STR_LIT>""" <EOL> bounder = args [ '<STR_LIT>' ] . bounder <EOL> num_gens = args [ '<STR_LIT>' ] . num_generations <EOL> max_gens = args [ '<STR_LIT>' ] <EOL> strength = args . setdefault ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> exponent = ( <NUM_LIT:1.0> - num_gens / float ( max_gens ) ) ** strength <EOL> mutant = copy . copy ( candidate ) <EOL> for i , ( c , lo , hi ) in enumerate ( zip ( candidate , bounder . lower_bound , bounder . upper_bound ) ) : <EOL> if random . random ( ) <= <NUM_LIT:0.5> : <EOL> new_value = c + ( hi - c ) * ( <NUM_LIT:1.0> - random . random ( ) ** exponent ) <EOL> else : <EOL> new_value = c - ( c - lo ) * ( <NUM_LIT:1.0> - random . random ( ) ** exponent ) <EOL> mutant [ i ] = new_value <EOL> return mutant </s>
