<s> """<STR_LIT>""" <EOL> import sys <EOL> import array <EOL> import random <EOL> import logging <EOL> import multiprocessing <EOL> from deap import base , creator , tools <EOL> from deap . algorithms import varAnd <EOL> from jcvi . algorithms . lis import longest_monotonic_subseq_length <EOL> def make_data ( POINTS , SCF ) : <EOL> seq = range ( POINTS ) <EOL> scaffolds = [ ] <EOL> batch = POINTS / SCF <EOL> for i in xrange ( SCF ) : <EOL> p = seq [ i * batch : ( i + <NUM_LIT:1> ) * batch ] <EOL> scaffolds . append ( p ) <EOL> return scaffolds <EOL> def colinear_evaluate ( tour , scaffolds ) : <EOL> series = [ ] <EOL> for t in tour : <EOL> series . extend ( scaffolds [ t ] ) <EOL> score , diff = longest_monotonic_subseq_length ( series ) <EOL> return score , <EOL> def genome_mutation ( candidate ) : <EOL> """<STR_LIT>""" <EOL> size = len ( candidate ) <EOL> prob = random . random ( ) <EOL> if prob > <NUM_LIT> : <EOL> p = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> q = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> if p > q : <EOL> p , q = q , p <EOL> q += <NUM_LIT:1> <EOL> s = candidate [ p : q ] <EOL> x = candidate [ : p ] + s [ : : - <NUM_LIT:1> ] + candidate [ q : ] <EOL> return creator . Individual ( x ) , <EOL> else : <EOL> p = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> q = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> cq = candidate . pop ( q ) <EOL> candidate . insert ( p , cq ) <EOL> return candidate , <EOL> def genome_mutation_orientation ( candidate ) : <EOL> size = len ( candidate ) <EOL> prob = random . random ( ) <EOL> if prob > <NUM_LIT> : <EOL> p = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> q = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> if p > q : <EOL> p , q = q , p <EOL> q += <NUM_LIT:1> <EOL> for x in xrange ( p , q ) : <EOL> candidate [ x ] = - candidate [ x ] <EOL> else : <EOL> p = random . randint ( <NUM_LIT:0> , size - <NUM_LIT:1> ) <EOL> candidate [ p ] = - candidate [ p ] <EOL> return candidate , <EOL> def GA_setup ( guess , weights = ( <NUM_LIT:1.0> , ) ) : <EOL> creator . create ( "<STR_LIT>" , base . Fitness , weights = weights ) <EOL> creator . create ( "<STR_LIT>" , array . array , typecode = '<STR_LIT:i>' , fitness = creator . FitnessMax ) <EOL> toolbox = base . Toolbox ( ) <EOL> toolbox . register ( "<STR_LIT>" , creator . Individual , guess ) <EOL> toolbox . register ( "<STR_LIT>" , tools . initRepeat , list , toolbox . individual ) <EOL> toolbox . register ( "<STR_LIT>" , tools . cxPartialyMatched ) <EOL> toolbox . register ( "<STR_LIT>" , genome_mutation ) <EOL> toolbox . register ( "<STR_LIT>" , tools . selTournament , tournsize = <NUM_LIT:3> ) <EOL> return toolbox <EOL> def eaSimpleConverge ( population , toolbox , cxpb , mutpb , ngen , stats = None , <EOL> halloffame = None , callback = None , verbose = True ) : <EOL> """<STR_LIT>""" <EOL> invalid_ind = [ ind for ind in population if not ind . fitness . valid ] <EOL> fitnesses = toolbox . map ( toolbox . evaluate , invalid_ind ) <EOL> for ind , fit in zip ( invalid_ind , fitnesses ) : <EOL> ind . fitness . values = fit <EOL> if halloffame is not None : <EOL> halloffame . update ( population ) <EOL> record = stats . compile ( population ) if stats else { } <EOL> gen = <NUM_LIT:1> <EOL> best = <NUM_LIT:0> <EOL> while True : <EOL> offspring = toolbox . select ( population , len ( population ) ) <EOL> offspring = varAnd ( offspring , toolbox , cxpb , mutpb ) <EOL> invalid_ind = [ ind for ind in offspring if not ind . fitness . valid ] <EOL> fitnesses = toolbox . map ( toolbox . evaluate , invalid_ind ) <EOL> for ind , fit in zip ( invalid_ind , fitnesses ) : <EOL> ind . fitness . values = fit <EOL> if halloffame is not None : <EOL> halloffame . update ( offspring ) <EOL> if callback is not None : <EOL> callback ( halloffame [ <NUM_LIT:0> ] , gen ) <EOL> population [ : ] = offspring <EOL> record = stats . compile ( population ) if stats else { } <EOL> current_best = record [ '<STR_LIT>' ] <EOL> if gen % <NUM_LIT:20> == <NUM_LIT:0> and verbose : <EOL> print >> sys . stderr , "<STR_LIT>" . format ( gen , current_best ) <EOL> if current_best > best : <EOL> best = current_best <EOL> updated = gen <EOL> gen += <NUM_LIT:1> <EOL> if gen - updated > ngen : <EOL> break <EOL> return population <EOL> def GA_run ( toolbox , ngen = <NUM_LIT> , npop = <NUM_LIT:100> , seed = <NUM_LIT> , cpus = <NUM_LIT:1> , callback = None ) : <EOL> logging . debug ( "<STR_LIT>" . format ( ngen , npop , cpus , seed ) ) <EOL> if cpus > <NUM_LIT:1> : <EOL> pool = multiprocessing . Pool ( cpus ) <EOL> toolbox . register ( "<STR_LIT>" , pool . map ) <EOL> random . seed ( seed ) <EOL> pop = toolbox . population ( n = npop ) <EOL> hof = tools . HallOfFame ( <NUM_LIT:1> ) <EOL> stats = tools . Statistics ( lambda ind : ind . fitness . values ) <EOL> stats . register ( "<STR_LIT>" , max ) <EOL> stats . register ( "<STR_LIT>" , min ) <EOL> eaSimpleConverge ( pop , toolbox , <NUM_LIT> , <NUM_LIT> , ngen , stats = stats , <EOL> halloffame = hof , callback = callback ) <EOL> tour = hof [ <NUM_LIT:0> ] <EOL> if cpus > <NUM_LIT:1> : <EOL> pool . terminate ( ) <EOL> return tour , tour . fitness <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> POINTS , SCF = <NUM_LIT:200> , <NUM_LIT:20> <EOL> scaffolds = make_data ( POINTS , SCF ) <EOL> guess = range ( SCF ) <EOL> guess [ <NUM_LIT:5> : <NUM_LIT:15> ] = guess [ <NUM_LIT:5> : <NUM_LIT:15> ] [ : : - <NUM_LIT:1> ] <EOL> guess [ <NUM_LIT:7> : <NUM_LIT> ] = guess [ <NUM_LIT:7> : <NUM_LIT> ] [ : : - <NUM_LIT:1> ] <EOL> print guess <EOL> toolbox = GA_setup ( guess ) <EOL> toolbox . register ( "<STR_LIT>" , colinear_evaluate , scaffolds = scaffolds ) <EOL> tour , tour . fitness = GA_run ( toolbox , cpus = <NUM_LIT:8> ) <EOL> print tour , tour . fitness </s>
