-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheeshits_example.py
58 lines (43 loc) · 2.64 KB
/
eeshits_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import random
from deap import creator, base, tools, algorithms
import numpy as np
args = {'sequence_length' : 5 , 'nucleotide_frequency' :[0.25,0.25,0.25,0.25] }
randomizer=np.random
def random_sequence_generator(randomizer,args) :
return randomizer.choice(list('ACGT') , p=args['nucleotide_frequency'] )
def fitness(individual):
return (individual.count('A')),
def mutation(individual, indpb):
for i in xrange(len(individual)):
if random.random() < indpb:
if individual[i]=='A' :
individual[i] = (randomizer.choice(list('CGT') , p=[args['nucleotide_frequency'][1]/(1-args['nucleotide_frequency'][0]) ,args['nucleotide_frequency'][2]/(1-args['nucleotide_frequency'][0]) ,args['nucleotide_frequency'][3]/(1-args['nucleotide_frequency'][0]) ] ) )
elif individual[i]=='C' :
individual[i] = (randomizer.choice(list('AGT') , p=[args['nucleotide_frequency'][0]/(1-args['nucleotide_frequency'][1]) ,args['nucleotide_frequency'][2]/(1-args['nucleotide_frequency'][1]) ,args['nucleotide_frequency'][3]/(1-args['nucleotide_frequency'][1]) ] ) )
elif individual[i]=='G' :
individual[i] = (randomizer.choice(list('CGT') , p=[args['nucleotide_frequency'][2]/(1-args['nucleotide_frequency'][2]) ,args['nucleotide_frequency'][1]/(1-args['nucleotide_frequency'][2]) ,args['nucleotide_frequency'][3]/(1-args['nucleotide_frequency'][2]) ] ) )
elif individual[i]=='T' :
individual[i] = (randomizer.choice(list('CGT') , p=[args['nucleotide_frequency'][0]/(1-args['nucleotide_frequency'][3]) ,args['nucleotide_frequency'][1]/(1-args['nucleotide_frequency'][3]) ,args['nucleotide_frequency'][2]/(1-args['nucleotide_frequency'][3]) ] ) )
print(individual)
print(individual.fitness)
return individual,
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list , fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("base", random_sequence_generator , randomizer , args)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.base, n=args['sequence_length'])
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", fitness)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", mutation, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
population = toolbox.population(n=300)
NGEN=40
for qgen in range(NGEN):
offspring = algorithms.varAnd(population, toolbox, cxpb=0.1, mutpb=0.1)
fits = toolbox.map(toolbox.evaluate, offspring)
for fit, ind in zip(fits, offspring):
ind.fitness.values = fit
population = toolbox.select(offspring, k=len(population))
top10 = tools.selBest(population, k=10)
print top10