mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Weighted sampling without replacement accepts nonfinite weights

Open SahilKumar75 opened this issue 2 months ago • 0 comments

Describe the bug

AgentSet.select_random() accepts NaN and positive infinity when sampling without replacement. These values do not define valid probabilities. NaN can remain in the generated sampling keys and affect which agent is selected, while positive infinity produces a key of one. The same inputs are rejected by the standard library path used when replacement is enabled.

Expected behavior

Weighted sampling should reject every nonfinite weight before selecting either sampling algorithm.

To Reproduce

Create two agents and call select_random(1, weights=[float("nan"), 1.0], replace=False). The call returns a sample instead of raising a validation error. Replacing NaN with positive infinity has the same problem.

This was reproduced against current main at commit a5dfcd6ad2bb7863a00d8735649d4c3ebaf014f8.

Additional context

A focused validation before the existing negative weight check makes replacement and nonreplacement behavior consistent. Regression coverage should include both NaN and positive infinity.

SahilKumar75 avatar Sep 10 '26 06:09 SahilKumar75