在R地块的足球场上让球员抓住机会

时间:2017-05-03 05:40:35

标签: r plot sampling markov-chains

背景:

我试图想出一个编码方案,以便我的14个玩家中的任何一个(分开; 每个玩家在 下方显示为编号的圈子)如果.1的结果为outcome = sample(x = c(1, 2), size = 1),则我的足球场的中心粗线独立向左移动1(以x轴值为单位) outcome = sample(x = c(1, 2), size = 1)的结果为2,播放器向右移动.1(以x轴值为单位)。

因此,目标是每个玩家可以根据outcome的结果独立向左或向右移动。

编码问题(下面提供R代码):

我是否需要为每位玩家写outcome[i] = sample(x = c(1, 2), size = 1) 16次,然后构建如下内容:

outcome1 = sample(x = c(1, 2), size = 1)
outcome2 = sample(x = c(1, 2), size = 1)
#.
#.
#.

然后根据他的x

更改该玩家的outcome
if(outcome1 == 1) {  points(x - .1, y, cex = 4.5, lwd = 3, pch = 21, bg = 0)  
  } else { points(x + .1, y, cex = 4.5, lwd = 3, pch = 21, bg = 0)  }

还是有更好的方法?

enter image description here

enter image description here

这是我的R代码:

plot(1, ty = "n", ann = F, cex = 3)

par = par('usr')
rect(par[1], par[3], par[2], par[4], col = 'darkseagreen1' )

points( 1, 1, cex = 5, pch =20, col = 0)
points( 1, 1, cex = 33, lwd = 5, col = 0)
abline(v = 1, lwd = 10, col = 0)

rect(.6, .6, 1.4, 1.4, lwd = 5, border = 0)
rect(0, .85, .65, 1.15, lwd = 5, col = 'darkseagreen1', border = 0)
rect(1.35, .85, 1.45, 1.15, lwd = 5, col = 'darkseagreen1', border = 0)
box()

x = rep(1, 14); y = seq(.6, 1.4, len = 14)
points(x, y, cex = 4.5, lwd = 3, pch = 21, bg = 0)
text(x, y, 1:14, font = 2)

1 个答案:

答案 0 :(得分:1)

您可以先为每个玩家采样所有动作并将其存储在矩阵中(行是玩家,列是时间步长):

nSteps <- 16
nPlayers <- 14

## Sample movement of players:
xStepsMx <- matrix(sample(c(-1,1)*0.1, nPlayers*nSteps, replace = TRUE),
        nrow = nPlayers, ncol = nSteps)

然后,您可以在每个时间步骤评估每个玩家的位置:

## Position of players:
xPosMx <- t(sapply(1:nrow(xStepsMx), function(ii) cumsum(xStepsMx[ii,]))) + x 

对于1,2,...,16中的每个时间步timeStep,您可以例如使用

绘制球员的位置
timeStep <- 5    
points(xPosMx[,timeStep], y, cex = 4.5, lwd = 3, pch = 21, bg = "white")
text(xPosMx[,timeStep], y, 1:14, font = 2)