Skip to content

Instantly share code, notes, and snippets.

@yfe404
Created April 12, 2015 11:06
Show Gist options
  • Save yfe404/a36e2cfd4d2dc0934fee to your computer and use it in GitHub Desktop.
Save yfe404/a36e2cfd4d2dc0934fee to your computer and use it in GitHub Desktop.
A simple GA with - roulette-wheel sampling - population size 100- single point crossover rate 0.7- bitwise mutation rate 0.002- chromosome length 100- generations 500
__author__ = 'yafeunteun'
import pyevolve
from pyevolve import G1DList, GSimpleGA, Selectors
# This function is the evaluation function, we want
# to give high score to more one'ed chromosomes
def eval_func(chromosome):
score = 0.0
# iterate over the chromosome elements (items)
for value in chromosome:
if value==1:
score += 1.0
return score
def main():
# Genome instance
genome = G1DList.G1DList(100)
# The evaluator function (objective function)
genome.evaluator.set(eval_func)
genome.setParams(rangemin=0, rangemax=1)
ga = GSimpleGA.GSimpleGA(genome)
ga.selector.set(Selectors.GRouletteWheel)
ga.setCrossoverRate(0.7)
ga.setMutationRate(0.002)
ga.setGenerations(500)
# Do the evolution, with stats dump
# frequency of 10 generations
ga.evolve(freq_stats=10)
# Best individual
print ga.bestIndividual()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment