此书我才看了没几页。但是其中的代码实在是精妙,不由顿生不明觉厉的感慨。
最早的版本是数据和算法混合在一起的代码:
-
import string
-
import random
-
-
geneSet=string.ascii_letters+" !?.,"
-
target="Hello World!"
-
-
def generate_parent(length):
-
genes=[]
-
while len(genes) < length:
-
sampleSize=min((length - len(genes)), len(geneSet))
-
genes.extend(random.sample(geneSet, sampleSize))
-
return ''.join(genes)
-
-
-
#print(generate_parent(10))
-
-
def get_fitness(guess):
-
return sum(1 for expected, actual in zip(target, guess) if expected == actual)
-
-
def mutate(parent):
-
index=random.randrange(0,len(parent))
-
childGenes=list(parent)
-
newGene, alternate=random.sample(geneSet, 2)
-
childGenes[index]=alternate if newGene ==childGenes[index] else newGene
-
return ''.join(childGenes)
-
-
-
import datetime
-
-
def display(guess):
-
timeDiff=datetime.datetime.now() - startTime
-
fitness=get_fitness(guess)
-
print("{0}\t{1}\t{2}".format(guess, fitness, str(timeDiff)))
-
-
-
random.seed()
-
startTime=datetime.datetime.now()
-
bestParent=generate_parent(len(target))
-
bestFitness=get_fitness(bestParent)
-
display(bestParent)
-
-
while True:
-
child=mutate(bestParent)
-
childFitness=get_fitness(child)
-
if bestFitness >= childFitness:
-
continue
-
display(child)
-
if childFitness >= len(bestParent):
-
break
-
bestFitness=childFitness
-
bestParent=child
然后第二个版本是讲算法单独抽出来做了个genetic.py.
将实际的应用写入guesspassword.py
genetic.py 如下:
-
import random
-
-
def _generate_parent(length, geneSet):
-
genes=[]
-
while len(genes) < length:
-
sampleSize=min(length-len(genes), len(geneSet))
-
genes.extend(random.sample(geneSet, sampleSize))
-
return ''.join(genes)
-
-
def _mutate(parent, geneSet):
-
index=random.randrange(0, len(parent))
-
childGenes=list(parent)
-
newGene, alternate=random.sample(geneSet, 2)
-
if newGene==childGenes[index]:
-
childGenes[index]=alternate
-
else:
-
childGenes[index]=newGene
-
-
return ''.join(childGenes)
-
-
-
def get_best(get_fitness, targetLen, optimalFitness, geneSet, display):
-
random.seed()
-
bestParent=_generate_parent(targetLen, geneSet)
-
bestFitness=get_fitness(bestParent)
-
display(bestParent)
-
-
if bestFitness >= optimalFitness:
-
return bestParent
-
-
while True:
-
child=_mutate(bestParent, geneSet)
-
childFitness = get_fitness(child)
-
if bestFitness >= childFitness:
-
continue
-
-
display(child)
-
if childFitness >= optimalFitness:
-
return child
-
bestFitness = childFitness
-
bestParent=child
guesspassword.py 内容如下:
-
import datetime
-
import genetic
-
import string
-
import unittest
-
-
def test_hello_world():
-
target="Hello World!"
-
guess_password(target)
-
-
def guess_password(target):
-
geneset=string.ascii_letters+" !?.,"
-
startTime=datetime.datetime.now()
-
-
def fnGetFitness(genes):
-
return get_fitness(genes,target)
-
-
def fnDisplay(genes):
-
display(genes, target, startTime)
-
-
optimalFitness=len(target)
-
genetic.get_best(fnGetFitness, len(target), optimalFitness, geneset, fnDisplay)
-
-
def display(genes, target, startTime):
-
timeDiff=datetime.datetime.now() - startTime
-
fitness= get_fitness(genes, target)
-
print("{0}\t{1}\t{2}".format(genes, fitness, str(timeDiff)))
-
-
def get_fitness(genes, target):
-
return sum( 1 for expected, actual in zip(target, genes) if expected==actual)
-
-
-
#test_hello_world()
-
-
-
def fnDisplay(genes):
-
display(genes, target, startTime)
-
-
class GuessPasswordTests(unittest.TestCase):
-
geneset=string.ascii_letters+" !?,."
-
startTime=datetime.datetime.now()
-
-
def test_001_hello_world(self):
-
target="Hello World!"
-
self.guess_password(target)
-
-
def guess_password(self, target):
-
def fnGetFitness(genes):
-
return get_fitness(genes,target)
-
-
def fnDisplay(genes):
-
display(genes, target, self.startTime)
-
-
optimalFitness=len(target)
-
best=genetic.get_best(fnGetFitness, len(target), optimalFitness, self.geneset, fnDisplay)
-
self.assertEqual(best, target)
-
-
-
def test_002_for_i_am_fearfully_and_wonderfully_made(self):
-
target="For I am fearfully and wonderfully made."
-
self.guess_password(target)
-
-
unittest.main()
后记:
因为书上用了statistics 这个library,害得我在kali 上找了半天,不知道为啥官方源把我定向到kali.mirror.garr.it,超级慢,apt-get update 要跑3天。
还是改了好。
-
#中科大kali source
-
deb http://mirrors.ustc.edu.cn/kali kali-rolling main contrib non-free
-
deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main contrib non-free
瞎猜一通,感觉astlib 像是,最后还给我蒙对了
-
apt-cache search statistics |grep -i python3
-
apt-get install astlib
-
>>> import statistics
-
>>> statistics.__file__
-
'/usr/lib/python3.5/statistics.py'
-
>>> statistics.__name__
-
'statistics'
阅读(1997) | 评论(0) | 转发(0) |