Last active
July 21, 2018 12:11
-
-
Save rish-16/fed09bb00a0bb992cabfb561143e5a64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SAgent(): | |
| def __init__(self, in_str): | |
| self.in_str = in_str | |
| self.in_str_len = len(in_str) | |
| self.average_score_fitness = -1 | |
| self.number_successful = 0 | |
| self.population = random.randint(1, 21) | |
| self.generations = random.randint(1, 5000) | |
| self.threshold = 90 | |
| def __str__(self): | |
| return 'Population: ' + str(self.population) + ' | Generations: ' + str(self.generations) + ' Fitness: ' + str(self.average_score_fitness) | |
| def init_pagents(self, pop, length): | |
| # ... | |
| return [PAgent(length) for _ in range(pop)] | |
| def fitness(self, agents): | |
| # ... | |
| return agents | |
| def selection(self, agents): | |
| # ... | |
| return agents | |
| def crossover(self, agents): | |
| # ... | |
| return agents | |
| def mutation(self, agents): | |
| # ... | |
| return agents | |
| def ga(self): | |
| agents = self.init_pagents(self.population, self.in_str_len) | |
| for generation in range(self.generations): | |
| print ('\tPrimary Agent Generation: ' + str(generation)) | |
| agents = self.fitness(agents) | |
| agents = self.selection(agents) | |
| agents = self.crossover(agents) | |
| agents = self.mutation(agents) | |
| if any(agent.fitness >= self.threshold for agent in agents): | |
| self.number_successful += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment