试图编写一个R脚本来消除控制策略(只是比较两个向量),结果出现了一些问题
所以我很快就会用R做一些工作,我需要学习如何使用它。我认为一个很好的练习是尝试编写一个程序,它采用一个回报矩阵,并对支配策略进行迭代消除(如果你不知道我在说什么,这是非常简单的博弈论内容,对问题并不重要)。第一步是编写一个函数,该函数接受一个矩阵,并返回所有占主导地位的策略的摘要,但出现了一些问题试图编写一个R脚本来消除控制策略(只是比较两个向量),结果出现了一些问题,r,R,所以我很快就会用R做一些工作,我需要学习如何使用它。我认为一个很好的练习是尝试编写一个程序,它采用一个回报矩阵,并对支配策略进行迭代消除(如果你不知道我在说什么,这是非常简单的博弈论内容,对问题并不重要)。第一步是编写一个函数,该函数接受一个矩阵,并返回所有占主导地位的策略的摘要,但出现了一些问题 strictdomlist <- function(m) { # takes a matrix, determines if row player has strictly #
strictdomlist <- function(m) {
# takes a matrix, determines if row player has strictly
# dominated strategies
strategies <- dim(m)[1]
dominatingstrategies <- list()
for (i in 1:strategies) {
dstrat <- 0
for (j in 1:strategies) {
if (i != j) {
if (all(m[i, ]<m[j, ])) dstrat <- c(dstrat,j)
}
}
dominatingstrategies[i] <- dstrat
}
return(dominatingstrategies)
}
我希望它能还给我:
[[1]]
[1] 2 4
[[2]]
[1] 4
[[3]]
[1] 1 2 4
[[4]]
[1] 0
但它给我的是:
> strictdomlist(m2)
[[1]]
[1] 4
[[2]]
[1] 4
[[3]]
[1] 4
[[4]]
[1] 0
Warning messages:
1: In dominatingstrategies[i] <- dstrat :
number of items to replace is not a multiple of replacement length
2: In dominatingstrategies[i] <- dstrat :
number of items to replace is not a multiple of replacement length
3: In dominatingstrategies[i] <- dstrat :
number of items to replace is not a multiple of replacement length
>strictdomlist(m2)
[[1]]
[1] 4
[[2]]
[1] 4
[[3]]
[1] 4
[[4]]
[1] 0
警告信息:
1:在支配策略[i]中,这是您的列表分配:
dominatingstrategies[i] <- dstrat
i、 例如,您在每个元素中得到一个0
,包括行数较大的元素
如果您不想这样做,可以将dstrat
初始化为dstrat这里有一个更为简单的解决方案:它创建一个矩阵,如果策略i严格受策略j支配,则其(i,j)项为真
k <- 3
set.seed(2)
m <- matrix(round(10*runif(k^2)),nc=k)
# [,1] [,2] [,3]
# [1,] 2 2 1
# [2,] 7 9 8
# [3,] 6 9 5
d <- matrix( mapply(
function(i,j) all(m[i,] < m[j,]),
row(m), col(m)
), nr=nrow(m) )
# [,1] [,2] [,3]
# [1,] FALSE TRUE TRUE
# [2,] FALSE FALSE FALSE
# [3,] FALSE FALSE FALSE
apply(d, 1, which)
k啊,谢谢你!我就知道会是这样的傻事。非常感谢。这些就是我需要从R开始思考的方式。
dominatingstrategies[[i]] <- dstrat
> strictdomlist(m)
[[1]]
[1] 0 2 4
[[2]]
[1] 0 4
[[3]]
[1] 0 1 2 4
[[4]]
[1] 0
if (length(dstrat)==0)
dominatingstrategies[[i]] <- 0
else
dominatingstrategies[[i]] <- dstrat
k <- 3
set.seed(2)
m <- matrix(round(10*runif(k^2)),nc=k)
# [,1] [,2] [,3]
# [1,] 2 2 1
# [2,] 7 9 8
# [3,] 6 9 5
d <- matrix( mapply(
function(i,j) all(m[i,] < m[j,]),
row(m), col(m)
), nr=nrow(m) )
# [,1] [,2] [,3]
# [1,] FALSE TRUE TRUE
# [2,] FALSE FALSE FALSE
# [3,] FALSE FALSE FALSE
apply(d, 1, which)