当向量很大时,如何得到R中向量的所有可能分区的列表?

当向量很大时,如何得到R中向量的所有可能分区的列表?,r,vector,combinatorics,R,Vector,Combinatorics,我试图使用R来找到所有可能的方法来将长度为n的向量x划分为最多m个分区。当n很小时,我知道怎么做: library(partitions) x <- c(10, 20, 30, 40) n <- length(x) m <- 3 # In how many ways can we partition n objects into at most m patitions parts <- restrictedparts(n, m) sets <- setparts(

我试图使用R来找到所有可能的方法来将长度为
n
的向量
x
划分为最多
m
个分区。当
n
很小时,我知道怎么做:

library(partitions)
x <- c(10, 20, 30, 40)
n <- length(x)
m <- 3

# In how many ways can we partition n objects into at most m patitions
parts <- restrictedparts(n, m)
sets <- setparts(parts)
集合的每一列
告诉我,对于每个独特的排列,
x
中的每个项目都应该分配到哪个分区

n
较大时会出现问题:

n <- 15
m <- 4
parts <- restrictedparts(n, m)
# This expression will max out your CPU usage and eventually run out of memory.
sets <- setparts(parts)

n如果您想成批计算它们,那么至少在某些列中可以这样做。在像您这样的机器上,我无法完成
restrictedparts(15,4)
中几个单独列的计算。在第40列之前,我可以一次成功地成批处理5-10列,但在抛出malloc错误之前,有几个单独的列确实报告了许多列。所以你可能只需要一台更大的机器。在我的Mac上,构建第53列的32 GB内存消耗了一半的内存。大机器上的列数估计值与4GB机器上的报告一致:

> ncol( setparts( restrictedparts(15,4)[,53]))
[1] 6306300
R(317,0xa077a720) malloc: *** mmap(size=378380288) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

(对于这是否是一个合理的项目,我没有提供任何意见。)

如果您想分批计算它们,那么至少对某些列来说,这是可能的。在像您这样的机器上,我无法完成
restrictedparts(15,4)
中几个单独列的计算。在第40列之前,我可以一次成功地成批处理5-10列,但在抛出malloc错误之前,有几个单独的列确实报告了许多列。所以你可能只需要一台更大的机器。在我的Mac上,构建第53列的32 GB内存消耗了一半的内存。大机器上的列数估计值与4GB机器上的报告一致:

> ncol( setparts( restrictedparts(15,4)[,53]))
[1] 6306300
R(317,0xa077a720) malloc: *** mmap(size=378380288) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

(我对这是否是一个合理的项目没有任何意见。)

如果你和我一样不是组合数学的超级明星,但你相信
分区是正确的,那么至少你可以利用包的代码来计算分区的最终数量。在这里,我破解了
setparts
函数,因此它返回的不是分区本身,而是分区数:

num.partitions <- function (x) {
    if (length(x) == 1) {
        if (x < 1) {
            stop("if single value, x must be >= 1")
        }
        else if (x == 1) {
            out <- 1
        }
        else return(Recall(parts(x)))
    }
    if (is.matrix(x)) {
        out <- sum(apply(x, 2, num.partitions))
    }
    else {
        x   <- sort(x[x > 0], decreasing = TRUE)
        out <- factorial(sum(x))/(prod(c(factorial(x), 
                                         factorial(table(x)))))
    }
    return(out)
}
num.partitions(restrictedparts(4, 3))
# [1] 14
ncol(setparts(restrictedparts(4, 3)))
# [1] 14

num.partitions(restrictedparts(8, 4))
# [1] 2795
ncol(setparts(restrictedparts(8, 4)))
# [1] 2795
现在让我们看一下您的大型案例:

num.partitions(restrictedparts(15, 4))
# [1] 44747435
这确实是相当多的分区。。。无论
setparts
写得有多好,输出都不能放在单个数组中:

sets <- matrix(1, 15, 44747435)
# Error in matrix(1, 15, 44747435) : 
#  cannot allocate vector of length 671211525

set如果你像我一样不是组合数学的超级明星,但你相信
分区
是正确的,那么至少你可以利用包的代码来计算分区的最终数量。在这里,我破解了
setparts
函数,因此它返回的不是分区本身,而是分区数:

