Skip to content

Fix WeightedRandomOpponent when rho_normalization is not given - #794

Merged
BDonnot merged 1 commit into
Grid2op:dev_1.12.7from
prawalpokharel:fix-weighted-opponent-default-norm
Nov 23, 2026
Merged

BDonnot merged 1 commit into
Grid2op:dev_1.12.7from
prawalpokharel:fix-weighted-opponent-default-norm

Conversation

@prawalpokharel

Copy link
Copy Markdown
added removed
Python (grid2op package) +1 -1
Tests (grid2op/tests) +29 -0
Docs + changelog + notebooks +2 -0
total +32 -1

What was wrong

rho_normalization is optional in WeightedRandomOpponent.init (it only warns that no normalization will be performed), but leaving it out breaks the opponent. The default is built with np.ones_like(lines_attacked), and lines_attacked is a list of line names, so the result is an array of strings (array(['1', '1', ...], dtype='<U...')). At the first attack, dividing rho by it raises:

TypeError: ufunc 'divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

The l2rpn_neurips_2020_track1 configs and the example in docs/user/opponent.rst all pass rho_normalization, so they are not affected.

What this PR does

The default becomes np.ones(len(lines_attacked)). Nothing changes when rho_normalization is given.

Tests

  • New test_WeightedRandomOpponent_without_rho_normalization in test_Opponent.py. It fails on dev_1.12.7 with the TypeError above and passes with the fix.
  • python -m unittest grid2op.tests.test_Opponent: 42 tests pass.
  • I did not run the full CI suite.

I ran into this while working on #793.

The default normalization was built with np.ones_like(lines_attacked).
On a list of line names that gives an array of strings, so the first
attack failed with a TypeError when dividing rho by it. Use an array of
ones of the right length instead, and add a test that attacks without
rho_normalization.

Signed-off-by: Prawal Pokharel <57414987+prawalpokharel@users.noreply.github.com>
Assisted-by: Claude Code (claude-opus-5-5)
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@BDonnot BDonnot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Perfect, thanks for the fix :-)

@BDonnot
BDonnot merged commit 3f27c01 into Grid2op:dev_1.12.7 Nov 23, 2026
19 of 21 checks passed
@prawalpokharel

Copy link
Copy Markdown
Author

Thank you so much, and I’m looking forward to contributing more 🚀🚀

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