Question about genetic operators when inheriting from numpy.

74 views

Rémi Mochon

Aug 21, 2022, 1:34:14 PM8/21/22
to deap-users
Hello,

I am currently developping a lgp module in deap taking example on the existing gp module (and geppy). This lgp module stands for Linear Genetic Programming. The principle is roughly the same as in gp, but the individuals are not lists of primitives but 2D matrices of integers instead.

I chosed to create my individuals with a class inheriting from ndarray. I did it because some functions (like genetic operators) are, to my mind, cleaner written using numpy instead of a list of lists. I am not an expert in python, so I just findout that numpy arrays (unlike lists) can't be modified in place.

Here is my issue:  when two individuals get involved in a crossover operation, their respective size can change, and since they are numpy arrays, I can't modify them in place. Instead I have to create two new individual instances in the crossover operation and return them. So, for example, if I create two individuals and make a crossover operation with them:

# creating two individuals
ind_1 = toolbox.individual()
ind_2 = toolbox.individual()

# applying crossover operator, may modify the size of both individuals

offspring_1, offspring_2 = crossover(ind_1, ind_2)

Then "ind_1 is offspring_1" in python will return "False". This is because offspring_1, is a new instance of toolbox.individual(). In the gp module, since the individuals are inheriting from lists, the individuals can be modified in place, and I think they are in all genetic operators defined in the gp module. As a result, in gp, "ind_1 is offspring_1" returns "True".

My question: Is it a problem, within deap, to have genetic operators that return new individual instances, or must they modify the individuals in place ?

If it is a problem, then I shall create my individuals as list of lists (to mimic a 2D matrix) instead of using numpy.

I hope I am clear in my explanations. If needed, I can send my code.
Looking forward to an answer ! Best regards.

Rémi MOCHON

andrew morgan

Aug 21, 2022, 1:47:06 PM8/21/22
Hello Rémi

I’m not sure if it helps, but a while back I coded a custom mutation function called  varBornAgain(population, toolbox)   which might be interesting to review.

It’s accepts an individual and returns a completely random genetic individual, that is effectively a new genetic individual.
If I remember - I was experimenting with a "linear migration” rather than a ring migration for island models…and this provided fresh random genes at one end of the line of islands.

Code is here:

Andrew

--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/deap-users/d315bc84-ec43-46a7-a56f-a36113973444n%40googlegroups.com.

José Antonio Fuentes Tomás

Aug 21, 2022, 7:56:14 PM8/21/22