2

this question is a follow-on to answer of this question about python deap genetic algorithm library: How to add elimination mechanism in Python genetic algorithm based on DEAP

using reference code from deap github: https://github.com/DEAP/deap/blob/master/examples/ga/onemax.py

line 112 while max(fits) < 100 and g < 1000: #from onemax.py

on the deap github example 'onemax_mp.py': https://github.com/DEAP/deap/blob/master/examples/ga/onemax_mp.py

how do i add a max(or min) condition similar to max(fits) < 100 in the onemax_mp.py?

if i do add this condition is this condition applied to each process in the entire multi-process pool of processes? if one process meets the end condition are the other processes halted?
right now it seems that i can only control the number of generations:

https://github.com/DEAP/deap/blob/master/examples/ga/onemax_mp.py

line 40

algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40, stats=stats, halloffame=hof) #ngen=40 means calculate 40 generations

i am new to stackoverflow, please let me know if i need to edit this question to fit forum rules

bob smith
  • 23
  • 3

1 Answers1

0

So, the line you're looking at is the termination condition. The evolution stops when an individual with fitness greater than 100 is found or after 1000 generations. I've done a lot of work with MOEAs, but I'm not too familiar with DEAP. That disclaimer aside, it looks like it's not evolving separate populations, just doing parallelized evaluation. So there's only one population. From the docs, it looks like you could take onemax.py and slot in a multiprocessing pool by doing this:

import multiprocessing

pool = multiprocessing.Pool()
toolbox.register("map", pool.map)

I took this code from here: https://deap.readthedocs.io/en/master/tutorials/basic/part4.html

  • so i think you mean avoid the code in onemax_short.py and onemax_mp.py and just add multi-processing to the original onemax.py, did i read that right? – bob smith Dec 20 '18 at 02:25
  • Yup, if I'm reading the docs right, all you have to do is drop those lines in the original `onemax.py`. – Matthew Woodruff Dec 20 '18 at 15:30