<s> """<STR_LIT>""" <EOL> import collections <EOL> import copy <EOL> import functools <EOL> from inspyred . ec import archivers <EOL> from inspyred . ec import generators <EOL> from inspyred . ec import migrators <EOL> from inspyred . ec import observers <EOL> from inspyred . ec import replacers <EOL> from inspyred . ec import selectors <EOL> from inspyred . ec import terminators <EOL> from inspyred . ec import variators <EOL> import itertools <EOL> import logging <EOL> import math <EOL> import time <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class EvolutionExit ( Error ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Bounder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lower_bound = None , upper_bound = None ) : <EOL> self . lower_bound = lower_bound <EOL> self . upper_bound = upper_bound <EOL> if self . lower_bound is not None and self . upper_bound is not None : <EOL> if not isinstance ( self . lower_bound , collections . Iterable ) : <EOL> self . lower_bound = itertools . repeat ( self . lower_bound ) <EOL> if not isinstance ( self . upper_bound , collections . Iterable ) : <EOL> self . upper_bound = itertools . repeat ( self . upper_bound ) <EOL> def __call__ ( self , candidate , args ) : <EOL> if self . lower_bound is None or self . upper_bound is None : <EOL> return candidate <EOL> else : <EOL> if not isinstance ( self . lower_bound , collections . Iterable ) : <EOL> self . lower_bound = [ self . lower_bound ] * len ( candidate ) <EOL> if not isinstance ( self . upper_bound , collections . Iterable ) : <EOL> self . upper_bound = [ self . upper_bound ] * len ( candidate ) <EOL> bounded_candidate = candidate <EOL> for i , ( c , lo , hi ) in enumerate ( zip ( candidate , self . lower_bound , <EOL> self . upper_bound ) ) : <EOL> bounded_candidate [ i ] = max ( min ( c , hi ) , lo ) <EOL> return bounded_candidate <EOL> class DiscreteBounder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , values ) : <EOL> self . values = values <EOL> self . lower_bound = itertools . repeat ( min ( self . values ) ) <EOL> self . upper_bound = itertools . repeat ( max ( self . values ) ) <EOL> def __call__ ( self , candidate , args ) : <EOL> if not isinstance ( self . lower_bound , collections . Iterable ) : <EOL> self . lower_bound = [ min ( self . values ) ] * len ( candidate ) <EOL> if not isinstance ( self . upper_bound , collections . Iterable ) : <EOL> self . upper_bound = [ max ( self . values ) ] * len ( candidate ) <EOL> closest = lambda target : min ( self . values , key = lambda x : abs ( x - target ) ) <EOL> bounded_candidate = candidate <EOL> for i , c in enumerate ( bounded_candidate ) : <EOL> bounded_candidate [ i ] = closest ( c ) <EOL> return bounded_candidate <EOL> class Individual ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , candidate = None , maximize = True ) : <EOL> self . _candidate = candidate <EOL> self . fitness = None <EOL> self . birthdate = time . time ( ) <EOL> self . maximize = maximize <EOL> @ property <EOL> def candidate ( self ) : <EOL> return self . _candidate <EOL> @ candidate . setter <EOL> def candidate ( self , value ) : <EOL> self . _candidate = value <EOL> self . fitness = None <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( str ( self . candidate ) , str ( self . fitness ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( str ( self . candidate ) , str ( self . fitness ) , self . birthdate ) <EOL> def __lt__ ( self , other ) : <EOL> if self . fitness is not None and other . fitness is not None : <EOL> if self . maximize : <EOL> return self . fitness < other . fitness <EOL> else : <EOL> return self . fitness > other . fitness <EOL> else : <EOL> raise Error ( '<STR_LIT>' ) <EOL> def __le__ ( self , other ) : <EOL> return self < other or not other < self <EOL> def __gt__ ( self , other ) : <EOL> if self . fitness is not None and other . fitness is not None : <EOL> return other < self <EOL> else : <EOL> raise Error ( '<STR_LIT>' ) <EOL> def __ge__ ( self , other ) : <EOL> return other < self or not self < other <EOL> def __eq__ ( self , other ) : <EOL> return ( ( self . _candidate , self . fitness , self . maximize ) == <EOL> ( other . _candidate , other . fitness , other . maximize ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> class EvolutionaryComputation ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , random ) : <EOL> self . selector = selectors . default_selection <EOL> self . variator = variators . default_variation <EOL> self . replacer = replacers . default_replacement <EOL> self . migrator = migrators . default_migration <EOL> self . observer = observers . default_observer <EOL> self . archiver = archivers . default_archiver <EOL> self . terminator = terminators . default_termination <EOL> self . termination_cause = None <EOL> self . generator = None <EOL> self . evaluator = None <EOL> self . bounder = None <EOL> self . maximize = True <EOL> self . archive = None <EOL> self . population = None <EOL> self . num_evaluations = <NUM_LIT:0> <EOL> self . num_generations = <NUM_LIT:0> <EOL> self . logger = logging . getLogger ( '<STR_LIT>' ) <EOL> try : <EOL> self . logger . addHandler ( logging . NullHandler ( ) ) <EOL> except AttributeError : <EOL> pass <EOL> self . _random = random <EOL> self . _kwargs = dict ( ) <EOL> def _should_terminate ( self , pop , ng , ne ) : <EOL> terminate = False <EOL> fname = '<STR_LIT>' <EOL> if isinstance ( self . terminator , collections . Iterable ) : <EOL> for clause in self . terminator : <EOL> self . logger . debug ( '<STR_LIT>' . format ( clause . __name__ , ng , ne ) ) <EOL> terminate = terminate or clause ( population = pop , num_generations = ng , num_evaluations = ne , args = self . _kwargs ) <EOL> if terminate : <EOL> fname = clause . __name__ <EOL> break <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . terminator . __name__ , ng , ne ) ) <EOL> terminate = self . terminator ( population = pop , num_generations = ng , num_evaluations = ne , args = self . _kwargs ) <EOL> fname = self . terminator . __name__ <EOL> if terminate : <EOL> self . termination_cause = fname <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . termination_cause , ng , ne ) ) <EOL> return terminate <EOL> def evolve ( self , generator , evaluator , pop_size = <NUM_LIT:100> , seeds = None , maximize = True , bounder = None , ** args ) : <EOL> """<STR_LIT>""" <EOL> self . _kwargs = args <EOL> self . _kwargs [ '<STR_LIT>' ] = self <EOL> if seeds is None : <EOL> seeds = [ ] <EOL> if bounder is None : <EOL> bounder = Bounder ( ) <EOL> self . termination_cause = None <EOL> self . generator = generator <EOL> self . evaluator = evaluator <EOL> self . bounder = bounder <EOL> self . maximize = maximize <EOL> self . population = [ ] <EOL> self . archive = [ ] <EOL> if not isinstance ( seeds , collections . Sequence ) : <EOL> seeds = [ seeds ] <EOL> initial_cs = copy . copy ( seeds ) <EOL> num_generated = max ( pop_size - len ( seeds ) , <NUM_LIT:0> ) <EOL> i = <NUM_LIT:0> <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> while i < num_generated : <EOL> cs = generator ( random = self . _random , args = self . _kwargs ) <EOL> initial_cs . append ( cs ) <EOL> i += <NUM_LIT:1> <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> initial_fit = evaluator ( candidates = initial_cs , args = self . _kwargs ) <EOL> for cs , fit in zip ( initial_cs , initial_fit ) : <EOL> if fit is not None : <EOL> ind = Individual ( cs , maximize = maximize ) <EOL> ind . fitness = fit <EOL> self . population . append ( ind ) <EOL> else : <EOL> self . logger . warning ( '<STR_LIT>' . format ( cs ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( self . population ) ) ) <EOL> self . num_evaluations = len ( initial_fit ) <EOL> self . num_generations = <NUM_LIT:0> <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> self . archive = self . archiver ( random = self . _random , population = list ( self . population ) , archive = list ( self . archive ) , args = self . _kwargs ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( self . archive ) ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( self . population ) ) ) <EOL> if isinstance ( self . observer , collections . Iterable ) : <EOL> for obs in self . observer : <EOL> self . logger . debug ( '<STR_LIT>' . format ( obs . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> obs ( population = list ( self . population ) , num_generations = self . num_generations , num_evaluations = self . num_evaluations , args = self . _kwargs ) <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . observer . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> self . observer ( population = list ( self . population ) , num_generations = self . num_generations , num_evaluations = self . num_evaluations , args = self . _kwargs ) <EOL> while not self . _should_terminate ( list ( self . population ) , self . num_generations , self . num_evaluations ) : <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . selector . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> parents = self . selector ( random = self . _random , population = list ( self . population ) , args = self . _kwargs ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( parents ) ) ) <EOL> parent_cs = [ copy . deepcopy ( i . candidate ) for i in parents ] <EOL> offspring_cs = parent_cs <EOL> if isinstance ( self . variator , collections . Iterable ) : <EOL> for op in self . variator : <EOL> self . logger . debug ( '<STR_LIT>' . format ( op . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> offspring_cs = op ( random = self . _random , candidates = offspring_cs , args = self . _kwargs ) <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . variator . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> offspring_cs = self . variator ( random = self . _random , candidates = offspring_cs , args = self . _kwargs ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( offspring_cs ) ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( evaluator . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> offspring_fit = evaluator ( candidates = offspring_cs , args = self . _kwargs ) <EOL> offspring = [ ] <EOL> for cs , fit in zip ( offspring_cs , offspring_fit ) : <EOL> if fit is not None : <EOL> off = Individual ( cs , maximize = maximize ) <EOL> off . fitness = fit <EOL> offspring . append ( off ) <EOL> else : <EOL> self . logger . warning ( '<STR_LIT>' . format ( cs ) ) <EOL> self . num_evaluations += len ( offspring_fit ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . replacer . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> self . population = self . replacer ( random = self . _random , population = self . population , parents = parents , offspring = offspring , args = self . _kwargs ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( self . population ) ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . migrator . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> self . population = self . migrator ( random = self . _random , population = self . population , args = self . _kwargs ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( self . population ) ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . archiver . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> self . archive = self . archiver ( random = self . _random , archive = self . archive , population = list ( self . population ) , args = self . _kwargs ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( self . archive ) ) ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( len ( self . population ) ) ) <EOL> self . num_generations += <NUM_LIT:1> <EOL> if isinstance ( self . observer , collections . Iterable ) : <EOL> for obs in self . observer : <EOL> self . logger . debug ( '<STR_LIT>' . format ( obs . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> obs ( population = list ( self . population ) , num_generations = self . num_generations , num_evaluations = self . num_evaluations , args = self . _kwargs ) <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' . format ( self . observer . __name__ , self . num_generations , self . num_evaluations ) ) <EOL> self . observer ( population = list ( self . population ) , num_generations = self . num_generations , num_evaluations = self . num_evaluations , args = self . _kwargs ) <EOL> return self . population <EOL> class GA ( EvolutionaryComputation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , random ) : <EOL> EvolutionaryComputation . __init__ ( self , random ) <EOL> self . selector = selectors . rank_selection <EOL> self . variator = [ variators . n_point_crossover , variators . bit_flip_mutation ] <EOL> self . replacer = replacers . generational_replacement <EOL> def evolve ( self , generator , evaluator , pop_size = <NUM_LIT:100> , seeds = None , maximize = True , bounder = None , ** args ) : <EOL> args . setdefault ( '<STR_LIT>' , pop_size ) <EOL> return EvolutionaryComputation . evolve ( self , generator , evaluator , pop_size , seeds , maximize , bounder , ** args ) <EOL> class ES ( EvolutionaryComputation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , random ) : <EOL> EvolutionaryComputation . __init__ ( self , random ) <EOL> self . selector = selectors . default_selection <EOL> self . variator = self . _internal_variation <EOL> self . replacer = replacers . plus_replacement <EOL> def _internal_variation ( self , random , candidates , args ) : <EOL> tau = args . setdefault ( '<STR_LIT>' , None ) <EOL> tau_prime = args . setdefault ( '<STR_LIT>' , None ) <EOL> epsilon = args . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> mutants = [ ] <EOL> n = len ( candidates [ <NUM_LIT:0> ] ) // <NUM_LIT:2> <EOL> if tau is None : <EOL> tau = <NUM_LIT:1> / math . sqrt ( <NUM_LIT:2> * math . sqrt ( n ) ) <EOL> if tau_prime is None : <EOL> tau_prime = <NUM_LIT:1> / math . sqrt ( <NUM_LIT:2> * n ) <EOL> for candidate in candidates : <EOL> cand = candidate [ : n ] <EOL> strat = candidate [ n : ] <EOL> for i , s in enumerate ( strat ) : <EOL> strat [ i ] = s * math . exp ( tau_prime * random . gauss ( <NUM_LIT:0> , <NUM_LIT:1> ) + tau * random . gauss ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> strat [ i ] = max ( strat [ i ] , epsilon ) <EOL> for i , ( c , s ) in enumerate ( zip ( cand , strat ) ) : <EOL> cand [ i ] = c + random . gauss ( <NUM_LIT:0> , s ) <EOL> cand = self . bounder ( cand , args ) <EOL> cand . extend ( strat ) <EOL> mutants . append ( cand ) <EOL> return mutants <EOL> def _internal_evaluator ( self , func ) : <EOL> @ functools . wraps ( func ) <EOL> def evaluator ( candidates , args ) : <EOL> cands = [ ] <EOL> for candidate in candidates : <EOL> n = len ( candidate ) // <NUM_LIT:2> <EOL> cands . append ( candidate [ : n ] ) <EOL> return func ( cands , args ) <EOL> return evaluator <EOL> def evolve ( self , generator , evaluator , pop_size = <NUM_LIT:100> , seeds = None , maximize = True , bounder = None , ** args ) : <EOL> generator = generators . strategize ( generator ) <EOL> evaluator = self . _internal_evaluator ( evaluator ) <EOL> strategy_seeds = None <EOL> if seeds is not None : <EOL> strategy_seeds = [ ] <EOL> for candidate in seeds : <EOL> n = len ( candidate ) <EOL> c = copy . copy ( candidate ) <EOL> c . extend ( [ self . _random . random ( ) for _ in range ( n ) ] ) <EOL> strategy_seeds . append ( c ) <EOL> return EvolutionaryComputation . evolve ( self , generator , evaluator , pop_size , strategy_seeds , maximize , bounder , ** args ) <EOL> class EDA ( EvolutionaryComputation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , random ) : <EOL> EvolutionaryComputation . __init__ ( self , random ) <EOL> self . selector = selectors . truncation_selection <EOL> self . variator = self . _internal_variation <EOL> self . replacer = replacers . generational_replacement <EOL> def _internal_variation ( self , random , candidates , args ) : <EOL> num_offspring = args . setdefault ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> bounder = args [ '<STR_LIT>' ] . bounder <EOL> num_genes = max ( [ len ( x ) for x in candidates ] ) <EOL> genes = [ [ x [ i ] for x in candidates ] for i in range ( num_genes ) ] <EOL> mean = [ float ( sum ( x ) ) / float ( len ( x ) ) for x in genes ] <EOL> stdev = [ sum ( [ ( x - m ) ** <NUM_LIT:2> for x in g ] ) / float ( len ( g ) - <NUM_LIT:1> ) for g , m in zip ( genes , mean ) ] <EOL> offspring = [ ] <EOL> for _ in range ( num_offspring ) : <EOL> child = copy . copy ( candidates [ <NUM_LIT:0> ] ) <EOL> for i , ( m , s ) in enumerate ( zip ( mean , stdev ) ) : <EOL> child [ i ] = m + random . gauss ( <NUM_LIT:0> , s ) <EOL> child = bounder ( child , args ) <EOL> offspring . append ( child ) <EOL> return offspring <EOL> def evolve ( self , generator , evaluator , pop_size = <NUM_LIT:100> , seeds = None , maximize = True , bounder = None , ** args ) : <EOL> args . setdefault ( '<STR_LIT>' , pop_size // <NUM_LIT:2> ) <EOL> args . setdefault ( '<STR_LIT>' , pop_size ) <EOL> return EvolutionaryComputation . evolve ( self , generator , evaluator , pop_size , seeds , maximize , bounder , ** args ) <EOL> class DEA ( EvolutionaryComputation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , random ) : <EOL> EvolutionaryComputation . __init__ ( self , random ) <EOL> self . selector = selectors . tournament_selection <EOL> self . variator = [ variators . heuristic_crossover , variators . gaussian_mutation ] <EOL> self . replacer = replacers . steady_state_replacement <EOL> def evolve ( self , generator , evaluator , pop_size = <NUM_LIT:100> , seeds = None , maximize = True , bounder = None , ** args ) : <EOL> args . setdefault ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> return EvolutionaryComputation . evolve ( self , generator , evaluator , pop_size , seeds , maximize , bounder , ** args ) <EOL> class SA ( EvolutionaryComputation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , random ) : <EOL> EvolutionaryComputation . __init__ ( self , random ) <EOL> self . selector = selectors . default_selection <EOL> self . variator = variators . gaussian_mutation <EOL> self . replacer = replacers . simulated_annealing_replacement <EOL> def evolve ( self , generator , evaluator , pop_size = <NUM_LIT:1> , seeds = None , maximize = True , bounder = None , ** args ) : <EOL> pop_size = <NUM_LIT:1> <EOL> return EvolutionaryComputation . evolve ( self , generator , evaluator , pop_size , seeds , maximize , bounder , ** args ) </s>
