Skip to content

Agent that plays moves from database - #358

Merged
jonbinney merged 1 commit into
mainfrom
jdb/db-agent
Mar 16, 2026
Merged

Agent that plays moves from database#358
jonbinney merged 1 commit into
mainfrom
jdb/db-agent

Conversation

@jonbinney

@jonbinney jonbinney commented Mar 13, 2026

Copy link
Copy Markdown
Owner

The agent is implemented in python, and calls into rust to read the sqlite database that contains the policy.

How to Use

You can try this out quickly by compiling the rust code, then running create_policy_db, then running play.py to play against the PolicyDBAgent using the new policy database. These commands run in a few seconds each on my laptop. Run these commands from the deep_rabbit_hole directory.

Build the Rust Binary

(cd deep_quoridor/rust && cargo build --bin create_policy_db --release --all-features)

Create the Database

/usr/bin/time ./deep_quoridor/rust/target/release/create_policy_db --board-size 3 --max-walls 1 --max-steps 16 --num-threads 3 \
 --output policydb_3116.sqlite

Play against the agent

python deep_quoridor/src/play.py \
	-N 3 -W 1 -mx 16 \
	-t 1 \
	-i 34 \
	-r computationtimes arenaresults3 eloresults progressbar pygame \
	--num-workers=0 \
	-p \
	human \
	policydb:nick=policydb_a,db_path=policydb_3116.sqlite

@adamantivm

Copy link
Copy Markdown
Collaborator

LGTM.
What did you do with the database you already traiend?
Perpahs for a next pass we could try to figure out a way to store and share the DB, if it's worth it? Maybe on a cloud DB somewhre?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new “policy DB” agent that selects actions by querying a precomputed SQLite policy database via the existing Rust Python extension (quoridor_rs), and registers it so it can be invoked from the CLI/arena.

Changes:

  • Introduce PolicyDBAgent (+ params) that reconstructs game state from observations and calls quoridor_rs.policy_db_lookup to score actions.
  • Add policydb to AgentRegistry so it’s selectable via encoded agent names (e.g., policydb:db_path=...).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
deep_quoridor/src/agents/policy_db.py New agent implementation that queries Rust for action values from a SQLite policy DB and converts the chosen action into an env action index.
deep_quoridor/src/agents/__init__.py Registers the new agent type (policydb) in the agent registry.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +101 to +104
action_idx = self.action_encoder.action_to_index(WallAction((r, c), WallOrientation.HORIZONTAL))

if action_mask[action_idx]:
return action_idx
Comment on lines +56 to +71
def get_action(self, observation) -> int:
action_mask = observation["action_mask"]
obs = observation["observation"]

game, _, _ = construct_game_from_observation(obs)

grid = game.board._grid
player_positions = np.zeros((2, 2), dtype=np.int32)
player_positions[0] = game.board.get_player_position(Player.ONE)
player_positions[1] = game.board.get_player_position(Player.TWO)
walls_remaining = np.zeros(2, dtype=np.int32)
walls_remaining[0] = game.board.get_walls_remaining(Player.ONE)
walls_remaining[1] = game.board.get_walls_remaining(Player.TWO)
current_player = int(game.get_current_player())
completed_steps = game.completed_steps

@jonbinney

Copy link
Copy Markdown
Owner Author

The current DB for 5x5 with 3 walls is about 100GB. I thought about putting a DB for a smaller board in the repo or in the cloud, but I'm in the middle of changing the format so I left it out for now. We really only need a key value store, not a full sqlite database, so I've been looking for something that gives faster batch writes and has good compression. I tried parquet but it isn't good for the random access lookups that we need. I tried RocksDB, but it has annoying dependencies - you have to install c headers separately from the rust dependency system. Now I'm trying RedB, which is a key/value store like lmdb, but written in rust.

In any case it's super fast to generate a DB for a tiny board now, and following the command in this PR description will give a DB that is always in the most up to date format.

@jonbinney
jonbinney merged commit e3678a6 into main Mar 16, 2026
6 checks passed
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.

3 participants