Skip to content

fix: make q_learning choose_action doctest deterministic - #15459

Closed
ZainnQureshii wants to merge 1 commit into
TheAlgorithms:masterfrom
ZainnQureshii:fix-q-learning-doctest
Closed

ZainnQureshii wants to merge 1 commit into
TheAlgorithms:masterfrom
ZainnQureshii:fix-q-learning-doctest

Conversation

@ZainnQureshii

Copy link
Copy Markdown

Describe your change

The choose_action doctest in machine_learning/q_learning.py is flaky. It sets EPSILON = 0.0 inside the doctest, but doctest runs examples in a copy of the module globals, so choose_action() still reads the module's EPSILON = 0.2 and takes the random-exploration branch about one run in ten, printing 2 instead of 1.

Reproduced on master: repeated python3 -m doctest machine_learning/q_learning.py failed within the first 5 runs (Expected: 1, Got: 2). CI runs pytest with --iterations=8, so this hits PRs at random.

The fix patches random.random inside the doctest (return_value=0.99, so 0.99 < EPSILON is false and the greedy branch is always taken), the same unittest.mock.patch pattern used in other/fischer_yates_shuffle.py. Doctest-only change; the function body is untouched.

Verified: 200 consecutive python3 -m doctest machine_learning/q_learning.py runs pass, uvx pytest --doctest-modules machine_learning/q_learning.py passes, uvx ruff check and ruff format --check are clean.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

AI disclosure: prepared with AI assistance (Claude Code) and reviewed with GitHub Copilot, so I left the "all my own work" box unticked; nothing is copied from elsewhere.

🤖 Generated with Claude Code

The doctest assigned EPSILON = 0.0 in doctest's copy of the module globals, so choose_action() still read the module's EPSILON = 0.2 and explored at random about one run in ten. Patch random.random instead so the greedy branch is always taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Sep 28, 2026
@cclauss

cclauss commented Sep 30, 2026

Copy link
Copy Markdown
Member

Clearing all open pull requests to prepare for Hacktoberfest 2026 -- https://hacktoberfest.com

@cclauss cclauss closed this Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants