from random import randint,randomfrom operator import addfrom copy import deepcopy
def individual(length, min, max): #'Create a member of the population.' return [ randint(min,max) for x in xrange(length) ]def population(count, length, min, max): return [ individual(length, min, max) for x in xrange(count) ]def fitness(individual, target): sum = reduce(add, individual, 0) return abs(target-sum)def grade(pop, target): summed = reduce(add, (fitness(x, target) for x in pop), 0) return summed / (len(pop) * 1.0)def evolve(pop, target, retain=0.2, random_select=0.05, mutate=0.01): graded = [ (fitness(x, target), x) for x in pop] graded = [ x[1] for x in sorted(graded)] retain_length = int(len(graded)*retain) parents = graded[:retain_length] # randomly add other individuals to promote genetic diversity for individual in graded[retain_length:]: if random_select > random(): parents.append(individual) # mutate some individuals for individual in parents: if mutate > random(): pos_to_mutate = randint(0, len(individual)-1) # this mutation is not ideal, because it # restricts the range of possible values, # but the function is unaware of the min/max # values used to create the individuals, individual[pos_to_mutate] = randint( min(individual), max(individual)) # crossover parents to create children parents_length = len(parents) desired_length = len(pop) - parents_length children = [] while len(children) < desired_length: male = randint(0, parents_length-1) female = randint(0, parents_length-1) if male != female: male = parents[male] female = parents[female] half = len(male) / 2 child = male[:half] + female[half:] children.append(child) parents.extend(children) return parents iTarget = 76p = population(20,6,0,40)fitness_history = [grade(p, iTarget),]arBestP = []iTryGA = 1000 #try thousand times iMxScore = iTargetiLoopCnt = iTryGAfor i in xrange(iTryGA): p = evolve(p, iTarget) iScore = grade(p, iTarget) fitness_history.append(iScore) if iScore < iMxScore: iMxScore = iScore arBestP = deepcopy(p) if iScore == 0: iLoopCnt = i break; print "mx score = %s loop count = %s" % (iMxScore,iLoopCnt) print "Target:%s Best GA Pick:%s = %s" % (iTarget, arBestP[0],reduce(add, arBestP[0], 0)) #for datum in fitness_history: # print datum
def individual(length, min, max): #'Create a member of the population.' return [ randint(min,max) for x in xrange(length) ]
def population(count, length, min, max): return [ individual(length, min, max) for x in xrange(count) ]
def fitness(individual, target): sum = reduce(add, individual, 0) return abs(target-sum)
def grade(pop, target): summed = reduce(add, (fitness(x, target) for x in pop), 0) return summed / (len(pop) * 1.0)
def evolve(pop, target, retain=0.2, random_select=0.05, mutate=0.01): graded = [ (fitness(x, target), x) for x in pop] graded = [ x[1] for x in sorted(graded)] retain_length = int(len(graded)*retain) parents = graded[:retain_length]
# randomly add other individuals to promote genetic diversity for individual in graded[retain_length:]: if random_select > random(): parents.append(individual) # mutate some individuals for individual in parents: if mutate > random(): pos_to_mutate = randint(0, len(individual)-1) # this mutation is not ideal, because it # restricts the range of possible values, # but the function is unaware of the min/max # values used to create the individuals, individual[pos_to_mutate] = randint( min(individual), max(individual)) # crossover parents to create children parents_length = len(parents) desired_length = len(pop) - parents_length children = [] while len(children) < desired_length: male = randint(0, parents_length-1) female = randint(0, parents_length-1) if male != female: male = parents[male] female = parents[female] half = len(male) / 2 child = male[:half] + female[half:] children.append(child)
parents.extend(children) return parents
iTarget = 76p = population(20,6,0,40)
fitness_history = [grade(p, iTarget),]
arBestP = []iTryGA = 1000 #try thousand times iMxScore = iTargetiLoopCnt = iTryGA
for i in xrange(iTryGA): p = evolve(p, iTarget) iScore = grade(p, iTarget) fitness_history.append(iScore)
if iScore < iMxScore: iMxScore = iScore arBestP = deepcopy(p)
if iScore == 0: iLoopCnt = i break;
print "mx score = %s loop count = %s" % (iMxScore,iLoopCnt) print "Target:%s Best GA Pick:%s = %s" % (iTarget, arBestP[0],reduce(add, arBestP[0], 0))
#for datum in fitness_history: # print datum