daffidwilde / matching

A package for solving matching games
https://daffidwilde.github.io/matching/
MIT License
149 stars 42 forks source link

RecursionError for `deepcopy` with 400x400 matching task #139

Open james016 opened 2 years ago

james016 commented 2 years ago

I bumped into RecursionError in the deepcopy reproduced by

import sys
from matching.games import StableMarriage

# fix by enlarging the size of recursionlimit
# sys.setrecursionlimit(10000)

num_x = 400
num_y = 400

x2y = {x:list(range(num_y)) for x in range(num_x)}
y2x = x2y

game = StableMarriage.create_from_dictionaries(x2y, y2x)

It cased by the deepcopy of players. By setting recursionlimit to a higher value can reduce the problem in this case with 400x400 matching task, I found. But it may be not reduced fundamentally.

daffidwilde commented 2 years ago

Hi @james016. Thanks for raising this. That is certainly annoying but the call to copy.deepcopy() is pretty integral to how the players get created and adjusted.

I will try to set aside some time to think about an alternative solution but, for now, I think your workaround will have to do. I don't know if I'm comfortable making global changes to the system recursion limit as part of matching.

Sorry to disappoint and thanks again for opening this up.

obscurerichard commented 2 years ago

I stumbled on this issue and explored a bit more about the Python recursion limit and found a StackOverflow answer giving an elegant way of wrapping the setting of recursionlimit using with. The article has a bunch of other interesting advice and solutions regarding deep recursion in Python too.