add typed membership backend foundation with invariant tests.#1
add typed membership backend foundation with invariant tests.#1falloficaruss wants to merge 8 commits into
Conversation
|
Performance benchmarks:
|
Add get_group to GroupBy (mesa#3713)
| """Add one typed membership edge if it does not already exist.""" | ||
| if not isinstance(relation, str): | ||
| raise TypeError("relation must be a string") | ||
| triplet = (agent, group, relation) |
There was a problem hiding this comment.
Would it be better to use agent id here which has to be unique?
| def add_membership(self, agent: Hashable, group: Hashable, relation: str) -> None: | ||
| """Add one typed membership edge if it does not already exist.""" | ||
| if not isinstance(relation, str): | ||
| raise TypeError("relation must be a string") |
There was a problem hiding this comment.
I wanted to keep this first implementation simple. I guess I will change it be Hashable as well.
There was a problem hiding this comment.
RelationKey = Hashable might be better approach.
| """Initialize empty triplet storage and bidirectional indexes.""" | ||
| self._triplets: set[Triplet] = set() | ||
| self._by_agent: dict[Hashable, set[tuple[Hashable, str]]] = defaultdict(set) | ||
| self._by_group: dict[Hashable, set[tuple[Hashable, str]]] = defaultdict(set) |
There was a problem hiding this comment.
This is a very solid approach. Effectively just capturing the relations in a sparce matrix; nicely done. This gives a really strong base. Nice done!
| ) | ||
|
|
||
| def _to_id(self, entity: Hashable) -> Hashable: | ||
| """Normalize entities to canonical IDs when available.""" |
There was a problem hiding this comment.
This is a nitpick, but want to clearly articulate when somethings ties into the code mesa attributes or functions. In this case the docstring should read something like..
Use mesa.agent "unique_id" as triplet reference
| """Return all memberships as canonical triplets.""" | ||
| return set(self._triplets) | ||
|
|
||
| def assert_invariants(self) -> None: |
There was a problem hiding this comment.
Forgot to mention this is a very nice add
Thanks for opening a PR! Please click the
Previewtab and select a PR template:Feature/enhancement PRs require prior maintainer approval in an issue or discussion before they are accepted.