Skip to content

fix(emperor-dilemma): replace relative imports and fix unseeded random calls#463

Open
abhinavk0220 wants to merge 1 commit into
mesa:mainfrom
abhinavk0220:fix/emperor-dilemma-imports-and-random
Open

fix(emperor-dilemma): replace relative imports and fix unseeded random calls#463
abhinavk0220 wants to merge 1 commit into
mesa:mainfrom
abhinavk0220:fix/emperor-dilemma-imports-and-random

Conversation

@abhinavk0220

Copy link
Copy Markdown

Summary

Fixes two bugs preventing the Emperor's Dilemma example from running, as reported in #337.

Bug 1 — Relative imports break Solara module resolution

app.py used from .model import EmperorModel and model.py used from .agents import EmperorAgent. Solara loads example modules in a flat namespace and cannot resolve relative imports. Fixed by switching to absolute imports (from emperor_dilemma.model import ...), consistent with how other examples in this repo are structured (e.g. bank_reserves).

Bug 2 — Module-level random bypasses the seeded RNG

model.py imported the stdlib random module and called random.sample() and random.uniform() during agent initialisation. This means the rng parameter passed to EmperorModel() — and exposed as a slider in the UI — had no effect on the initial believer placement or conviction values, making runs non-reproducible. Fixed by replacing both calls with self.random.sample() and self.random.uniform().

Test plan

  • solara run app.py starts without ImportError
  • Agents render on the grid with correct colors (comply/enforce states)
  • Changing the Random Seed slider and clicking Reset produces a different initial configuration
  • Line plot shows Compliance / Enforcement / False Enforcement over time

🤖 Generated with Claude Code

…m calls

- Replace relative imports (from .model / from .agents) with absolute
  imports (from emperor_dilemma.model / from emperor_dilemma.agents)
  so Solara can resolve modules correctly when running app.py
- Remove top-level `import random` in model.py; replace random.sample()
  and random.uniform() with self.random equivalents so the RNG seed
  passed via the rng parameter is actually honoured

Fixes mesa#337

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sadrasabouri

Copy link
Copy Markdown

Hey @abhinavk0220
I verify that the issue you're trying to solve is still there (Python 3.12.13 and Mesa 4.0.0a0). I reviewed your PR, and here are some comments that can enhance it:

  • Currently, in examples/emperor_dilemma/README.md we have solara run app.py as the run command. This implicitly assumes that the user is running the example when the working directory is examples/emperor_dilemma. Therefore, your fix will result in ModuleNotFoundError: No module named 'emperor_dilemma'. My suggestion is to move the agents.py and model.py into an insider directory emperor_dilemma (similar to bank_reserves).
  • nit: make atomic-scoped PRs. The two bugs being solved by this PR do not live in the same scope and could be two separate valid PR.

The rest looks good to me.

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