Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
harwoodr opened this issue Nov 17, 2023 · 7 comments · Fixed by #762
Closed

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

harwoodr opened this issue Nov 17, 2023 · 7 comments · Fixed by #762

Comments

@harwoodr
Copy link

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
Copy link

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
Copy link
Author

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
Copy link
Member

fmder commented Nov 17, 2023

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

@harwoodr
Copy link
Author

harwoodr commented Nov 17, 2023

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
Copy link
Author

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

ray-project/ray#36578 (comment)

@harwoodr
Copy link
Author

Added #733 pull request with the very minor change to fix.

@fmder
Copy link
Member

fmder commented Nov 17, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants