-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpso.py
66 lines (53 loc) · 1.87 KB
/
pso.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
59
60
61
62
63
64
65
66
import common
import random
import math
import time
all_created_motifs = []
enough_time = 60
def mutate(motif):
return common.generate_adjecent_motif(motif)
def reproduce(mom, dad):
random_index = random.randint(0, common.input_motif_length - 1)
child = mom[:random_index] + dad[random_index - common.input_motif_length:]
return child
def generate_swarm(size):
population = []
for i in range(0, size):
population.append(common.generate_random_potential_motif())
return population
def get_random_from_population(population):
random_index = random.randint(0, len(population) - 1)
return population[random_index]
def implement_genetic_algorithm():
done = False
answer = None
population = generate_swarm(10)
while not done:
new_population = []
for i in range(0, len(population)):
random_motif_dad = get_random_from_population(population)
random_motif_mom = get_random_from_population(population)
motif_child = reproduce(random_motif_mom, random_motif_dad)
print(motif_child + ' is child of ' + random_motif_mom +
' and ' + random_motif_dad + '!', end='\r')
random_probability = random.randint(0, 100) / 100
if random_probability < 0.3:
motif_child = mutate(motif_child)
new_population.append(motif_child)
population = new_population
for motif in population:
if common.is_motif_valid(motif) == True:
done = True
answer = motif
break
if answer is not None:
return answer
else:
return False
st = common.start_time()
answer = implement_genetic_algorithm()
if answer != False:
common.print_success('\n' + answer + ' is a valid motif.')
else:
common.print_error('No motifs were found.')
common.end_time(st)