num.partitions <- function (x) {
    if (length(x) == 1) {
        if (x < 1) {
            stop("if single value, x must be >= 1")
        }
        else if (x == 1) {
            out <- 1
        }
        else return(Recall(parts(x)))
    }
    if (is.matrix(x)) {
        out <- sum(apply(x, 2, num.partitions))
    }
    else {
        x   <- sort(x[x > 0], decreasing = TRUE)
        out <- factorial(sum(x))/(prod(c(factorial(x), 
                                         factorial(table(x)))))
    }
    return(out)
}
num.partitions(restrictedparts(4, 3))
# [1] 14
ncol(setparts(restrictedparts(4, 3)))
# [1] 14

num.partitions(restrictedparts(8, 4))
# [1] 2795
ncol(setparts(restrictedparts(8, 4)))
# [1] 2795
现在让我们看一下您的大型案例:

num.partitions(restrictedparts(15, 4))
# [1] 44747435
这确实是相当多的分区。。。无论
setparts
写得有多好,输出都不能放在单个数组中:

sets <- matrix(1, 15, 44747435)
# Error in matrix(1, 15, 44747435) : 
#  cannot allocate vector of length 671211525

设置由于我无法安装分区包(缺少库),我想到了以下方法:

 ## Recursive function to get all partitions of a vector 
 ## Returns a list of logical vectors
 parts <- function(x) { 
   if (length(x) == 1) return(list(FALSE, TRUE))
   do.call(c, lapply(parts(x[-1]), function(y) list(c(FALSE, y), c(TRUE, y))))
 }
获取向量的所有分区的递归函数 ##返回逻辑向量的列表
零件由于我无法安装分区软件包(缺少库),我提出了以下建议:

 ## Recursive function to get all partitions of a vector 
 ## Returns a list of logical vectors
 parts <- function(x) { 
   if (length(x) == 1) return(list(FALSE, TRUE))
   do.call(c, lapply(parts(x[-1]), function(y) list(c(FALSE, y), c(TRUE, y))))
 }
获取向量的所有分区的递归函数 ##返回逻辑向量的列表
当你说至少进入
m
分区时,你的意思是最多?是的,谢谢你指出了错误。提示:
partition
库中的
nextpart
函数的描述可能是关键。上面编写的代码的最后一部分给出了我机器上的内存分配错误(OSX 10.8.2,R 2.15.1,分区1.9-12,4GB内存)。它对您的运行有效吗?我看到了与您相同的结果。我认为@flodel关于这个问题的上限的陈述是不正确的。如果您首先查看
受限部分(15,4)
,然后使用
选择(,。)
要查看每列中的数字,您可以看到组合爆炸。当我到达
ncol(setparts(restrictedparts(15,4)[,1:13])时,我已经超过了界限。
restrictedparts(15,4])中有54列
当你说进入至少
m
分区时,你的意思是最多?是的,我知道,谢谢你指出键入错误。提示:
partition
库中
nextpart
函数的描述可能是关键。上面编写的代码的最后一部分给出了我机器上的内存分配错误(OSX 10.8.2,R 2.15.1,分区1.9-12,4GB内存)。它对您的运行有效吗?我看到了与您相同的结果。我认为@flodel关于这个问题的上限的陈述是不正确的。如果您首先查看
受限部分(15,4)
,然后使用
选择(,。)
要查看每列中的数字,您可以看到组合爆炸。当我到达
ncol(setparts(restrictedparts(15,4)[,1:13])时,我已经超过了界限。
restrictedparts(15,4])中有54列
感谢您计算分区矩阵的大小–这是最有用的答案。我确信可以通过使用库(如
ff
)重写
setparts()
来强制执行此操作,但我将尝试解决我的问题(纸牌游戏中的预期点数分布)使用蒙特卡罗方法代替。感谢您计算分区矩阵的大小–这使它成为最有用的答案。我确信可以通过使用库(如
ff
)重写
setparts()
)来强制执行此操作,但我将尝试解决我的问题(纸牌游戏中的预期点数分布)用蒙特卡罗方法代替。