Commit f480d55
authored
[BUG]: Validate
#### Reference Issues/PRs
Fixes #358
#### What does this implement/fix? Explain your changes.
1. Expanded constructor input validation in `MCTS.__init__`:
* Raises `ValueError` when:- **
a. `n_iterations < 1`.
b. `depth < 1`.
c. `states` is empty.
d. `states` contains duplicates.
4. Added focused regression tests in `test_mcts.py`:
* Verifies invalid:-
a. `n_iterations` values (`0`, `-1`) are rejected.
b. `depth` values (`0`, `-1`) are rejected.
c. `states` inputs (empty and duplicate entries) are rejected.
```python
def __init__(self, ...) -> None:
if depth < 1:
raise ValueError(f"`depth` must be >= 1, got {depth}.")
if n_iterations < 1:
raise ValueError(f"`n_iterations` must be >= 1, got {n_iterations}.")
if states is None:
states = ["A_", "C_", "G_", "U_", "_A", "_C", "_G", "_U"]
elif not states:
raise ValueError("`states` must contain at least one entry.")
elif len(states) != len(set(states)):
raise ValueError("`states` must contain unique entries.")
```
#### What should a reviewer concentrate their feedback on?
- [ ] Please focus on whether validating `depth`, `n_iterations`, and
`states` in `MCTS.__init__` is the right and consistent fix for
preventing invalid runtime behavior (including the infinite-loop path in
`run()` for `n_iterations <= 0`).
- [ ] Please check whether `ValueError` is the correct API behavior for
these invalid constructor inputs.
- [ ] Please check that the added regression tests are focused and cover
the relevant invalid cases without over-scoping.
#### Did you add any tests for the change?
yes
```python
@pytest.mark.parametrize("depth", [0, -1])
def test_init_invalid_depth(self, depth):
...
@pytest.mark.parametrize("n_iterations", [0, -1])
def test_init_invalid_iterations(self, n_iterations):
...
def test_init_empty_states(self):
...
def test_init_duplicate_states(self):
...
```
#### PR checklist
- [x] The PR title starts with either [ENH], [MNT], [DOC], or [BUG].
[BUG]
- [x] Added/modified tests
- [x] Used pre-commit hooks when committing to ensure that code is
compliant with hooks.
py-test
<img width="1912" height="165" alt="image"
src="https://github.com/user-attachments/assets/a396765d-8f98-40f4-b108-73b39be7391d"
/>
notebooks diff
<img width="1919" height="121" alt="Screenshot 2026-04-11 004844"
src="https://github.com/user-attachments/assets/541bf011-285f-4319-8389-634def1ac032"
/>
pre-commit
<img width="1907" height="199" alt="image"
src="https://github.com/user-attachments/assets/41140756-df9d-4bd9-a7cc-5c7e8b5281e3"
/>
run-jupyter-notebooks
<img width="1914" height="300" alt="Image"
src="https://github.com/user-attachments/assets/c92528ec-5a7b-4144-a155-77c90effe95b"
/>n_iterations in MCTS (#361)1 parent f528b38 commit f480d55
2 files changed
Lines changed: 48 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
| 28 | + | |
28 | 29 | | |
29 | | - | |
| 30 | + | |
| 31 | + | |
30 | 32 | | |
31 | | - | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| |||
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
77 | 99 | | |
78 | 100 | | |
79 | 101 | | |
80 | 102 | | |
81 | 103 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | 104 | | |
86 | 105 | | |
87 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
244 | 267 | | |
245 | 268 | | |
246 | 269 | | |
| |||
0 commit comments