Skip to content

Commit

Permalink
Minor updates to make the python code a bit cleaner.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvazic committed Apr 1, 2013
1 parent 93add2f commit 12f5a74
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions python/gahelloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
@author: John Svazic
"""

from random import (random, randint)
from random import (choice, random, randint)

__all__ = ['Chromosome', 'Population']

class Chromosome(object):
class Chromosome:
"""
This class is used to define a chromosome for the gentic algorithm
simulation.
Expand All @@ -57,8 +57,8 @@ def mate(self, mate):
resulting in a new chromosome being returned.
"""
pivot = randint(0, len(self.gene) - 1)
gene1 = self.gene[0:pivot] + mate.gene[pivot:]
gene2 = mate.gene[0:pivot] + self.gene[pivot:]
gene1 = self.gene[:pivot] + mate.gene[pivot:]
gene2 = mate.gene[:pivot] + self.gene[pivot:]

return Chromosome(gene1), Chromosome(gene2)

Expand All @@ -69,7 +69,7 @@ def mutate(self):
will be created, but this original will not be affected.
"""
gene = list(self.gene)
delta = randint(0, 89) + 32
delta = randint(32, 121)
idx = randint(0, len(gene) - 1)
gene[idx] = chr((ord(gene[idx]) + delta) % 122)

Expand All @@ -95,11 +95,11 @@ def gen_random():
"""
gene = []
for x in range(len(Chromosome._target_gene)):
gene.append(chr(randint(0, 89) + 32))
gene.append(chr(randint(32, 121)))

return Chromosome(''.join(gene))

class Population(object):
class Population:
"""
A class representing a population for a genetic algorithm simulation.
Expand Down Expand Up @@ -128,9 +128,9 @@ def _tournament_selection(self):
A helper method used to select a random chromosome from the
population using a tournament selection algorithm.
"""
best = self.population[randint(0, len(self.population) - 1)]
best = choice(self.population)
for i in range(Population._tournamentSize):
cont = self.population[randint(0, len(self.population) - 1)]
cont = choice(self.population)
if (cont.fitness < best.fitness): best = cont

return best
Expand All @@ -149,7 +149,7 @@ def evolve(self):
"""
size = len(self.population)
idx = int(round(size * self.elitism))
buf = self.population[0:idx]
buf = self.population[:idx]

while (idx < size):
if random() <= self.crossover:
Expand All @@ -168,7 +168,7 @@ def evolve(self):
buf.append(self.population[idx])
idx += 1

self.population = list(sorted(buf[0:size], key=lambda x: x.fitness))
self.population = list(sorted(buf[:size], key=lambda x: x.fitness))

if __name__ == "__main__":
maxGenerations = 16384
Expand Down

0 comments on commit 12f5a74

Please sign in to comment.