├── .gitignore ├── README.md ├── LICENSE └── simple-ga.py /.gitignore: -------------------------------------------------------------------------------- 1 | # OS Related Files # 2 | *.DS_Store 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simple-ga 2 | Sample code for an incredibly simple genetic algorithm. 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Troy Squillaci 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /simple-ga.py: -------------------------------------------------------------------------------- 1 | from fuzzywuzzy import fuzz 2 | import random 3 | import string 4 | 5 | 6 | class Agent: 7 | 8 | def __init__(self, length): 9 | 10 | self.string = ''.join(random.choice(string.letters) for _ in xrange(length)) 11 | self.fitness = -1 12 | 13 | def __str__(self): 14 | 15 | return 'String: ' + str(self.string) + ' Fitness: ' + str(self.fitness) 16 | 17 | in_str = None 18 | in_str_len = None 19 | population = 20 20 | generations = 10000 21 | 22 | 23 | def ga(): 24 | 25 | agents = init_agents(population, in_str_len) 26 | 27 | for generation in xrange(generations): 28 | 29 | print 'Generation: ' + str(generation) 30 | 31 | agents = fitness(agents) 32 | agents = selection(agents) 33 | agents = crossover(agents) 34 | agents = mutation(agents) 35 | 36 | if any(agent.fitness >= 90 for agent in agents): 37 | 38 | print 'Threshold met!' 39 | exit(0) 40 | 41 | 42 | def init_agents(population, length): 43 | 44 | return [Agent(length) for _ in xrange(population)] 45 | 46 | 47 | def fitness(agents): 48 | 49 | for agent in agents: 50 | 51 | agent.fitness = fuzz.ratio(agent.string, in_str) 52 | 53 | return agents 54 | 55 | 56 | def selection(agents): 57 | 58 | agents = sorted(agents, key=lambda agent: agent.fitness, reverse=True) 59 | print '\n'.join(map(str, agents)) 60 | agents = agents[:int(0.2 * len(agents))] 61 | 62 | return agents 63 | 64 | 65 | def crossover(agents): 66 | 67 | offspring = [] 68 | 69 | for _ in xrange((population - len(agents)) / 2): 70 | 71 | parent1 = random.choice(agents) 72 | parent2 = random.choice(agents) 73 | child1 = Agent(in_str_len) 74 | child2 = Agent(in_str_len) 75 | split = random.randint(0, in_str_len) 76 | child1.string = parent1.string[0:split] + parent2.string[split:in_str_len] 77 | child2.string = parent2.string[0:split] + parent1.string[split:in_str_len] 78 | 79 | offspring.append(child1) 80 | offspring.append(child2) 81 | 82 | agents.extend(offspring) 83 | 84 | return agents 85 | 86 | 87 | def mutation(agents): 88 | 89 | for agent in agents: 90 | 91 | for idx, param in enumerate(agent.string): 92 | 93 | if random.uniform(0.0, 1.0) <= 0.1: 94 | 95 | agent.string = agent.string[0:idx] + random.choice(string.letters) + agent.string[idx+1:in_str_len] 96 | 97 | return agents 98 | 99 | 100 | if __name__ == '__main__': 101 | 102 | in_str = 'TroySquillaci' 103 | in_str_len = len(in_str) 104 | ga() 105 | --------------------------------------------------------------------------------