DEAP / deap

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

TypeError: Population must be a sequence. For dicts or sets, use sorted(d) #732

Closed harwoodr closed 9 months ago

harwoodr commented 10 months ago

I'm getting this on one system running python 3.11.2, but the exact same code works fine on a system running python 3.10.12 - any thoughts?

fengyangsun commented 10 months ago

It seems you are sorting a population represented by a dict or set, something like "pop.sorted()", which is not allowed. It usually works for list or ndarray. For sorting dict or set, you should use "sorted (pop)".

harwoodr commented 10 months ago

Checked with a "print(type(pop))" and got "<class 'list'>"... as far as I can see, I don't believe I'm doing any sorting...

Also, as I said, it works fine on another system - I'm going to test on yet another system...

fmder commented 10 months ago

It would be easier to debug with minimal reproducing example posted in a gist or similar.

harwoodr commented 10 months ago

Easy - take https://github.com/DEAP/deap/blob/master/examples/gp/parity.py and replace:

toolbox.register("mate", gp.cxOnePoint)

with

toolbox.register("mate", gp.cxOnePointLeafBiased, termpb=0.1)

On my one system it works, no problem, on the other I get:

Traceback (most recent call last):
  File "/home/harwood/parity.py", line 92, in <module>
    main()
  File "/home/harwood/parity.py", line 87, in main
    algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 40, stats, halloffame=hof)
  File "/home/harwood/.local/lib/python3.11/site-packages/deap/algorithms.py", line 168, in eaSimple
    offspring = varAnd(offspring, toolbox, cxpb, mutpb)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/harwood/.local/lib/python3.11/site-packages/deap/algorithms.py", line 73, in varAnd
    offspring[i - 1], offspring[i] = toolbox.mate(offspring[i - 1],
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/harwood/.local/lib/python3.11/site-packages/deap/gp.py", line 743, in cxOnePointLeafBiased
    type_ = random.sample(common_types, 1)[0]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/random.py", line 439, in sample
    raise TypeError("Population must be a sequence.  "
TypeError: Population must be a sequence.  For dicts or sets, use sorted(d).
harwoodr commented 10 months ago

Further investigation says it may be a change in Python 3.11 that is to blame.

https://github.com/ray-project/ray/issues/36578#issuecomment-1599753193

harwoodr commented 10 months ago

Added https://github.com/DEAP/deap/pull/733 pull request with the very minor change to fix.

fmder commented 10 months ago

Thanks for debugging this! I'll check if any other place in the library needs to be changed and push to your branch.