AttributeError: 'numpy.ndarray' object has no attribute 'fitness'

2,742 views
Skip to first unread message

Özlem Dalgıç

unread,
Dec 1, 2016, 11:53:19 AM12/1/16
to deap-users
Hello,

I am trying to make a basic maze generator with deap library. My individual is a two dimentional array like this:

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 1 0 0 0 0]
 [0 0 0 1 1 1 1 0 0 0]
 [0 1 1 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 1 0 0 0 0 0]
 [0 0 0 0 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

1 represents blocks and 0 represents hallways. I want to get best individual according to my own fitness functions.

My problem is,
...
popu = toolbox.population(n=3)
algorithms.eaSimple(popu, toolbox, cxpb=CXPB, mutpb=MUTPB, ngen=NGEN)
....

at the second line I am getting this error:

invalid_ind = [ind for ind in population if not ind.fitness.valid]
AttributeError: 'numpy.ndarray' object has no attribute 'fitness'

How can I handle this problem?
Thank you very much.




Marc-André Gardner

unread,
Dec 1, 2016, 3:40:22 PM12/1/16
to deap-users
Hi,

How are you declaring your individual in the creator? How do you initialize "popu"? Technically, inheriting from numpy arrays should work in most cases (see https://deap.readthedocs.io/en/master/tutorials/advanced/numpy.html for the edge cases that you actually have to handle), but it has to be declared correctly. Can we see the lines beginning by "creator.create" and the population initialization function?

Marc-André

François-Michel De Rainville

unread,
Dec 1, 2016, 3:42:51 PM12/1/16
to deap-users
The problem can also lie in the crossover/mutation operators.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Özlem Dalgıç

unread,
Dec 1, 2016, 4:09:46 PM12/1/16
to deap-...@googlegroups.com
Thank you for your response.
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator
.create("Individual", list, fitness=creator.FitnessMax)

toolbox.register("individual", tools.initIterate, createIndividual, creator.Individual)
toolbox.register("population", list, toolbox.individual)

in createIndividual function, individual type is numpy array but
still should I change the list type with numpy.ndarray in these lines?
Thank you.

--
You received this message because you are subscribed to a topic in the Google Groups "deap-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/deap-users/alxeOILteJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to deap-users+unsubscribe@googlegroups.com.

Marc-André Gardner

unread,
Dec 1, 2016, 4:15:37 PM12/1/16
to deap-users
Yes, you must change your individual type from list to numpy.ndarray (that would be the same thing if you would like to inherit from, say, array.array). You may see it as a standard inheritance, where you must tell Python your parent class :

class Individual(my_parent_class)

Modify the creator lines and it should work, providing that createInvidual works correctly.

Marc-André
To unsubscribe from this group and all its topics, send an email to deap-users+...@googlegroups.com.

Özlem Dalgıç

unread,
Dec 1, 2016, 4:27:53 PM12/1/16
to deap-...@googlegroups.com
Thanks for your help.

Have a nice day.




To unsubscribe from this group and all its topics, send an email to deap-users+unsubscribe@googlegroups.com.

M-I A

unread,
May 5, 2017, 2:15:20 PM5/5/17
to deap-users
Hi there,

I would like to ask for your help - I am encountering the same problem as above and unfortunately I can't seem to be able to fix it with the way described above... the code I copy below fails with:
 
  algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=50)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/deap/algorithms.py", line 146, in eaSimple

    invalid_ind = [ind for ind in population if not ind.fitness.valid]
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/deap/algorithms.py", line 146, in <listcomp>

    invalid_ind = [ind for ind in population if not ind.fitness.valid]
AttributeError: 'numpy.ndarray' object has no attribute 'fitness'

the DEAP code is:

# Fitness: 
creator.create("FitnessMulti", base.Fitness, weights=(-1.0, 1.0))
creator.create("Individual", numpy.ndarray, dtype=int, fitness=creator.FitnessMulti)
# toolbox
toolbox = base.Toolbox()
toolbox.register("individual", create_individual, n_c, n_d)
toolbox.register("population", tools.initRepeat, list, toolbox.individual) # short-hand for repeating individual creation
toolbox.register("evaluate", evaluate_individual, disc=disc, cpp=cpp, cpd=cpd)
toolbox.register("mate", crossover_individuals)
toolbox.register("mutate", mutate_individual)
toolbox.register("select", tools.selTournament, tournsize=3)
# initial (random) population
pop = toolbox.population(n=20)
# optimise
algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=50)

Checks:

the calls to toolbox functions seem to check OK in terms of types:
 
>>> create_individual(10,4)
array([[1, 0, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0],
       [1, 0, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0],
       [1, 0, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0]])
>>> type(create_individual(10,4))
<class 'numpy.ndarray'>
>>> 
>>> pop = toolbox.population(4)
>>> type(pop[0])
<class 'numpy.ndarray'>
>>> toolbox.evaluate(pop[0])
(30.7, 1.0794631944831576)
>>> toolbox.mate(pop[0],pop[1])
array([[0, 1, 0, 0],
       [0, 1, 0, 0],
       [1, 0, 0, 0],
       [1, 0, 0, 0],
       [1, 0, 0, 0],
       [0, 0, 1, 0],
       [0, 1, 0, 0],
       [1, 0, 0, 0],
       [0, 1, 0, 0],
       [1, 0, 0, 0]])
>>> type(toolbox.mate(pop[0],pop[1]))
<class 'numpy.ndarray'>
>>> toolbox.mutate(pop[0])
array([[1, 0, 0, 0],
       [0, 0, 1, 0],
       [0, 1, 0, 0],
       [1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 1, 0, 0],
       [1, 0, 0, 0],
       [1, 0, 0, 0],
       [1, 0, 0, 0],
       [0, 0, 1, 0]])
>>> type(toolbox.mutate(pop[0]))
<class 'numpy.ndarray'>


I would really appreciate any tips - going mad/blind after re-reading 2000 times that bit of code !

Best regards,
M-I

François-Michel De Rainville

unread,
May 5, 2017, 2:24:58 PM5/5/17
to deap-users
Your crossover/mutation output numpy arrays rather than individuals. Check the types and make sure you output individuals. The best way is usually to replace the content of arrays with something like this :

ind1[:] = ind2

Cheers

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+unsubscribe@googlegroups.com.

MIA

unread,
May 6, 2017, 7:26:37 AM5/6/17
to deap-users

Thanks so much clearer now and working.
Merci beaucoup
M-I


To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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.

Nicola Lazzarini

unread,
Aug 27, 2018, 12:35:02 PM8/27/18
to deap-users
I have a similar problem. My crossover function is:

def cxTwoPointCopy(ind1, ind2):
    ix = random.randint(1,N)
    new1a = ind1[:,:ix]
    new1b = ind2[:,ix:]
    new1 = np.concatenate((new1a,new1b),axis=1)
    new2a = ind2[:,:ix]
    new2b = ind1[:,ix:]
    new2 = np.concatenate((new2a,new2b),axis=1)
    return new1, new2

How do I cast from numpy array to individual?
My init function is:

def init2d(icls,N):
    IND = np.zeros((N,N))
    for i in range(N):
        for j in range(N):
            IND[i][j]= random.randint(1,N)
    return icls(IND)

toolbox.register("individual", init2d, creator.Individual, N)

So I have access to icls, but how can I do it for the crossover function?

Thanks

François-Michel De Rainville

unread,
Aug 27, 2018, 12:38:07 PM8/27/18
to deap-users
You have to modify the individuals inplace. To do that, use slicing and views of your numpy arrays.
Reply all
Reply to author
Forward
0 new messages