Skip to content

Make select() raise on invalid float at_most instead of silently ignoring it - #3853

Merged
quaquel merged 1 commit into
mesa:mainfrom
RudraDudhat2509:fix/select-at-most-invalid-float
Sep 15, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
RudraDudhat2509:fix/select-at-most-invalid-float

Conversation

@RudraDudhat2509

Copy link
Copy Markdown
Contributor

Fixes #3852

at_most <= 1.0 and isinstance(at_most, float) skips the fraction branch whenever the comparison is False, which is also what NaN does (every NaN comparison is False). So at_most=float('nan') fell through untouched, and the generator's count >= at_most check is then always False for NaN, so the loop never breaks and every agent gets selected instead of none. Negative floats and 0.0 had the same fallthrough problem, just landing on 0 selected instead of unlimited.

Fix validates the (0.0, 1.0] range up front and raises ValueError for anything outside it, matching how select_random's n parameter already validates the same range on its own float path.

Verified against the pre-fix commit: at_most=float('nan') returned 10/10 agents before the fix, raises ValueError after. Ran the full test_agentset.py, 39 passed. Added 4 cases (nan, 0.0, -0.5, 1.5) to the existing test_agentset test. ruff check + format clean.

…ring it

at_most <= 1.0 and isinstance(at_most, float) skips the fraction branch
whenever the comparison is False, which is also what happens for NaN
(all NaN comparisons are False). So at_most=float('nan') fell through
untouched and was later compared as count >= at_most in the generator,
which is always False for NaN, so the loop never breaks and every
agent gets selected instead of none. Negative floats and 0.0 had the
same silent-fallthrough problem, just landing on 0 selected instead of
unlimited.

Now the float path validates the (0.0, 1.0] range up front and raises
ValueError otherwise, matching how select_random already validates its
own n parameter.
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.6% [-1.4%, +0.1%] 🔵 -0.8% [-1.0%, -0.6%]
BoltzmannWealth large 🔵 +0.6% [-0.3%, +1.5%] 🔵 +1.7% [-1.3%, +4.2%]
Schelling small 🔵 +2.5% [+1.9%, +3.2%] 🔵 +1.4% [+0.6%, +2.2%]
Schelling large 🔵 -0.3% [-1.2%, +0.6%] 🔴 +9.9% [+7.1%, +12.7%]
WolfSheep small 🔵 +0.2% [-0.6%, +1.0%] 🔵 -1.1% [-4.4%, +2.2%]
WolfSheep large 🔵 +1.9% [+0.5%, +3.5%] 🔵 -0.2% [-2.5%, +2.2%]
SugarscapeG1mt small 🔵 +2.8% [+1.9%, +3.6%] 🔵 +2.1% [+0.3%, +4.0%]
SugarscapeG1mt large 🔵 +5.0% [+1.2%, +9.1%] 🔵 -0.0% [-2.3%, +2.6%]
BoidFlockers small 🔵 -2.9% [-3.8%, -2.0%] 🟢 -3.7% [-4.3%, -3.1%]
BoidFlockers large 🔵 +1.0% [-0.9%, +3.4%] 🔵 -2.8% [-3.3%, -2.4%]
MultiLevelAllianceModel small 🔵 -1.6% [-5.1%, +2.2%] 🔵 -0.4% [-0.8%, +0.1%]
MultiLevelAllianceModel large 🔵 -4.3% [-9.1%, +0.5%] 🔵 +1.6% [-0.7%, +4.4%]

@quaquel

quaquel commented Sep 15, 2026

Copy link
Copy Markdown
Member

It's a very implausible bug that is fixed here but it might crop up in rare cases. Fix seems fine.

@quaquel quaquel added the bug Release notes label label Sep 15, 2026
@quaquel
quaquel merged commit c267146 into mesa:main Sep 15, 2026
17 of 18 checks passed
quaquel pushed a commit that referenced this pull request Sep 15, 2026
CellCollection.select() had the same fraction check that #3853 fixed in
AbstractAgentSet.select(): `at_most <= 1.0 and isinstance(at_most, float)`.
With at_most=float("nan") the branch is skipped and the generator's
`count >= at_most` never becomes true, so every cell is selected. A float
above 1.0 such as 1.5 is used as a raw count (2 cells), and negative
floats or 0.0 silently select nothing.

Validate the (0.0, 1.0] range on the float path and raise ValueError
otherwise, matching AgentSet.select().
rishiiicreates added a commit to rishiiicreates/mesa that referenced this pull request Oct 3, 2026
Following mesa#3853 and mesa#3860, negative integers (which previously silently
returned an empty set) and booleans (which were treated as 0 or 1 because
isinstance(bool, int) is True) were not validated.

Now AbstractAgentSet.select() and CellCollection.select():
- Raise TypeError if at_most is a boolean or not int/float.
- Raise ValueError if at_most is an integer < 0.

Fixes mesa#3867
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
…ring it (#3853)

at_most <= 1.0 and isinstance(at_most, float) skips the fraction branch
whenever the comparison is False, which is also what happens for NaN
(all NaN comparisons are False). So at_most=float('nan') fell through
untouched and was later compared as count >= at_most in the generator,
which is always False for NaN, so the loop never breaks and every
agent gets selected instead of none. Negative floats and 0.0 had the
same silent-fallthrough problem, just landing on 0 selected instead of
unlimited.

Now the float path validates the (0.0, 1.0] range up front and raises
ValueError otherwise, matching how select_random already validates its
own n parameter.

(cherry picked from commit c267146)
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
CellCollection.select() had the same fraction check that #3853 fixed in
AbstractAgentSet.select(): `at_most <= 1.0 and isinstance(at_most, float)`.
With at_most=float("nan") the branch is skipped and the generator's
`count >= at_most` never becomes true, so every cell is selected. A float
above 1.0 such as 1.5 is used as a raw count (2 cells), and negative
floats or 0.0 silently select nothing.

Validate the (0.0, 1.0] range on the float path and raise ValueError
otherwise, matching AgentSet.select().

(cherry picked from commit 730b7fd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AgentSet.select(at_most=...) silently ignores NaN/negative/zero instead of raising

2 participants