Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中实现算法X_R_Algorithm_Recursion_Knuth - Fatal编程技术网

在R中实现算法X

在R中实现算法X,r,algorithm,recursion,knuth,R,Algorithm,Recursion,Knuth,我希望在R中实现类似的东西 问题是:我有一个nxk矩阵a,n>=k,实值项表示成本。一般来说,n和k都会非常小(n您没有将foo设置为矩阵,因此无法设置colnames(foo)或rownames(foo)。假设这只是一个输入错误,还有一个问题是,除了c=1,您永远不会访问任何内容,因为内部测试的两个分支都会返回一些内容。您可能希望在循环中收集结果,选择最佳结果,然后返回该结果 比如说, algorithmX <- function(A) { bestcost <- Inf

我希望在R中实现类似的东西


问题是:我有一个nxk矩阵a,n>=k,实值项表示成本。一般来说,n和k都会非常小(n您没有将foo设置为矩阵,因此无法设置
colnames(foo)
rownames(foo)
。假设这只是一个输入错误,还有一个问题是,除了
c=1
,您永远不会访问任何内容,因为内部测试的两个分支都会返回一些内容。您可能希望在循环中收集结果,选择最佳结果,然后返回该结果

比如说,

algorithmX <- function(A) {
  bestcost <- Inf
  save <- NULL
  for (c in 1:ncol(A)) {
    r <- which.min(A[,c])
    memory <- data.frame(LP_Number = colnames(A)[c], 
                         Visit_Number = rownames(A)[r], 
                         cost = as.numeric(A[r,c]))
    if (length(colnames(A))>1) {
      Ared <- A[-r, -c, drop=FALSE]
      memory <- rbind(memory, algorithmX(Ared)) 
    }
    if (sum(memory$cost) < bestcost) {
      bestcost <- sum(memory$cost)
      save <- memory
    }
  }
  return(save)
}

algorithmX感谢上面的user2554330提供了一些关于如何构造递归函数以便保留值的指针。我对他们的代码进行了如下修改,现在它似乎可以工作了,抓住了我之前识别的所有关键情况,这就要求我首先编写这个函数

algorithmX <- function(A) {
  best.match <- data.frame(LP_Number=numeric(), Visit_Number=numeric(), cost=numeric(), total.cost=numeric())
  for (c in 1:ncol(A)) {
    r <- which.min(A[,c])
    memory <- data.frame(LP_Number = colnames(A)[c], 
                         Visit_Number = rownames(A)[r], 
                         cost = as.numeric(A[r,c]),
                         total.cost = as.numeric(NA))
    if (length(colnames(A))>1) {
      Ared <- A[-r, -c, drop=FALSE]
      memory <- rbind(memory, algorithmX(Ared))
    }
    total.cost <- summarize(memory, sum(cost)) %>% unlist() %>% as.numeric()
    memory$total.cost <- total.cost
    if (length(best.match$total.cost)==0 | memory$total.cost[1] < best.match$total.cost[1]) {
      best.match <- memory
    }
  }
  return(best.match)
}

algorithmX这让我更接近了,谢谢。我不确定这里的礼节。我将把我对你的代码的修改放在一个单独的答案中,因为这个答案太长了。没关系。如果这是一个更好的解决方案,我会投赞成票。
algorithmX <- function(A) {
  best.match <- data.frame(LP_Number=numeric(), Visit_Number=numeric(), cost=numeric(), total.cost=numeric())
  for (c in 1:ncol(A)) {
    r <- which.min(A[,c])
    memory <- data.frame(LP_Number = colnames(A)[c], 
                         Visit_Number = rownames(A)[r], 
                         cost = as.numeric(A[r,c]),
                         total.cost = as.numeric(NA))
    if (length(colnames(A))>1) {
      Ared <- A[-r, -c, drop=FALSE]
      memory <- rbind(memory, algorithmX(Ared))
    }
    total.cost <- summarize(memory, sum(cost)) %>% unlist() %>% as.numeric()
    memory$total.cost <- total.cost
    if (length(best.match$total.cost)==0 | memory$total.cost[1] < best.match$total.cost[1]) {
      best.match <- memory
    }
  }
  return(best.match)
}