Skip to content

add typed membership backend foundation with invariant tests.#1

Open
falloficaruss wants to merge 8 commits into
mainfrom
backend
Open

add typed membership backend foundation with invariant tests.#1
falloficaruss wants to merge 8 commits into
mainfrom
backend

Conversation

@falloficaruss
Copy link
Copy Markdown
Owner

Thanks for opening a PR! Please click the Preview tab and select a PR template:

Feature/enhancement PRs require prior maintainer approval in an issue or discussion before they are accepted.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.8% [+0.3%, +1.4%] 🔵 -1.3% [-1.5%, -1.2%]
BoltzmannWealth large 🔵 +1.3% [+0.8%, +1.8%] 🔵 +1.2% [+0.1%, +2.4%]
Schelling small 🔵 +1.3% [+0.9%, +1.8%] 🔵 +0.1% [-0.6%, +0.8%]
Schelling large 🔵 +1.9% [+1.4%, +2.3%] 🔵 +1.3% [+0.5%, +2.2%]
WolfSheep small 🔵 +0.6% [+0.2%, +1.0%] 🔵 +0.7% [-3.6%, +5.1%]
WolfSheep large 🔵 +1.5% [+0.8%, +2.2%] 🔴 +5.2% [+3.8%, +7.0%]
SugarscapeG1mt small 🔵 +0.3% [-0.1%, +0.7%] 🔵 +0.3% [-1.0%, +1.6%]
SugarscapeG1mt large 🔵 +0.3% [-0.9%, +1.4%] 🔵 +1.4% [+0.3%, +2.5%]
BoidFlockers small 🔵 +0.2% [-0.3%, +0.6%] 🔵 -0.2% [-0.5%, +0.2%]
BoidFlockers large 🔵 +0.5% [-0.0%, +0.9%] 🔵 -0.3% [-0.5%, -0.1%]

"""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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use agent id here which has to be unique?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, will do.

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")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why restrict this to a string?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep this first implementation simple. I guess I will change it be Hashable as well.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@falloficaruss falloficaruss requested a review from tpike3 May 17, 2026 15:11
@falloficaruss falloficaruss self-assigned this May 19, 2026
)

def _to_id(self, entity: Hashable) -> Hashable:
"""Normalize entities to canonical IDs when available."""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention this is a very nice add

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants