-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_ga.py
74 lines (59 loc) · 2.19 KB
/
simple_ga.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
from fuzzywuzzy import fuzz
import random
import string
class Agent:
def __init__(self,length):
self.string = ''.join(random.choice(string.ascii_letters) for _ in range(length))
self.fitness = -1
def __str__(self):
return 'String: ' + str(self.string) + ' Fitness: ' + str(self.fitness)
in_str = None
in_str_len = None
population = 20
generations = 10000
def ga():
agents = init_agents(population,in_str_len)
for generation in range(generations):
print('Generation: ' + str(generation))
agents = fitness(agents)
agents = selection(agents)
agents = crossover(agents)
agents = mutation(agents)
if any(agent.fitness >= 99 for agent in agents):
print('Threshold met')
exit(0)
def init_agents(population , in_str_len):
return [Agent(in_str_len) for _ in range(population)]
def fitness(agents):
for agent in agents:
agent.fitness = fuzz.ratio(agent.string,in_str)
return agents
def selection(agents):
agents = sorted(agents, key=lambda agent: agent.fitness, reverse=True)
print('\n'.join(map(str,agents)))
agents = agents[:int(0.2 * len(agents))]
return agents
def crossover(agents):
offspring = []
for _ in range(int((population-len(agents))/2)):
parent1 = random.choice(agents)
parent2 = random.choice(agents)
child1 = Agent(in_str_len)
child2 = Agent(in_str_len)
split = random.randint(0,in_str_len)
child1.string = parent1.string[0:split] + parent2.string[split:in_str_len]
child2.string = parent2.string[0:split] + parent1.string[split:in_str_len]
offspring.append(child1)
offspring.append(child2)
agents.extend(offspring)
return agents
def mutation(agents):
for agent in agents:
for idx, param in enumerate(agent.string):
if(random.uniform(0.0,1.0)<=0.1):
agent.string = agent.string[0:idx] + random.choice(string.ascii_letters) + agent.string[(idx+1):in_str_len]
return agents
if __name__ == '__main__':
in_str = 'UdhayAakash'
in_str_len = len(in_str)
ga()