Pokerena r emo::ji("club_suit") r emo::ji("diamond_suit") r emo::ji("spade_suit") r emo::ji("heart_suit")

knitr::opts_chunk$set(echo = T, warning = F, error = F, message = F, fig.path = "man/figures/", fig.align = "center", fig.height = 3, fig.width = 5)

A Poker engine for AI development in R.

# devtools::install_github("hadley/emo")
pacman::p_load(tidyverse, devtools, crayon)
load_all()
ggplot2::theme_set(theme_classic())

# keywords <- emo::ji_keyword
# keywords[stringr::str_detect(names(keywords), "suit")]

Tutorial

This tutorial leads you to start point of poker AI development...

... But first install.

devtools::install_github("systats/pokerena")

There are two main ways to deploy a bot, as there are two ways for using the poker backend.

  1. preregister functions in interactive R session
  2. call a separate HTTP API endpoint from a job.

In general players just return a tibble with an action and amount of chips to bet.

player_call <- function(state_, ...){
  tibble(chips = state_$to_call, action = "call")
}
players <- rbind(
  tibble(name = "caller", fun = list(player_call)), # player 1
  tibble(name = "radnom", fun = list(player_random)), # player 2
  tibble(name = "potman", fun = list(player_api)), # player 2
  tibble(name = "folder", fun = list(player_fold)) # player 4
) %>% mutate(credit = 100, bb = 2)
g <- game$new(players, delay = 0)
g$run()
g$session %>% glimpse
g$events %>% glimpse

Similarily a tournament can be set up.

config <- list(
  credit = 100, # inital stack size, if NULL assumes individual credits
  max_round = 50, # how many rounds at max should be played
  bb = 2, # inital big blind
  bb_round = 10, # increase blinds after x hands
  bb_slope = "linear" # how to increase blinds
)

tourn <- tournament$new(players, config)
tourn$run(T)
# saveRDS(tourn, file = "man/figures/readme_tourn.rds")
tourn <- readRDS("man/figures/readme_tourn.rds")
tourn$games %>% glimpse
tourn$games %>% 
  transmute(id = 1:n(), result) %>% 
  unnest("result") %>% 
  distinct(id, bb) %>% 
  ggplot(aes(id, bb)) +
  geom_line()
credit <- tourn$games %>% 
  transmute(id = 1:n(), result) %>% 
  unnest("result") %>% 
  ggplot(aes(id, credit, colour = name)) + 
  geom_line() +
  scale_colour_viridis_d()

net <- tourn$games %>% 
  transmute(id = 1:n(), result) %>% 
  unnest("result") %>% 
  ggplot(aes(id, net, fill = name)) + 
  geom_col(postion = ggplot2::position_dodge(), alpha = .7, show.legend = F) +
  scale_fill_viridis_d()

gridExtra::grid.arrange(net, credit, ncol = 2)
ranges <- tourn$games %>% 
  transmute(id = 1:n(), events) %>% 
  unnest("events") %>% 
  filter(hand != "" & action != "fold") %>% 
  distinct(game_id, name, hand) %>%
  mutate(hand = hand %>% stringr::str_replace_all("10", "T")) %>% 
  tidyr::separate(hand, into = c("card1", "card2"), sep = "\\s+") %>% 
  left_join(pockets_both()) %>% 
  count(name, value1, value2)

ranges %>% 
  ggplot(aes(value1, value2, fill = n)) +
  geom_tile(alpha = .7) +
  scale_fill_viridis_c() +
  facet_wrap(~name)
library(tidygraph)
library(ggraph)

pay_network <- tourn$games %>% 
  transmute(id = 1:n(), result) %>% 
  unnest("result") %>% 
  group_by(id) %>% 
  mutate(winner_name  = ifelse(winner == 1, name, NA)) %>% 
  fill(winner_name, .direction = "updown") %>% 
  ungroup() %>% 
  group_by(name, winner_name) %>% 
  summarise(net = sum(net)) %>% 
  as_tbl_graph()

pay_network %>% 
  ggraph(layout = 'linear') + #, circular = TRUE
  geom_edge_arc0(aes(colour = net), arrow = arrow(length = unit(4, 'mm')), width = 1, alpha = .8) +
  scale_edge_color_viridis(direction = -1) +
  geom_node_text(aes(label = name))

Main Dependencies

Unit Testing

testthat::test_package("pokerena")

Roadmap

Engine

AI

Analytics



systats/pokerena documentation built on Sept. 17, 2020, 2:35 p.m.