def tournamentSelect(pop, SCORES, scorefunc, scoreparams, T, N): """ Return N individuals from the population by conducting tournaments of size T. Each tournament has exactly one winner """ from population import functify # from population import score answer = [] for _ in xrange(N): gladiators = sample(pop, T) for g in gladiators: if functify(g) not in SCORES: SCORES[functify(g)] = scorefunc(g, *scoreparams) answer.append(max(gladiators, key=lambda p: SCORES[functify(p)])) return answer
def getRouletteWheel(population, SCORES, scorefunc, scoreparams): """ Return a fitness-proportional roulette wheel for the population""" from population import functify wheel={} top = 0.0 for p in population: if functify(p) not in SCORES: SCORES[functify(p)] = scorefunc(p, *scoreparams) totalscore = abs(sum((SCORES[functify(p)] for p in population))) for p in population: fit = SCORES[functify(p)]/totalscore wheel[(top, top+fit)] = clone(p) top += fit # print 'returning wheel' ## return wheel
def headlessChickenMut(indiv, SCORES, F, T, scorefunc, scoreparams, maxheight): from population import Node, generateFull, generateGrow, functify height = getHeight(indiv) chicken, rooster = generateFull(F, T, Node(), height), generateGrow(F, T, Node(), height, maxheight) c1, c2 = crossover(indiv, chicken, maxheight) c3, c4 = crossover(indiv, rooster, maxheight) c5, c6 = crossover(chicken, rooster, maxheight) for c in [c1, c2, c3, c4, c5, c6]: if functify(c) not in SCORES: SCORES[functify(c)] = scorefunc(c, *scoreparams) return sorted([c1, c2, c3, c4, c5, c6], key=lambda f: SCORES[functify(c)], reverse=True) #if __name__ == "__main__": # print 'starting' # # from cPickle import load # n = load(open('testbed')) # # print GCP(n,7) # # print 'done'