R &引用;替换长度等于零“;

R &引用;替换长度等于零“;,r,if-statement,optimization,error-handling,R,If Statement,Optimization,Error Handling,我运行程序时出错。它使用的是genalg包,但是这个包不是问题所在,因为我遇到了多个包的问题。我相信我的if语句中有一个错误,我不知道如何使它运行并找到正确的答案。错误是 Error in evalVals[object] <- evalFunc(population[object, ]) : replacement has length zero evalVals[object]中的错误=1 x12=2 x246=1 x34=1 xtot=2 x456=3 x2456=1 x56

我运行程序时出错。它使用的是genalg包,但是这个包不是问题所在,因为我遇到了多个包的问题。我相信我的if语句中有一个错误,我不知道如何使它运行并找到正确的答案。错误是

Error in evalVals[object] <- evalFunc(population[object, ]) : 
  replacement has length zero
evalVals[object]中的错误=1 x12=2 x246=1 x34=1 xtot=2 x456=3 x2456=1 x56添加:


在您的
if
语句的末尾。

我不确定这里发生了什么,但我建议使用一致的样式。一般来说,坚持使用一种特定的方式键入代码有助于避免错误


我读过的所有R书籍都建议只使用
,正如前面提到的,我认为您缺少了最后的
else
语句,添加了
else return(0)
解决了它。我还建议将
else if
构造替换为
开关
,并添加适当的默认情况(当前缺少
else

在问题中包含完整的错误信息!
#initilaize library
library(genalg)

#insert data

#set objective
#minimize the sume of x1,x2,x3,x4,x5,x6
datat = data.frame(city1 = c(0, 10, 20, 30, 30, 20), 
                   city2 = c(10, 0, 25, 35, 15, 30),
                   city3 = c(20, 25, 0, 15, 30, 20),
                   city4 = c(30, 35, 15, 0, 15, 25),
                   city5 = c(30, 15, 30, 15, 0, 14),
                   city6 = c(20, 30, 20, 25, 14, 0))

coeff = c(0, 10, 15, 15, 14, 0)

#constraints
#add in a penalty to run function



evalFunc = function(x = c()){
  # x1 + x2 >= 1
  x12 <- c(1, 1, 0, 0, 0, 0)
  # x2 + x4 + x6 >= 2
  x246 <- c(0, 1, 0, 1, 0, 1)
  # x3 + x4 >= 1
  x34 <- c(0, 0, 1, 1, 0, 0)
  # x1 + x2 + x3 + x4 + x5 + x6 >= 1
  xtot <- c(1, 1, 1, 1, 1, 1)
  # x4 + x5 + x6 >= 2
  x456 <- c(0, 0, 0, 1, 1, 1)
  # x2 + x4 + x5 + x6 >= 3
  x2456 <- c(0, 1, 0, 1, 1, 1)
  # x5 + x6 >= 1
  x56 <- c(0, 0, 0, 0, 1, 1)
  objt <-  sum(x*xtot)
  con12 <-  sum(x*x12)
  con246 <-  sum(x*x246)
  con34 <-  sum(x*x34)
  con456 <-  sum(x*x456)
  con2456 <-  sum(x*x2456)
  con56 <-  sum(x*x56)
  minnot <- 0
  min1 <- 1
  min2 <- 2

  # objt >= minnot
  # con12 >= minnot
  # con246 >= min1
  # con34 >= minnot
  # con456 >= min1
  # con2456 >= min2
  # con56 >= minnot

  print(x)
  c12 <- c(2, 0, 0, 0, 0, 0)
  y = sum(x*c12)
  print(y)

  if (objt <= minnot) {
    return(400)}#xtot <- c(100, 100, 100, 100, 100, 100)#multiplication is causing error
    else if (con12 <= minnot) {
      return(400)}#x12 <- c(100, 100, 100, 100, 100, 100) #how do I fix it?
      else if (con246 <= min1) {
        return(400)} #x246 <- c(100, 100, 100, 100, 100, 100)
        else if (con34 <= minnot) {
          return(400)} # x34 <- c(100, 100, 100, 100, 100, 100)
          else if (con456 <= min1) {
            return(400)} #x456 <- c(100, 100, 100, 100, 100, 100)
              else if (con2456 <= min2) {
                return(400)} #x2456 <- c(100, 100, 100, 100, 100, 100)
                else if (con56 <= minnot) {
                  return(400)} #x56 <- c(100, 100, 100, 100, 100, 100)

  }



#create function to create shortest path
gamod = rbga.bin(size = 6,
                 popSize = 200,
                 iters = 300,
                 mutationChance = .01,
                 elitism = 1,
                 evalFunc = evalFunc)

#run  function


#display results
 else {
    return(0)} # Replace 0 by what you want
    if (condition) {
    print("foo")
    }