具有指定分布的简单对策在R上的Monte Carlo模拟

lndjwyie  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(89)

我试图找出如何创建一个蒙特卡洛模拟的游戏在R。
在这个游戏中,你有两个玩家,P1和P2。在每一轮中,一个玩家可以采取行动。P1采取行动的时间为60%。P2采取行动的时间为40%。
游戏中有3个动作:A、B和C。A得3分。P1执行A的可能性为20%,P2执行A的可能性为40%。B得1分。P1执行B的可能性为30%,P2执行B的可能性为50%。C得1分。P1执行C的可能性为50%,P2执行C的可能性为10%。
当一个玩家达到10分时游戏结束。
我想在1000场游戏结束时看到:

  1. P1和P2的获胜计数
    1.所有游戏中动作A、B和C的计数
    我已经能够编写代码来计算获胜的数量和所采取的行动的计数,如果:
  2. P1和P2采取行动的可能性相等(即50%)
    1.每个动作奖励相同的点数
    1.每一个动作都有相同的可能性(即,任何一个玩家的任何动作都有33%的可能性)
rkue9o1l

rkue9o1l1#

像这样?

draw <- function(players, actions, points, probs) {
  player <- sample(players$player, 1, prob = players$prob)
  s <- sample(actions, 1, prob = probs[[player]])
  list(player = player, action = s, points = points[[s]])
}
game <- function(players, actions, points, probs) {
  p <- c(P1 = 0L, P2 = 0L)
  total_actions <- c(A = 0L, B = 0L, C = 0L)
  while(all(p < 10L)) {
    d <- draw(players, actions, points, probs)
    p[d$player] <- p[d$player] + d$points
    total_actions[d$action] <- total_actions[d$action] + 1L
  }
  list(win = p >= 10L, actions = total_actions)
}
sim <- function(n, players, actions, points, probs) {
  r <- replicate(n, game(players, actions, points, probs), simplify = FALSE)
  wins <- do.call(rbind.data.frame, lapply(r, `[[`, "win"))
  tot_actions <- do.call(rbind.data.frame, lapply(r, `[[`, "actions"))
  names(wins) <- players$player
  names(tot_actions) <- actions
  cbind(wins, tot_actions)
}  

Players <- list(player = c("P1", "P2"), prob = c(0.6, 0.4))
Actions <- c("A", "B", "C")
Points <- c(A = 3, B = 1, C = 1)
Probs <- list(P1 = c(20, 30, 50)/100, P2 = c(40, 50, 10)/100)

# test run of one draw
draw(Players, Actions, Points, Probs)
#> $player
#> [1] "P2"
#> 
#> $action
#> [1] "B"
#> 
#> $points
#> [1] 1

# test run of one game
game(Players, Actions, Points, Probs)
#> $win
#>    P1    P2 
#>  TRUE FALSE 
#> 
#> $actions
#> A B C 
#> 2 6 6

# simulate 1000 games
set.seed(2023)
n <- 1000L
s <- sim(n, Players, Actions, Points, Probs)
# totals
colSums(s)
#>   P1   P2    A    B    C 
#>  594  406 2816 3854 3525

创建于2023年3月5日,使用reprex v2.0.2

注解

在问题末尾定义了参数的游戏可以用

  1. Players <- list(player = c("P1", "P2"), prob = c(0.5, 0.5))
  2. Points <- c(A = 1, B = 1, C = 1)
  3. Probs <- list(P1 = rep(1, 3)/3, P2 = rep(1, 3)/3)

相关问题