forked from lubien/ufpa-ai-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_elitist_genetic_algorithm.py
144 lines (115 loc) · 3.26 KB
/
hello_elitist_genetic_algorithm.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Baseado:
Copyright (c) 2011 Colin Drake
"""
import random
from operator import itemgetter
file = open('data', 'w')
#
# Variáveis Globais
#
OPTIMAL = "Hello World"
DNA_SIZE = len(OPTIMAL)
POP_SIZE = 200
GENERATIONS = 1000
mutation_chance = 100 # 1/mutation_chance
crossover_chance= 6 # 1/...
# Salva os dois melhores
def save_elite (items):
items_copy = items
items_copy.sort(key=itemgetter(1), reverse=True)
e_ind1 = items_copy[0]
e_ind2 = items_copy[1]
return e_ind1, e_ind2
def weighted_choice(items):
# items = Pop = [(ind1, peso1), (ind2, peso2)..]
# método da roleta
# vai descontando os pesos até encontrar o menor
weight_total = sum((item[1] for item in items))
n = random.uniform(0, weight_total)
for item, weight in items:
if n < weight: return item
n = n - weight
return item
def random_char():
# Return a ASCII: 32..126
return chr(int(random.randrange(32, 126, 1)))
def random_population():
pop = []
for i in range(POP_SIZE):
dna = ""
for c in range(DNA_SIZE):
dna += random_char()
pop.append(dna)
return pop
#
# GA functions
#
def fitness(dna):
#calcula a distancia da letra atual até a otima
fitness = 0
for c in range(DNA_SIZE):
fitness += abs(ord(dna[c]) - ord(OPTIMAL[c]))
if fitness == 0: return 1.0
else: return 1.0/(fitness+1)
def mutate(dna):
# tenta mudar cada um dos char, probalidade 1/mutation_chance
dna_out = ""
for c in range(DNA_SIZE):
if int(random.randrange(1,mutation_chance,1)) == 1:
dna_out += random_char()
else:
dna_out += dna[c]
return dna_out
def crossover(dna1, dna2):
if int(random.randrange(1,crossover_chance,1)) == 1:
pos = int(random.random()*DNA_SIZE)
return (dna1[:pos]+dna2[pos:], dna2[:pos]+dna1[pos:])
else: return (dna1, dna2)
def main():
population = random_population()
# roda as gerações
for generation in range(GENERATIONS):
if generation % 100 == 0:
print ("Generation %s... Random sample: '%s'" % (generation, population[0]))
w_population = []
for individual in population:
fitness_val = fitness(individual)
pair = (individual, fitness_val)
w_population.append(pair)
# salva dois melhores main()
e_ind1, e_ind2 = save_elite(w_population)
population = []
for _ in range(int(POP_SIZE/2)-1):
# Selection
ind1 = weighted_choice(w_population)
ind2 = weighted_choice(w_population)
# Crossover
ind1, ind2 = crossover(ind1, ind2)
# Mutate and add back into the population.
population.append(mutate(ind1))
population.append(mutate(ind2))
# Adiciona o dois melhores no final da lista sem os pesos
population.append(e_ind1[0])
population.append(e_ind2[0])
# pega a população e ordena para testar o ótimo
wp=w_population
wp.sort(key=itemgetter(1), reverse=True)
wp0=wp[0]
file.write(str(wp0[1]) + "\n")
# se encontra a meta sai
if wp0[1]== 1:
print ("Encontrou o Ótimo:" , wp0[0],wp0[1])
exit(0)
wp=w_population
wp.sort(key=itemgetter(1), reverse=True)
wp0=wp[0]
print ("O melhor ainda não ótimo:", wp0[0],wp0[1])
file.close()
exit(0)
#
# Gera a população e evolui ela
#
if __name__ == "__main__":
main()
exit(0)