-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.py
110 lines (63 loc) · 2.26 KB
/
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
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
from fuzzywuzzy import fuzz
import random
import string
class Agent:
def __init__(self, length):
self.string = ''.join(random.choice(string.letters) for _ in xrange(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 xrange(generations):
print 'Generation: ' + str(generation)
agents = fitness(agents)
agents = selection(agents)
agents = crossover(agents)
agents = mutation(agents)
for agent in agents:
if agent.string == in_str:
print agent.string
break
if any(agent.fitness == 100 for agent in agents):
print 'Threshold met!'
break
def init_agents(population, length):
return [Agent(length) for _ in xrange(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 xrange((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.letters) + agent.string[idx+1:in_str_len]
return agents
if __name__ == '__main__':
in_str = 'pawan'
in_str_len = len(in_str)
ga()