TypeError: unsupported operand type(s) for /: 'tuple' and 'int' during stats.compile for DEAP

609 views
Skip to first unread message

Robin Müller-Bady

unread,
Jan 13, 2017, 12:13:48 PM1/13/17
to deap-users
Dear all,

I got a problem using the module and the stats module. Maybe one of the experienced users may help me with that.
Before: I'm using the module since more than 2 years, this is the first time this error occurs (therefore I think it comes from my side).

I'm creating an EA using DEAP 1.1. Individuals consist of Integer lists, where each element may be 0 or 1. The population itself is again a list.
Now, I create stats with the following elements:

stats = tools.Statistics(key=lambda ind: ind.fitness.values)
time_start
= time.time()

def timecap(ind):
 
return time.time() - time_start

stats
.register("avg", np.mean)
stats
.register("med", np.median)
stats
.register("std", np.std)
stats
.register("min", np.min)
stats
.register("max", np.max)
stats
.register("time", timecap)

Occasionally (sometimes after 1,000,000 evaluations / 200 generations, sometimes right after the start with the initial population), the following error occurs:

  File "/home/robin/workspace/untitled/monitor_selection.py", line 219, in EA
    record = stats.compile(population) if stats is not None else {}
  File "/usr/local/lib/python3.5/dist-packages/deap-1.1.0-py3.5-linux-x86_64.egg/deap/tools/support.py", line 208, in compile
    entry[key] = func(values)
  File "/usr/local/lib/python3.5/dist-packages/numpy/core/fromnumeric.py", line 2992, in std
    keepdims=keepdims)
  File "/usr/local/lib/python3.5/dist-packages/numpy/core/_methods.py", line 124, in _std
    keepdims=keepdims)
  File "/usr/local/lib/python3.5/dist-packages/numpy/core/_methods.py", line 94, in _var
    arrmean, rcount, out=arrmean, casting='unsafe', subok=False)
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

I tried to debug the code using PyCharms debugger, but unfortunately I can't find an error in my code (especially when it runs in most cases).
Do you have any idea?

Thank you very much in advance!

François-Michel De Rainville

unread,
Jan 13, 2017, 4:40:46 PM1/13/17
to deap-users
My first guess is that, for an unknown reason, you are giving an empty population to stats.compile.

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

Robin Müller-Bady

unread,
Jan 14, 2017, 8:06:11 AM1/14/17
to deap-users
Unfortunately, this is not the case. The population is present (I see the instances with my debugger).
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.

François-Michel De Rainville

unread,
Jan 14, 2017, 9:02:57 AM1/14/17
to deap-users
Using pdb I think you can add an instruction to start debugger on exception. You could then get a full stack trace and find what tuple cannot be divided by an integer.

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

Robin Müller-Bady

unread,
Jan 16, 2017, 5:35:03 AM1/16/17
to deap-users
Thank you very much. I found out that, for reasons yet unknown, one individual of my population is not evaluated. I don't know about your software engineering, but an error handling like follows could help debugging (at least for me :-) ):


in module deap/tools/support.py
def compile(self, data):
   
"""Apply to the input sequence *data* each registered function
    and return the results as a dictionnary.


    :param data: Sequence of objects on which the statistics are computed.
    """

    values
= tuple(self.key(elem) for elem in data)
   
assert all(values), "Error: not all values are evaluated!"
    entry
= dict()
   
for key, func in self.functions.items():
        entry
[key] = func(values)
   
return entry

However, the program crashes again, but at least you know why :-)

Best regards,
Robin

François-Michel De Rainville

unread,
Jan 16, 2017, 8:01:14 AM1/16/17
to deap-users
Thanks, we will look into adding an assertion like proposed.

To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages