for i in tmp_elite:
         ELITES.add(i.tag)
     new_population = P.map(functionalize_write_file_and_test_fitness_full_model, tmp_elite)
     for i in done_elite:
         new_population.append(i)
 else:
     new_population = population[:ELITE]
 ##########OK, NOW WE ARE DONE WITH ELITES, THEY ARE SAVED FOR NEXT GENERATION, NOW WE NEED TO FILL OUT REST OF POP#####
 #########WHICH WE DO BY RECOMBINING AND MUTATING LAST GENERATION, ALL BASED ON PAST PERFORMANCE (I.E. OUR RANK BASED WEIGHTS)
 auto_pop_size = NCPUs + ELITE
 # print [i.tag for i in population]
 # print memo
 # print NCPUs, len([1 for i in population if i.tag in memo])
 # print range(ELITE, auto_pop_size)
 for i in range(ELITE, auto_pop_size):
     c1 = weighted_sampler(weight_dict)  # SELECT A ORDER BASED ON WEIGHTS, THIS IS PARENT #1
     c2 = c1
     while c2 == c1:
         c2 = weighted_sampler(weight_dict)  # SELECT A SECOND ORDER, MAKING SURE IT'S DIFFERENT FROM THE FIRST
     # c1 and c2 are the 2 indivduals to be recombined
     cnew = swap_mutation(population[c1], memo, {})  # first mutate c1.  could do c2 too, but I didn't for now.
     cnew = recombination(cnew, population[c2], memo, {})  # NOW RECOMBINE
     new_population.append(cnew)  # add this onto the next generation
 ###########FINALLY A LITTLE BOOK KEEPING TO CLEAR OUT DUPLICATE CONTIG ORDERS, WHICH ARE A WASTE OF CYCLES#################
 seen = []
 tmp_pop = []
 for (
     i
 ) in (
     new_population
 ):  # occasionally identical orders get in, this removes duplicates and fills in with a new random order
            for i in tmp_elite: ELITES.add(i.tag)
            new_population = P.map(functionalize_write_file_and_test_fitness_full_model, tmp_elite)
            for i in done_elite: new_population.append(i)
            print 'recalc needed'
        else:
            new_population = population[:ELITE]
        print time.time() - start, '7 done with recalc as needed'
##########OK, NOW WE ARE DONE WITH ELITES, THEY ARE SAVED FOR NEXT GENERATION, NOW WE NEED TO FILL OUT REST OF POP#####
#########WHICH WE DO BY RECOMBINING AND MUTATING LAST GENERATION, ALL BASED ON PAST PERFORMANCE (I.E. OUR RANK BASED WEIGHTS)
        auto_pop_size = NCPUs+ELITE
        #print [i.tag for i in population]
        #print memo
        #print NCPUs, len([1 for i in population if i.tag in memo])
        #print range(ELITE, auto_pop_size)
        for i in range(ELITE, auto_pop_size):
            c1 = weighted_sampler(weight_dict)#SELECT A ORDER BASED ON WEIGHTS, THIS IS PARENT #1
            c2 = c1
            while c2 == c1:
                c2 = weighted_sampler(weight_dict) #SELECT A SECOND ORDER, MAKING SURE IT'S DIFFERENT FROM THE FIRST
            #c1 and c2 are the 2 indivduals to be recombined
            cnew = swap_mutation(population[c1], memo, {})# first mutate c1.  could do c2 too, but I didn't for now.  
            cnew = recombination(cnew, population[c2], memo, {})#NOW RECOMBINE
            new_population.append(cnew)#add this onto the next generation
        print time.time() - start, '8 recomb and fitness'
###########FINALLY A LITTLE BOOK KEEPING TO CLEAR OUT DUPLICATE CONTIG ORDERS, WHICH ARE A WASTE OF CYCLES#################
        seen = []
        tmp_pop = []
        for i in new_population: #occasionally identical orders get in, this removes duplicates and fills in with a new random order
            if i.tag not in seen:
                seen.append(i.tag)
                tmp_pop.append(i)
Example #3
0
for i in range(1, POP_SIZE): #shuffle all but the first one, leave that at whatever is in the file (prob. v2 genome order)
    population[i].shuffle()  #randomize
#print [i.tag for i in population]

start  = time.time()
for gen in xrange(NGEN):
    for i in range(POP_SIZE):
        print "generation="+str(gen+1)+';', 'individual='+str(i+1)+';', ' elapsed time (sec):', time.time()-start
        population[i].write_file_and_test_fitness()
    population.sort(key = lambda s: s.Fitness*-1)
    weights = list(reversed(range(1, len(population)+1)))
    weight_dict = {i:weights[i] for i in range(len(population))} #using ranked based selection (see :http://www.obitko.com/tutorials/genetic-algorithms/selection.php)
    new_population = population[:ELITE]#save the elites
    for i in range(ELITE, POP_SIZE):
        c1 = weighted_sampler(weight_dict)
        c2 = c1
        while c2 == c1:
            c2 = weighted_sampler(weight_dict)
        #c1 and c2 are the 2 indivduals to be recombined
        cnew = swap_mutation(population[c1])# first mutate c1.  could do c2 too, but I didn't for now.  
        cnew = recombination(cnew, population[c2])
        new_population.append(cnew)#add this onto the next generation
    seen = []
    tmp_pop = []
    for i in new_population: #occasionally identical orders get in, this removes duplicates and fills in with a new random order
        if i.tag not in seen:
            seen.append(i.tag)
            tmp_pop.append(i)
        else:
            cnew  = ContigOrder(chrom_list, chrom_dict, scaff_lookup)
            update_subset_memo(i.chrom_list, i.Rates)
            memo[i.tag] = [i.Fitness, i.Rates]
        weights = list(reversed(range(1, len(population)+1)))
        weight_dict = {i:weights[i] for i in range(len(population))} #using ranked based selection (see :http://www.obitko.com/tutorials/genetic-algorithms/selection.php)
 #       tmp_elite, done_elite = [], []
 #       for i in population[:ELITE]:
 #           if i.tag not in ELITES: tmp_elite.append(i)
 #           else: done_elite.append(i)
 #       if tmp_elite: 
 #           for i in tmp_elite: ELITES.add(i.tag)
 #           new_population = P.map(functionalize_write_file_and_test_fitness_full_model, tmp_elite)
 #           for i in done_elite: new_population.append(i)
 #       else:
        new_population = population[:ELITE]
        for i in range(ELITE, POP_SIZE):
            c1 = weighted_sampler(weight_dict)
            c2 = c1
            while c2 == c1:
                c2 = weighted_sampler(weight_dict)
            #c1 and c2 are the 2 indivduals to be recombined
            cnew = swap_mutation(population[c1], memo, subset_memo)# first mutate c1.  could do c2 too, but I didn't for now.  
            cnew = recombination(cnew, population[c2], memo, subset_memo)
            new_population.append(cnew)#add this onto the next generation
        seen = []
        tmp_pop = []
        for i in new_population: #occasionally identical orders get in, this removes duplicates and fills in with a new random order
            if i.tag not in seen:
                seen.append(i.tag)
                tmp_pop.append(i)
            else:
                cnew  = ContigOrder(chrom_list, chrom_dict, scaff_lookup, memo, subset_memo)