/usr/share/doc/python-pyevolve-doc/examples/pyevolve_ex17_gtree.py is in python-pyevolve-doc 0.6~rc1+svn398+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | from pyevolve import GSimpleGA
from pyevolve import GTree
from pyevolve import Crossovers
from pyevolve import Mutators
import time
import random
def eval_func(chromosome):
score = 0.0
# If you want to add score values based
# in the height of the Tree, the extra
# code is commented.
#height = chromosome.getHeight()
for node in chromosome:
score += (100 - node.getData())*0.1
#if height <= chromosome.getParam("max_depth"):
# score += (score*0.8)
return score
def run_main():
genome = GTree.GTree()
root = GTree.GTreeNode(2)
genome.setRoot(root)
genome.processNodes()
genome.setParams(max_depth=3, max_siblings=2, method="grow")
genome.evaluator.set(eval_func)
genome.crossover.set(Crossovers.GTreeCrossoverSinglePointStrict)
ga = GSimpleGA.GSimpleGA(genome)
ga.setGenerations(100)
ga.setMutationRate(0.05)
ga.evolve(freq_stats=10)
print ga.bestIndividual()
if __name__ == "__main__":
run_main()
|