Permalink
Browse files

separate solver code from test specific code

  • Loading branch information...
1 parent a1f9c6d commit 36905751485840ca5a0f93281d054a916ba90895 @handcraftsman committed Jun 9, 2015
Showing with 49 additions and 42 deletions.
  1. +24 −42 genetic.py
  2. +25 −0 stringDuplicationTests.py
View
@@ -1,49 +1,31 @@
-#!/usr/bin/python
+import random
-import random
-import datetime
-
-def getFitness(candidate, target):
- fitness = 0
- for i in range(0, len(candidate)):
- if target[i] == candidate[i]:
- fitness += 1
- return(fitness)
-
-def mutate(candidate):
- geneIndex = random.randint(0, len(geneset) -1);
- index = random.randint(0, len(candidate) - 1)
- genes = list(candidate)
- genes[index] = geneset[geneIndex]
+def mutate(parent, geneSet):
+ geneIndex = random.randint(0, len(geneSet) -1);
+ index = random.randint(0, len(parent) - 1)
+ genes = list(parent)
+ genes[index] = geneSet[geneIndex]
return(''.join(genes))
-def generateParent(length):
+def generateParent(length, geneSet):
genes = list("")
for i in range(0,length):
- geneIndex = random.randint(0, len(geneset) -1);
- genes.append(geneset[geneIndex])
+ geneIndex = random.randint(0, len(geneSet) -1);
+ genes.append(geneSet[geneIndex])
return(''.join(genes))
-def display(candidate, startTime):
- timeDiff = datetime.datetime.now() - startTime
- fitness = getFitness(candidate, target)
- print ("%s\t%i\t%s" % (candidate, fitness, str(timeDiff)))
-
-
-geneset = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!."
-target = "Not all those who wander are lost."
-
-random.seed()
-startTime = datetime.datetime.now()
-bestParent = generateParent(len(target))
-bestFitness = getFitness(bestParent, target)
-display(bestParent, startTime)
-
-while bestFitness < len(bestParent):
- child = mutate(bestParent)
- childFitness = getFitness(child, target)
-
- if childFitness > bestFitness:
- bestFitness = childFitness
- bestParent = child
- display(bestParent, startTime)
+def getBest(get_fitness, display, targetLen, geneSet):
+ random.seed()
+ bestParent = generateParent(targetLen, geneSet)
+ bestFitness = get_fitness(bestParent)
+ display(bestParent)
+
+ while bestFitness < len(bestParent):
+ child = mutate(bestParent, geneSet)
+ childFitness = get_fitness(child)
+
+ if childFitness > bestFitness:
+ bestFitness = childFitness
+ bestParent = child
+ display(bestParent)
+ return bestParent
@@ -0,0 +1,25 @@
+import datetime
+import genetic
+
+def getFitness(candidate, target):
+ fitness = 0
+ for i in range(0, len(candidate)):
+ if target[i] == candidate[i]:
+ fitness += 1
+ return(fitness)
+
+def display(candidate, target, startTime):
+ timeDiff = datetime.datetime.now() - startTime
+ fitness = getFitness(candidate, target)
+ print ("%s\t%i\t%s" % (candidate, fitness, str(timeDiff)))
+
+def test_string_duplication():
+ geneset = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!."
+ startTime = datetime.datetime.now()
+ target = "Not all those who wander are lost."
+ fnDisplay = lambda candidate: display(candidate, target, startTime)
+ fnGetFitness = lambda candidate: getFitness(candidate, target)
+ best = genetic.getBest(fnGetFitness, fnDisplay, len(target), geneset)
+
+if __name__ == '__main__':
+ test_string_duplication()

0 comments on commit 3690575

Please sign in to comment.