A tournament framework for Connect Four decision policies, featuring a time-bounded Monte Carlo Tree Search agent with UCB1 and tactical safety checks.
Connect Four is small enough to reason about, but large enough to expose the trade-off between planning depth, exploration and strict decision-time budgets. This repository provides a reusable environment for loading policies dynamically, running head-to-head matches and recording every game as JSON.
groups/Final/policy.py implements the Head agent:
- Immediate tactics: take a winning move when available.
- One-step safety: avoid actions that allow an immediate opponent win whenever a safe alternative exists.
- Selection: traverse expanded nodes using UCB1.
- Expansion: add one unexplored legal action.
- Simulation: finish the game with random rollouts.
- Backpropagation: update visits and alternating-player rewards.
- Final decision: return the most visited action, breaking ties by mean value.
| Parameter | Default | Purpose |
|---|---|---|
| Exploration constant | sqrt(2) |
Balance exploitation and exploration |
| Per-turn budget | 2.0 s |
Normal planning time per action |
| Global budget | 58.0 s |
Maximum accumulated search time |
| Minimum reserve | 0.05 s |
Preserve enough time to return a legal action |
.
|-- main.py # Discovers policies and starts a tournament
|-- tournament.py # Match execution and JSON result storage
|-- connect4/
| |-- connect_state.py # Board state, transitions and winner detection
| |-- environment_state.py
| |-- policy.py # Policy interface
| `-- utils.py # Dynamic policy discovery
`-- groups/
|-- Final/policy.py # MCTS + tactical safeguards
`-- Random/policy.py # Random baseline
git clone https://github.com/rafaelsava/Connect4RL.git
cd Connect4RL
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python main.pyOn Windows PowerShell, activate with .\.venv\Scripts\Activate.ps1.
The runner discovers every class derived from connect4.policy.Policy under groups/, builds the bracket and prints the champion. Match histories are written to versus/ as JSON.
- Create
groups/<PolicyName>/policy.py. - Implement a class derived from
Policy. - Provide
mount(action_timeout=None)andact(board). - Return a legal column index from
0to6. - Run
python main.py; discovery is automatic.
The board uses 0 for empty cells, -1 for red and 1 for yellow.
- Rollouts and bracket order use randomness, so repeated tournaments can differ.
- The current implementation does not expose a global random seed.
- Runtime depends on the number of policies, game duration and configured action limits.
- Tournament results should be reported across multiple runs rather than from a single bracket.
- Seeded experiment runs and aggregate win-rate reporting.
- First-Visit Monte Carlo and TBOPI baselines under the same interface.
- Unit tests for terminal-state detection and tactical filters.
- Parallel tournament execution and performance profiling.
- Rafael Salcedo: created the initial Connect Four environment, policy interface, dynamic policy discovery and tournament runner; maintains the repository documentation.
- Fermin Escalona: developed and refined the MCTS policy variants, timing and safe-action logic, experiments and final policy organization.
This is a collaborative academic project. The commit history is the source of truth for individual code contributions.