DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.85k stars 1.13k forks source link

call custom code for each individual in every population #236

Open dwightee opened 7 years ago

dwightee commented 7 years ago

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:

def inspectPopulation(population):
         for individual in population:
                 print individual.name + " " + individual.score 

basically I need something so that I can iterate over all individuals in each generation.

fmder commented 7 years ago

What is wrong about putting that after modifying the individuals in your main loop?

dwightee commented 7 years ago

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,)

ghost commented 6 years ago

I wish to do the same thing, have you figured out a way how to do this please?

dwightee commented 6 years ago

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

ghost commented 6 years ago

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.

erap129 commented 5 years ago

Hi, any updates here? Want to be able to run library functions along with custom code in each generation.