R 错误:(all(xi <= xj) && any(xi < xj)) { :需要 TRUE/FALSE 的缺失值

问题描述

我正在使用 R 编程语言。我正在尝试使用以下库来优化我编写的任意函数https://cran.r-project.org/web/packages/nsga2R/nsga2R.pdf

首先我为这个例子创建了一些数据:

#load library
library(dplyr)
library(nsga2R)

#create data for this example
# create some data for this example
a1 = rnorm(1000,100,10)
b1 = rnorm(1000,10)
c1 = sample.int(1000,1000,replace = TRUE)
train_data = data.frame(a1,b1,c1)

然后,我定义了优化函数(7 个输入,4 个输出):

#define function

funct_set <- function (x) {
    x1 <- x[1]; x2 <- x[2]; x3 <- x[3] ; x4 <- x[4]; x5 <- x[5]; x6 <- x[6]; x[7] <- x[7]
    f <- numeric(4)
    
    
    #bin data according to random criteria
    train_data <- train_data %>%
        mutate(cat = ifelse(a1 <= x1 & b1 <= x3,"a",ifelse(a1 <= x2 & b1 <= x4,"b","c")))
    
    train_data$cat = as.factor(train_data$cat)
    
    #new splits
    a_table = train_data %>%
        filter(cat == "a") %>%
        select(a1,c1,cat)
    
    b_table = train_data %>%
        filter(cat == "b") %>%
        select(a1,cat)
    
    c_table = train_data %>%
        filter(cat == "c") %>%
        select(a1,cat)
    
    
    
    #calculate  quantile ("quant") for each bin
    
    table_a = data.frame(a_table%>% group_by(cat) %>%
                             mutate(quant = ifelse(c1 > x[5],1,0 )))
    
    table_b = data.frame(b_table%>% group_by(cat) %>%
                             mutate(quant = ifelse(c1 > x[6],0 )))
    
    table_c = data.frame(c_table%>% group_by(cat) %>%
                             mutate(quant = ifelse(c1 > x[7],0 )))
    
    f[1] = mean(table_a$quant)
    f[2] = mean(table_b$quant)
    f[3] = mean(table_c$quant)
    
    
    #group all tables
    
    final_table = rbind(table_a,table_b,table_c)
    # calculate the total mean : this is what needs to be optimized
    
    f[4] = mean(final_table$quant)
    
    
    return (f);
}

然后,我运行了优化代码

#optimization
results <- nsga2R(fn=funct_set,varNo=7,objdim=4,lowerBounds=c(80,80,200,300),upperBounds=c(120,120,300,400),popSize=50,tourSize=2,generations=50,cprob=0.9,XoverdistIdx=20,mprob=0.1,MudistIdx=3)

但这会返回以下错误

Error in if (all(xi <= xj) && any(xi < xj)) { : 
  missing value where TRUE/FALSE needed

有谁知道产生的错误是否是因为我为这个问题定义了函数/数据的方式?或者是否有其他原因导致出现此错误

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)