qlearn: qlearn

Description Usage Arguments Details Value Note Author(s) References Examples

Description

Input a game that has variables statevars (which the player can keep track of). The player can perform any of possibleactions. The output matrix will give the expected value of each action (column) in each state (row).

Usage

1
2
qlearn(game, statevars, possibleactions, playername="P1",
  numiter=1000, prevstrategy=NULL, ...)

Arguments

game

Name of the game to be played/learned.

statevars

A vector of the states to be monitored inside game. These are the conditions under which we the player has to make his decision.

possibleactions

A vector of the names of the possible actions inside game. This should be a list of every possible action that can be taken, regardless of state.

playername

The name of the variable that holds the name for the player's action inside game. See Details.

numiter

Number of iterations of game. Defaults to 50.

prevstrategy

Reward matrix returned by a previous qlearn function; serves as a starting point. Defaults to a blank reward matrix.

...

Additional arguments to be passed to game.

Details

At some point in game, there must be a line of the format

playername <- 'Choose'

where playername is substituted with the paramater "playername". This line should be at the point where the user wants to have the player choose an action. Since playername defaults to "P1", it sufficient to put the line:

P1 <- 'Choose'

somewhere in the function.

Value

A matrix describing the expected reward values of performing a certain action (columns) in a certain state (rows).

Note

Contact at liam.bressler@yale.edu

Author(s)

Liam Bressler

References

http://labressler.github.io/analytics

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cardgame <- function()
{
  playercards <- sample(1:8,4) #distribute the cards, we're player one
  ourcard <- playercards[1] #our card
  playertotals <- rep(-1,4) #including the antes
  playersinpot <- vector()
  for (player in 2:4) #other 3 players go first
  {
    if (playercards[player]>=2)
    {
      playertotals[player] <- (-3)
      playersinpot <- append(playersinpot,player)
    }
  }
  #the next line is where we want to choose our action
  player1 <- 'Choose'
  if (player1=="Call")
  {
    playertotals[1] <- (-3)
    playersinpot <- append(playersinpot,1)
  }
  potsize <- -1*(sum(playertotals)) #the amount in the pot is how much the players put in
  playercards[!(1:4 %in% playersinpot)] <- 0 #get rid of everyone who folded
  winner <- which.max(playercards) #winner is the person with the highest card who didn't fold
  playertotals[winner] <- playertotals[winner]+potsize
  return(playertotals[1]) #return how much we won
}

strat <- qlearn(game="cardgame",statevars="ourcard",possibleactions=c("Call","Fold"),
  playername="player1",numiter=25000) #make sure each function and variable name is a string

strat

labressler/QLearning documentation built on May 20, 2019, 7:32 p.m.