Skip to content

Commit

Permalink
feat: add counterfactual regret minimization node and state
Browse files Browse the repository at this point in the history
Summary:
- Add CFR game state tree that uses a vec as an allocation arena.

Test Plan:
- Added a test.
  • Loading branch information
elliottneilclark committed Dec 28, 2024
1 parent 60063d4 commit b130fb5
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 17 deletions.
125 changes: 111 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ arbitrary = { version = "~1.4.1", optional = true, features = ["derive"] }
tracing = { version = "~0.1.41", optional = true}
approx = { version = "~0.5.1", optional = true}
uuid = {version = "~1.11.0", optional = true, features = ["v7"]}
little-sorry = { version = "~0.5.0", optional = true}
ndarray = { version = "~0.16.1", optional = true}

[dev-dependencies]
criterion = "0.5.1"
Expand All @@ -33,7 +35,7 @@ approx = { version = "0.5.1"}
default = ["arena", "serde"]
uuid = ["dep:uuid"]
serde = ["dep:serde", "dep:serde_json", "uuid?/serde"]
arena = ["dep:tracing", "uuid"]
arena = ["dep:tracing", "dep:little-sorry", "dep:ndarray", "uuid"]
arena-test-util = ["arena", "dep:approx"]

[[bench]]
Expand Down
43 changes: 43 additions & 0 deletions src/arena/cfr/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
mod node;
mod state;

pub use node::{Node, NodeData, PlayerData, TerminalData};
pub use state::CFRState;

#[cfg(test)]
mod tests {
use std::vec;

use crate::arena::game_state::{Round, RoundData};

use crate::arena::GameState;
use crate::core::{Hand, PlayerBitSet};

#[test]
fn test_should_fold_all_in() {
let num_agents = 2;
let hand_zero = Hand::new_from_str("AsKsKcAcTh4d8d").unwrap();
let hand_one = Hand::new_from_str("JdTcKcAcTh4d8d").unwrap();

// The board is the last 5 cards of the hand
let board = hand_zero.iter().skip(2).copied().collect();
// Zero is all in.
let stacks: Vec<f32> = vec![0.0, 900.0];
let player_bet = vec![1000.0, 100.0];
// Create a game state where player 0 is all in and player 1 should make a
// decision to call or fold
let round_data = RoundData::new(num_agents, 100.0, PlayerBitSet::new(num_agents), 1);
let _game_state = GameState::new(
Round::River,
round_data,
board,
vec![hand_zero, hand_one],
stacks,
player_bet,
5.0,
0.0,
0.0,
0,
);
}
}
Loading

0 comments on commit b130fb5

Please sign in to comment.