-
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 a new single table tournament
Summary: Add a single table tournament and a builder struct for it. The goal is to be able to see how agents compete against each other from different positions and stack sizes. Test Plan: Added an example binary that runs the code. cargo clippy
- Loading branch information
1 parent
67e11c6
commit 0a9b55e
Showing
12 changed files
with
278 additions
and
11 deletions.
There are no files selected for viewing
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,28 @@ | ||
use std::vec; | ||
|
||
use rs_poker::arena::{ | ||
agent::{CallingAgentBuilder, RandomAgentBuilder}, | ||
tournament, AgentBuilder, | ||
}; | ||
|
||
fn main() { | ||
let stacks = vec![100.0, 100.0, 50.0]; | ||
|
||
let agent_builders: Vec<Box<dyn AgentBuilder>> = vec![ | ||
Box::new(CallingAgentBuilder), | ||
Box::<RandomAgentBuilder>::default(), | ||
Box::<RandomAgentBuilder>::default(), | ||
]; | ||
|
||
let game_state = rs_poker::arena::game_state::GameState::new(stacks, 10.0, 5.0, 0.0, 0); | ||
|
||
let tournament = tournament::SingleTableTournamentBuilder::default() | ||
.agent_builders(agent_builders) | ||
.starting_game_state(game_state) | ||
.build() | ||
.unwrap(); | ||
|
||
let results = tournament.run().unwrap(); | ||
|
||
println!("Agent Results: {:?}", results); | ||
} |
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
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
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
File renamed without changes.
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,14 @@ | ||
use super::Historian; | ||
|
||
pub struct NullHistorian; | ||
|
||
impl Historian for NullHistorian { | ||
fn record_action( | ||
&mut self, | ||
_id: &uuid::Uuid, | ||
_game_state: &crate::arena::GameState, | ||
_action: crate::arena::action::Action, | ||
) -> Result<(), super::HistorianError> { | ||
Ok(()) | ||
} | ||
} |
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,160 @@ | ||
use tracing::{event, trace_span}; | ||
|
||
use super::{errors::HoldemSimulationError, historian::HistorianBuilder, AgentBuilder, GameState}; | ||
|
||
/// A `SingleTableTournament` is a tournament that has multiple agents | ||
/// playing holdem poker at a single table. The tournament is played | ||
/// until a single agent has all the money. | ||
/// | ||
/// This builder is used to create a `SingleTableTournament`. | ||
#[derive(Default)] | ||
pub struct SingleTableTournamentBuilder { | ||
agent_builders: Option<Vec<Box<dyn AgentBuilder>>>, | ||
historian_builders: Option<Vec<Box<dyn HistorianBuilder>>>, | ||
starting_game_state: Option<GameState>, | ||
panic_on_historian_error: bool, | ||
} | ||
|
||
pub struct SingleTableTournament { | ||
agent_builders: Vec<Box<dyn AgentBuilder>>, | ||
historian_builders: Vec<Box<dyn HistorianBuilder>>, | ||
starting_game_state: GameState, | ||
panic_on_historian_error: bool, | ||
// TODO should this include payouts? | ||
} | ||
|
||
impl SingleTableTournamentBuilder { | ||
pub fn agent_builders(mut self, agent_builders: Vec<Box<dyn AgentBuilder>>) -> Self { | ||
self.agent_builders = Some(agent_builders); | ||
self | ||
} | ||
|
||
pub fn historian_builders( | ||
mut self, | ||
historian_builders: Vec<Box<dyn HistorianBuilder>>, | ||
) -> Self { | ||
self.historian_builders = Some(historian_builders); | ||
self | ||
} | ||
|
||
pub fn starting_game_state(mut self, starting_game_state: GameState) -> Self { | ||
self.starting_game_state = Some(starting_game_state); | ||
self | ||
} | ||
|
||
pub fn panic_on_historian_error(mut self, panic_on_historian_error: bool) -> Self { | ||
self.panic_on_historian_error = panic_on_historian_error; | ||
self | ||
} | ||
|
||
pub fn build(self) -> Result<SingleTableTournament, HoldemSimulationError> { | ||
let agent_builders = self | ||
.agent_builders | ||
.ok_or(HoldemSimulationError::NeedAgents)?; | ||
let starting_game_state = self | ||
.starting_game_state | ||
.ok_or(HoldemSimulationError::NeedGameState)?; | ||
let historian_builders = self.historian_builders.unwrap_or_default(); | ||
Ok(SingleTableTournament { | ||
agent_builders, | ||
historian_builders, | ||
starting_game_state, | ||
panic_on_historian_error: self.panic_on_historian_error, | ||
}) | ||
} | ||
} | ||
|
||
impl SingleTableTournament { | ||
// Returns a vector of the place that each agent finished in. | ||
pub fn run(&self) -> Result<Vec<usize>, HoldemSimulationError> { | ||
let span = trace_span!("SingleTableTournament::run"); | ||
let _enter = span.enter(); | ||
|
||
let mut place = self.agent_builders.len(); | ||
let mut results = vec![0; self.agent_builders.len()]; | ||
let mut game_state = self.starting_game_state.clone(); | ||
while place > 1 { | ||
let agents = self | ||
.agent_builders | ||
.iter() | ||
.map(|builder| builder.build(&game_state)) | ||
.collect::<Vec<_>>(); | ||
let historians = self | ||
.historian_builders | ||
.iter() | ||
.map(|builder| builder.build(&game_state)) | ||
.collect::<Vec<_>>(); | ||
let mut sim = crate::arena::HoldemSimulationBuilder::default() | ||
.game_state(game_state.clone()) | ||
.agents(agents) | ||
.historians(historians) | ||
.panic_on_historian_error(self.panic_on_historian_error) | ||
.build()?; | ||
|
||
// Run the simulation | ||
sim.run(); | ||
|
||
// Update the results | ||
let mut out = sim | ||
.game_state | ||
.stacks | ||
.iter() | ||
.enumerate() | ||
.filter(|(_, stack)| **stack == 0.0) | ||
.filter(|(idx, _)| sim.game_state.starting_stacks[*idx] != 0.0) | ||
.map(|(idx, _)| idx) | ||
.collect::<Vec<_>>(); | ||
|
||
// Sort by the starting stack going into the hand | ||
out.sort_by(|a, b| { | ||
sim.game_state.starting_stacks[*b] | ||
.partial_cmp(&sim.game_state.starting_stacks[*a]) | ||
.unwrap() | ||
.reverse() | ||
}); | ||
for idx in out { | ||
event!( | ||
tracing::Level::INFO, | ||
"Agent {} finished in place {}", | ||
idx, | ||
place | ||
); | ||
results[idx] = place; | ||
place -= 1; | ||
} | ||
let mut dealer_idx = (sim.game_state.dealer_idx + 1) % sim.game_state.stacks.len(); | ||
while sim.game_state.stacks[dealer_idx] == 0.0 { | ||
dealer_idx = (dealer_idx + 1) % sim.game_state.stacks.len(); | ||
} | ||
|
||
game_state = GameState::new( | ||
sim.game_state.stacks, | ||
sim.game_state.big_blind, | ||
sim.game_state.small_blind, | ||
sim.game_state.ante, | ||
dealer_idx, | ||
); | ||
} | ||
|
||
if place == 1 { | ||
let winners: Vec<usize> = game_state | ||
.stacks | ||
.iter() | ||
.enumerate() | ||
.filter(|(_, stack)| **stack > 0.0) | ||
.map(|(idx, _)| idx) | ||
.collect(); | ||
|
||
if winners.len() != 1 { | ||
return Err(HoldemSimulationError::NoWinner); | ||
} | ||
|
||
let idx = winners[0]; | ||
|
||
results[idx] = 1; | ||
println!("Agent {} finished in place 1", idx); | ||
event!(tracing::Level::INFO, "Agent {} finished in place 1", idx); | ||
} | ||
Ok(results) | ||
} | ||
} |