'''returns a float which is the populations average score''' total = sum(pop) return float(total) / float(len(pop)) def bestScore(pop, minmax='max'): '''returns the Gen with the highest score in a population''' if minmax == 'max': return sorted(pop, key=attrgetter('rawScore'))[len(pop) - 1] else: return sorted(pop, key=attrgetter('rawScore'))[0] lower = 0 # bounds for Gen parameters upper = 100 rpop = initialPop(20, lower, upper, 5) # initializes pops ypop = initialPop(20, lower, upper, 5) for i in range(1000): testFitFunc2(rpop, ypop) # sets pop scores if i % 100 == 0: # prints every 100th generation print('Generation', i) print(str('r ') + str(averageScore(rpop)) + ' ' + str(bestScore(rpop))) print(str('y ') + str(averageScore(ypop)) + ' ' + str(bestScore(ypop))) #Evolves! (gets the new pop) rpop = nextpop(rpop, .05, lower, upper, 'max') ypop = nextpop(ypop, .05, lower, upper, 'max')
def averageScore(pop): '''returns a float which is the populations average score''' total = sum(pop) return float(total)/float(len(pop)) def bestScore(pop,minmax='max'): '''returns the Gen with the highest score in a population''' if minmax == 'max': return sorted(pop,key=attrgetter('rawScore'))[len(pop)-1] else: return sorted(pop,key=attrgetter('rawScore'))[0] lower = 0 # bounds for Gen parameters upper = 100 rpop = initialPop(20,lower,upper,5) # initializes pops ypop = initialPop(20,lower,upper,5) for i in range(1000): testFitFunc2(rpop,ypop) # sets pop scores if i%100 == 0: # prints every 100th generation print('Generation',i) print(str('r ')+str(averageScore(rpop))+' '+str(bestScore(rpop))) print(str('y ')+str(averageScore(ypop))+' '+str(bestScore(ypop))) #Evolves! (gets the new pop) rpop = nextpop(rpop,.05,lower,upper,'max') ypop = nextpop(ypop,.05,lower,upper,'max')
sort = sorted(pop, key=attrgetter('rawScore')) return sort[0], sort[10] def bestScore(pop, minmax='max'): '''returns the Gen with the highest score in a population''' if minmax == 'max': return sorted(pop, key=attrgetter('rawScore'))[len(pop) - 1] else: return sorted(pop, key=attrgetter('rawScore'))[0] #Initial parameters lower = -10 #lower and upper are bounds for the Gen parameters upper = 10 rpop = initialPop(20, lower, upper, 8) #Initializes predator and prey populations ypop = initialPop(20, lower, upper, 8) #Set up log to collect data log = open("6BestCoEvolveLog.txt", 'a') log.write('NewTest\n') for i in range(5): #Runs this number of generations #Calculates the population scores fitFunc(rpop, ypop) #Log relevant data print 'Generation ' + str(i) br, ar = bestAverageScore(rpop, 'min') by, ay = bestAverageScore(ypop) aSr = averageScore(rpop) aSy = averageScore(ypop)
sort = sorted(pop,key=attrgetter('rawScore')) return sort[len(pop)-1], sort[11] else: sort = sorted(pop,key=attrgetter('rawScore')) return sort[0], sort[10] def bestScore(pop,minmax='max'): '''returns the Gen with the highest score in a population''' if minmax == 'max': return sorted(pop,key=attrgetter('rawScore'))[len(pop)-1] else: return sorted(pop,key=attrgetter('rawScore'))[0] #Initial parameters lower = -10 #lower and upper are bounds for the Gen parameters upper = 10 rpop = initialPop(20,lower,upper,8) #Initializes predator and prey populations ypop = initialPop(20,lower,upper,8) #Set up log to collect data log = open("6BestCoEvolveLog.txt",'a') log.write('NewTest\n') for i in range(5): #Runs this number of generations #Calculates the population scores fitFunc(rpop,ypop) #Log relevant data print 'Generation '+str(i) br,ar = bestAverageScore(rpop,'min') by,ay = bestAverageScore(ypop) aSr = averageScore(rpop) aSy = averageScore(ypop)