Skip to content

Commit 602a2a1

Browse files
Merge pull request #396 from sylvesterkaczmarek:fix/validate-sanctioner-alternating-steps
PiperOrigin-RevId: 969328287 Change-Id: Id4c48c5847bfdb4966a620d8e0a0b2fa6c28ad10
2 parents cf55ca6 + b7bdfb4 commit 602a2a1

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎meltingpot/utils/puppeteers/clean_up.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,10 @@ def __init__(
304304
self._threshold = threshold
305305
self._recency_window = recency_window
306306

307-
self._alternating_steps = alternating_steps
307+
if alternating_steps > 0:
308+
self._alternating_steps = alternating_steps
309+
else:
310+
raise ValueError('alternating_steps must be positive')
308311
self._nice = nice
309312

310313
self._steps_to_sanction_when_motivated = steps_to_sanction_when_motivated
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2026 DeepMind Technologies Limited.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Validation tests for SanctionerAlternator."""
15+
16+
from unittest import mock
17+
18+
from absl.testing import absltest
19+
from absl.testing import parameterized
20+
from meltingpot.utils.puppeteers import clean_up
21+
22+
23+
class SanctionerAlternatorValidationTest(parameterized.TestCase):
24+
25+
@parameterized.parameters(0, -1)
26+
def test_rejects_nonpositive_alternating_steps(self, alternating_steps):
27+
with self.assertRaisesRegex(ValueError, 'alternating_steps must be positive'):
28+
clean_up.SanctionerAlternator(
29+
cooperate_goal=mock.sentinel.cooperate,
30+
defect_goal=mock.sentinel.defect,
31+
sanction_goal=mock.sentinel.sanction,
32+
num_others_cooperating_cumulant='NUM_OTHERS_COOPERATING',
33+
threshold=1,
34+
alternating_steps=alternating_steps,
35+
)
36+
37+
38+
if __name__ == '__main__':
39+
absltest.main()

0 commit comments

Comments
 (0)