Agent that plays moves from database - #358
Conversation
|
LGTM. |
There was a problem hiding this comment.
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 callsquoridor_rs.policy_db_lookupto score actions. - Add
policydbtoAgentRegistryso 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.
| action_idx = self.action_encoder.action_to_index(WallAction((r, c), WallOrientation.HORIZONTAL)) | ||
|
|
||
| if action_mask[action_idx]: | ||
| return action_idx |
| 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 | ||
|
|
|
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. |
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 runningplay.pyto play against the PolicyDBAgent using the new policy database. These commands run in a few seconds each on my laptop. Run these commands from thedeep_rabbit_holedirectory.Build the Rust Binary
Create the Database
Play against the agent