Open dwightee opened 7 years ago
What is wrong about putting that after modifying the individuals in your main loop?
well that is the tricky thing here, I dont really have any loops here. I run code with a library function:
algorithms.eaSimple(pop, toolbox, 0, self.mut_prob, self.gen_count, halloffame=hof,)
I wish to do the same thing, have you figured out a way how to do this please?
Yes, I did. You simply create a For Loop where you implement all the necessary parts you need. Let me give you an inspiration:
for g in range(NGEN):
# Select the next generation individuals
offspring = toolbox.select(population, len(population))
# Clone the selected individuals
offspring = map(toolbox.clone, offspring)
# Apply mutation on the offspring
for ofIndex in range(len(offspring)):
mutant = offspring[ofIndex]
if random.random() < MUTPB:
ancestor = toolbox.clone(mutant)
offspring[ofIndex] = mutateWithLimit(mutant)
self.printMutationChange(ancestor, mutant)
# Use elitism for
offspring.sort(key = attrgetter('fitness'), reverse = True) # <- SORT !
for ind in range(ELITISM):
potential = population[ind]
if offspring[POPS-1-ind].score < potential.score:
offspring[POPS-1-ind] = potential
# Remove duplicates in the offspring
offspring.sort(key = attrgetter('fitness'), reverse = True) # <- SORT !
for outer in range(len(offspring)):
for inner in range(outer+1, len(offspring)):
if str(offspring[outer]) == str(offspring[inner]):
print >>sys.stderr, ' *'
offspring[inner] = mutateWithLimit(offspring[inner])
# Try selective mutations on BEST individuals
offspring.sort(key = attrgetter('fitness'), reverse = True) # <- SORT !
needsSorting = False
for index in range(parameters.ELITISM_TUNE_UP):
best = toolbox.clone(offspring[0])
newBest = mutateWithLimit(best)
if newBest.score > best.score:
offspring[POPS-1-ind] = newBest
needsSorting = True
break
if needsSorting:
offspring.sort(key = attrgetter('fitness'), reverse = True) # <- SORT !
population[:] = offspring
if you need any other help do not hesitate to ask
Thank you for your reply. I was going to try using the history module because I think it should give me all the information I require. If that fails, I'll try and use your approach.
Thanks again for your time. Much appreciated.
Hi, any updates here? Want to be able to run library functions along with custom code in each generation.
Hi guys I tried to look everywhere, I was not able to find what I was looking for. Can anybody help ?
I want to call a custom code / function at the very end of population changes. Example would be a function:
basically I need something so that I can iterate over all individuals in each generation.