-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Hi Kartik,
As my models develop, it often helps to try to organize my code in different files. When agents are heterogeneous (e.g., different available actions, reward functions), one of the intuitive options is to divide them into separate python files (e.g., alice.py
, bob.py
), then import the necessary classes and functions from those files into one another.
When I use memo, this works with from A import a
, but not import A
. Is there a way to allow memo to use both syntaxes? I've got a reproducible example similar to the Memonomicon below:
We start with a simple model of Alice in alice.py
with no frills (no errors).
from memo import memo
from enum import IntEnum
class Cards(IntEnum):
JACK = 0
QUEEN = 1
KING = 2
@memo
def chooses_card[c: Cards](beta):
alice: chooses(c in Cards, wpp=exp(beta * c))
return E[alice.c == c]
Then, we attempt to import alice
into bob.py
in two ways. The first works, the second does not.
from memo import memo
# import for Version 1
from alice import Cards as AlicesCards
from alice import chooses_card as alice_chooses_card
# import for Version 2
import alice
# Version 1 - works
@memo
def chooses_card_v1[c: AlicesCards]():
bob: thinks[partner : chooses(c in AlicesCards, wpp=alice_chooses_card[c](3.0))]
bob: chooses(c in AlicesCards, wpp=E[~(c == partner.c)])
return E[bob.c == c]
# No errors, works as expected
# Version 2 - fails
@memo
def chooses_card_v2[c: alice.Cards]():
bob: thinks[partner : chooses(c in alice.Cards, wpp=alice.chooses_card[c](3.0))]
bob: chooses(c in alice.Cards, wpp=E[~(c == partner.c)])
return E[bob.c == c]
# AssertionError
# line 633, in parse_memo
# assert isinstance(tp.bound, ast.Name)
Is there a way to allow memo to work with version 2 also? Although neither import style is perfect, and support for both would be great, I think version two will often lead to clearer and more readable code. As models get larger and more complex, I think the benefit of version two also grows. Thoughts?
Thanks for your help,
Ian