在R中查找带约束的值的组合

在R中查找带约束的值的组合,r,loops,recursion,constraints,tree-traversal,R,Loops,Recursion,Constraints,Tree Traversal,向量中有一组值,例如 all_points <- c(1, 4, 2, 12, 6, 5, 25) 目前,我正在尝试实现一个递归函数,该函数测试所有向右值的大小,并返回一个向量列表,但它不起作用。下面是部分R代码和部分伪代码来解释我的方法 my_recursive_function <- function(input_points, running_vector = c(1)){ start_point <- input_points[1] rightward

向量中有一组值,例如

all_points <- c(1, 4, 2, 12, 6, 5, 25)
目前,我正在尝试实现一个递归函数,该函数测试所有向右值的大小,并返回一个向量列表,但它不起作用。下面是部分R代码和部分伪代码来解释我的方法

my_recursive_function <- function(input_points, running_vector = c(1)){
    start_point <- input_points[1]
    rightward_points <- input_points[2:length(input_points)
    for(i in 1:length(rightward_points)){
        if(rightward_points[i] != 25 & rightward_points[i] > start_point){
            set_of_points <- c(running_vector, rightward_points[i])
            my_recursive_function(rightward_points, set_of_points)
        }
        if(rightward_points[i] == 25){
            print(c(running_vector, 25)
            flush.console()
            #I will end up doing more than printing here, but this is enough for the example
        }

        #do something to return to the previous level of recursion, 
        #including returning running_vector and rightward_points
        #to the appropriate states
    }

my_recursive_function一种可能的方法是使用不同长度的
combn
创建所有可能的组合,如下所示:

combis <- lapply(0L:(length(all_points)-2L), 
            function(n) combn(
                seq_along(all_points)[c(-1L, -length(all_points))], 
                n, 
                function(x) all_points[x],
                FALSE))

lapply(unlist(combis, recursive=FALSE),
    function(x) c(all_points[1L], x, all_points[length(all_points)]))

一种可能的方法是使用不同长度的
combn
创建所有可能的组合,如下所示:

combis <- lapply(0L:(length(all_points)-2L), 
            function(n) combn(
                seq_along(all_points)[c(-1L, -length(all_points))], 
                n, 
                function(x) all_points[x],
                FALSE))

lapply(unlist(combis, recursive=FALSE),
    function(x) c(all_points[1L], x, all_points[length(all_points)]))

这是一个非递归函数。输出是矩阵列表,每个矩阵都有对应于所需向量的列

non_recursive_function <- function(X){
  N <- length(X)
  X2 <- X[-c(1, N)]
  res <- lapply(seq_along(X2), function(k) t(combn(X2, k)))
  inx <- lapply(res, function(x){
    apply(x, 1, function(y) all(diff(y) > 0))
  })
  res <- lapply(seq_along(res), function(i) res[[i]][inx[[i]], ])
  res <- res[sapply(res, length) > 0]
  res <- lapply(res, function(x) 
    apply(as.matrix(x), 1, function(y) c(X[1], y, X[N])))
  res
}

all_points <- c(1, 4, 2, 12, 6, 5, 25)
x <- non_recursive_function(all_points)

非递归函数这里是一个非递归函数。输出是矩阵列表,每个矩阵都有对应于所需向量的列

non_recursive_function <- function(X){
  N <- length(X)
  X2 <- X[-c(1, N)]
  res <- lapply(seq_along(X2), function(k) t(combn(X2, k)))
  inx <- lapply(res, function(x){
    apply(x, 1, function(y) all(diff(y) > 0))
  })
  res <- lapply(seq_along(res), function(i) res[[i]][inx[[i]], ])
  res <- res[sapply(res, length) > 0]
  res <- lapply(res, function(x) 
    apply(as.matrix(x), 1, function(y) c(X[1], y, X[N])))
  res
}

all_points <- c(1, 4, 2, 12, 6, 5, 25)
x <- non_recursive_function(all_points)

non_recursive_函数我的问题是只能接受从左到右顺序排列的组合,所以简单的排序不起作用。不管怎样,我的实际问题有不同的约束条件,但是考虑到简化的问题输出,例如1、2、4、25是不允许的。你也可以使用相同的索引,而不是它自己的值。我的问题只能接受从左到右顺序排列的组合,因此简单的排序是不起作用的。无论如何,我的实际问题有不同的约束条件,但考虑到简化的问题,不允许像1,2,4,25这样的输出。您也可以使用相同的索引,而不是值本身