-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add counterfactual regret minimization node and state
Summary: - Add CFR game state tree that uses a vec as an allocation arena. - Add a base cfr agent that can be filled out - Add a historian that will follow the game for an agent filling out the cfr tree. Test Plan: - Added a test for CFRState - Added doc tests for state
- Loading branch information
1 parent
3bdf181
commit 614fdc9
Showing
9 changed files
with
686 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::arena::{ | ||
action::{Action, AgentAction}, | ||
GameState, | ||
}; | ||
|
||
use super::{CFRState, TraversalState}; | ||
|
||
pub trait ActionGenerator { | ||
fn new(cfr_state: CFRState, traversal_state: TraversalState) -> Self; | ||
|
||
fn action_to_idx(&self, action: &Action) -> usize; | ||
|
||
fn gen_action(&self, game_state: &GameState) -> AgentAction; | ||
|
||
fn num_possible_actions(&self) -> usize; | ||
} | ||
|
||
pub struct CFRActionGenerator { | ||
cfr_state: CFRState, | ||
traversal_state: TraversalState, | ||
} | ||
|
||
impl ActionGenerator for CFRActionGenerator { | ||
fn action_to_idx(&self, _action: &Action) -> usize { | ||
todo!() | ||
} | ||
|
||
fn gen_action(&self, _game_state: &GameState) -> AgentAction { | ||
todo!() | ||
} | ||
|
||
fn new(cfr_state: CFRState, traversal_state: TraversalState) -> Self { | ||
CFRActionGenerator { | ||
cfr_state, | ||
traversal_state, | ||
} | ||
} | ||
|
||
fn num_possible_actions(&self) -> usize { | ||
// TODO: Implement this. It has to always be less | ||
// than 52 since we use the same children array | ||
// for all nodes including chance nodes. | ||
8 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::arena::{ | ||
action::{Action, AgentAction}, | ||
Agent, GameState, Historian, HistorianError, | ||
}; | ||
|
||
use super::{state::CFRState, state::TraversalState}; | ||
|
||
pub struct CFRAgent { | ||
pub traversal_state: TraversalState, | ||
pub cfr_state: CFRState, | ||
} | ||
|
||
pub struct CFRHistorian { | ||
pub current_traversal_state: TraversalState, | ||
pub cfr_state: CFRState, | ||
} | ||
|
||
impl CFRHistorian { | ||
pub fn new(traversal_state: TraversalState, cfr_state: CFRState) -> Self { | ||
CFRHistorian { | ||
current_traversal_state: traversal_state, | ||
cfr_state, | ||
} | ||
} | ||
} | ||
|
||
impl Historian for CFRHistorian { | ||
fn record_action( | ||
&mut self, | ||
_id: &uuid::Uuid, | ||
_game_state: &GameState, | ||
action: Action, | ||
) -> Result<(), HistorianError> { | ||
match action { | ||
// These are all assumed from game start and encoded in the root node. | ||
Action::GameStart(_) | Action::ForcedBet(_) | Action::PlayerSit(_) => Ok(()), | ||
// We don't encode round advance in the tree because it never changes the outcome. | ||
Action::RoundAdvance(_) => Ok(()), | ||
// Rather than use award since it can be for a side pot we use the final award ammount | ||
// in the terminal node. | ||
Action::Award(_) => Ok(()), | ||
Action::DealStartingHand(_deal_starting_hand_payload) => todo!(), | ||
Action::PlayedAction(_played_action_payload) => todo!(), | ||
Action::FailedAction(_failed_action_payload) => todo!(), | ||
Action::DealCommunity(_card) => todo!(), | ||
} | ||
} | ||
} | ||
|
||
impl CFRAgent { | ||
pub fn new( | ||
cfr_state: CFRState, | ||
node_idx: usize, | ||
chosen_child: usize, | ||
player_idx: usize, | ||
) -> Self { | ||
CFRAgent { | ||
cfr_state, | ||
traversal_state: TraversalState::new(node_idx, chosen_child, player_idx), | ||
} | ||
} | ||
|
||
pub fn historian(&self) -> CFRHistorian { | ||
CFRHistorian::new(self.traversal_state.clone(), self.cfr_state.clone()) | ||
} | ||
} | ||
|
||
impl Agent for CFRAgent { | ||
fn act( | ||
&mut self, | ||
_id: &uuid::Uuid, | ||
_game_state: &GameState, | ||
) -> crate::arena::action::AgentAction { | ||
AgentAction::Fold | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::arena::game_state; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_create_agent() { | ||
let game_state = game_state::GameState::new_starting(vec![100.0; 3], 10.0, 5.0, 0.0, 0); | ||
let cfr_state = CFRState::new(game_state); | ||
let _ = CFRAgent::new( | ||
cfr_state.clone(), | ||
// we are still at root so 0 | ||
0, | ||
0, | ||
0, | ||
); | ||
} | ||
} |
Oops, something went wrong.