carykh / PrisonersDilemmaTournament

Watch This Place's awesome video about iterated Prisoner's Dilemma for context! https://www.youtube.com/watch?v=BOvAbjfJ0x0
MIT License
205 stars 160 forks source link

Reload to clear module-level variables between games #60

Open cai-lw opened 3 years ago

cai-lw commented 3 years ago

Mitigates #58 by forcing to reload the module and reset its global variables (See https://docs.python.org/3/library/importlib.html#importlib.reload)

Confirmed that this fix makes the code in #58 no longer work.

However, there are still ways to get around, and I don't think a complete fix is possible. To prevent this completely we may have to rely on manual inspection.

cai-lw commented 3 years ago

The following code can still retain variables in MyMemory class between games.

import random

def strategy(history, memory):
    round = history.shape[1] + 1
    if round == 1:
        # This should never be 1
        print(MyMemory.test)
        return 1, MyMemory
    mem = memory
    mem.test = 1
    return 1, mem

try:
    MyMemory
except NameError:
    class MyMemory:
        test = 0