def swap_mutation(contig_ord, memo, subset_memo, mut_rate=MUTATION): # for swap mutation protocol see http://www.theprojectspot.com/tutorial-post/applying-a-genetic-algorithm-to-the-travelling-salesman-problem/5 a = sampler(mut_rate, 1)[0] new = [i for i in contig_ord.chrom_list] pos = range(len(new)) random.shuffle(pos) for i in range(a): sign = random.choice([-1, 1]) p1 = pos.pop(0) p2 = pos.pop(0) tmp = new[p1] new[p1] = new[p2] * sign new[p2] = tmp * sign c = ContigOrder(new, chrom_dict, scaff_lookup, memo, subset_memo) return c
def swap_mutation(contig_ord, mut_rate = MUTATION): #for swap mutation protocol see http://www.theprojectspot.com/tutorial-post/applying-a-genetic-algorithm-to-the-travelling-salesman-problem/5 a = sampler(mut_rate, 1)[0] new = [i for i in contig_ord.chrom_list] pos = range(len(new)) random.shuffle(pos) for i in range(a): sign = random.choice([-1, 1]) p1 = pos.pop(0) p2 = pos.pop(0) tmp = new[p1] new[p1] = new[p2]*sign new[p2] = tmp*sign c = ContigOrder(new, chrom_dict, scaff_lookup) return c
def recombination(contig_ord1, contig_ord2): #use order crossover (see: http://www.theprojectspot.com/tutorial-post/applying-a-genetic-algorithm-to-the-travelling-salesman-problem/5) parents = [[i for i in contig_ord1.chrom_list], [i for i in contig_ord2.chrom_list]] random.shuffle(parents) contig_ord1, contig_ord2 = parents #just scramble which parent is donor, so p2 isn't always new = ['-' for i in contig_ord1] pos = range(len(new)) a,b = sorted(sampler(pos, 2)) sval = set([abs(i) for i in contig_ord2[a:b+1]]) sidx = set(range(a,b+1)) for i in range(a, b+1): new[i] = contig_ord2[i] picks = [i for i in contig_ord1 if abs(i) not in sval] for i in [j for j in range(len(contig_ord1)) if j not in sidx]: p = picks.pop(0) new[i] = p c = ContigOrder(new, chrom_dict, scaff_lookup) return c
def recombination(contig_ord1, contig_ord2, memo, subset_memo): #use order crossover (see: http://www.theprojectspot.com/tutorial-post/applying-a-genetic-algorithm-to-the-travelling-salesman-problem/5) parents = [[i for i in contig_ord1.chrom_list], [i for i in contig_ord2.chrom_list]] random.shuffle(parents) contig_ord1, contig_ord2 = parents #just scramble which parent is donor, so p2 isn't always new = ['-' for i in contig_ord1] pos = range(len(new)) a,b = sorted(sampler(pos, 2)) sval = set([abs(i) for i in contig_ord2[a:b+1]]) sidx = set(range(a,b+1)) for i in range(a, b+1): new[i] = contig_ord2[i] picks = [i for i in contig_ord1 if abs(i) not in sval] for i in [j for j in range(len(contig_ord1)) if j not in sidx]: p = picks.pop(0) new[i] = p c = ContigOrder(new, chrom_dict, scaff_lookup, memo, subset_memo) return c