Skip to content

refactor(discrete_space): name and document the random probe budget (#3999) - #4020

Merged
quaquel merged 5 commits into
mesa:mainfrom
dietcoke-17:phase-2
Nov 7, 2026
Merged

quaquel merged 5 commits into
mesa:mainfrom
dietcoke-17:phase-2

Conversation

@dietcoke-17

Copy link
Copy Markdown
Contributor

Summary

Name the random-probe budget used by Grid.select_random_empty_cell and Grid.select_random_cell_with_capacity (_MAX_RANDOM_PROBES = 50), replace the stale FIXME with the reasoning behind it, and add tests that pin down the behaviour. No behaviour change: the budget stays at 50.

Motive

Phase 2 of #3626 (proposal in #3999). The budget was a magic number duplicated in two methods, with an outdated FIXME. After benchmarking alternatives, I don't think a different fixed value is justified yet, so this PR records why 50 is a safe choice and guards against regressions.

Implementation

  • mesa/discrete_space/grid.py: new class attribute Grid._MAX_RANDOM_PROBES = 50, used by both methods. The comment explains that all probes miss with probability (1 - p)^50, about 0.5% at 10% empty cells and about 1e-5 at 20%, so grids that aren't nearly full practically never reach the O(n) fallback. It also notes that smaller budgets make the scan fire too often at p >= 10% and should be re-evaluated with the benchmark script first.
  • tests/discrete_space/test_discrete_space.py:
    • a half-empty grid never reaches the scan over 500 calls (guards the Schelling-like regime);
    • a budget of 0 goes straight to the deterministic fallback.

Additional Notes

Findings (local, noisy; please confirm on CI):

  • One fallback scan costs about 10-12 random probes for grids of 400-40,000 cells, and about 29 at 90,000.
  • Small budgets hurt at p >= 10% on larger grids: at 90,000 cells and 10% empty, 15 probes took about 56.7 us per call versus about 11.2 us for 50, and 25 probes about 27.5 us.
  • Small budgets look faster on nearly full grids, but my timings there were too noisy to quote a number.

A budget that scales with grid size remains possible, but needs multi-machine benchmark data before it is worth the extra complexity. This PR builds on the flatnonzero fallback PR for #3626 and should be rebased once that merges.

Refs #3999
Refs #3626
closes #3999

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-29T10:37:12.658869Z
We couldn’t complete this review. Comment @codex review to try again.
857406d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added the testing Release notes label label Oct 29, 2026

@quaquel quaquel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do I understand this PR correctly in that all it does it move the implicit 50 tries into an explicit global variable?

@dietcoke-17

Copy link
Copy Markdown
Contributor Author

Do I understand this PR correctly in that all it does it move the implicit 50 tries into an explicit global variable?

Yes, that's right. Behaviour is unchanged: the budget stays at 50 and the probe loop is identical.

  1. The hard-coded 50 (duplicated in select_random_empty_cell and select_random_cell_with_capacity) becomes a private class attribute, Grid._MAX_RANDOM_PROBES, rather than a module-level global.
  2. The outdated FIXME is replaced with the reasoning for 50: at 10% empty cells all probes miss ~0.5% of the time, at 20% ~1e-5, so models like Schelling practically never hit the O(n) fallback.
  3. Two tests: a half-empty grid never reaches the fallback (a guard against regressions like the one that got Optimize near-full Grid empty-cell selection #3619 closed), and the budget is honoured.
    In my local runs, smaller budgets help on nearly full grids but make the scan fire far too often at >=10% empty (e.g. 90k cells, 10% empty: 15 probes was ~5x slower than 50), and the near-full gains were too noisy on my machine to quote. So this PR records that outcome rather than changing behaviour.

@quaquel quaquel added ignore-for-release PRs that aren't included in the release notes and removed testing Release notes label labels Nov 7, 2026
@quaquel
quaquel merged commit 9532420 into mesa:main Nov 7, 2026
17 checks passed
@github-actions github-actions Bot added the testing Release notes label label Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ignore-for-release PRs that aren't included in the release notes testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal for #3626: RNG-preserving fallback for select_random_empty_cell, then a benchmark-derived probe budget

2 participants