I am working with the R programming language. I am trying to use the following library (https://cran.r-project.org/web/packages/mopsocd/mopsocd.pdf) for the optimizing a function with constraints.

First, I defined the function ("funct_set") that I wanted to optimize:

```
#Load library:
library(mopsocd)
```

Then, I created some data for this example:

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

I then defined the function used in this example:

```
#define function:
funct_set <- function (x) {
#bin data according to random criteria
train_data <- train_data %>%
mutate(cat = ifelse(a1 <= x[1] & b1 <= x[3], "a",
ifelse(a1 <= x[2] & b1 <= x[4], "b", "c")))
train_data$cat = as.factor(train_data$cat)
#new splits
a_table = train_data %>%
filter(cat == "a") %>%
select(a1, b1, c1, cat)
b_table = train_data %>%
filter(cat == "b") %>%
select(a1, b1, c1, cat)
c_table = train_data %>%
filter(cat == "c") %>%
select(a1, b1, c1, 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],1,0 )))
table_c = data.frame(c_table%>% group_by(cat) %>%
mutate(quant = ifelse(c1 > x[7],1,0 )))
f1 = mean(table_a$quant)
f2 = mean(table_b$quant)
f3 = 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
f4 = mean(final_table$quant)
return (c(f1, f2, f3, f4));
}
```

Then, I defined the constraints :

```
#define constraints
gn <- function(x) {
g1 <- x[3] - x[1] >= 0.0
g2 <- x[4] - x[2] >= 0.0
g3 <- x[7] - x[6] >0
g4<- x[6] - x[5] >0
return(c(g1,g2,g3, g4))
}
```

Next, I customized some of the options for the optimization function (e.g. number of variables, lower and upper bounds for the ranges, etc.)

```
## Set Arguments
varcount <- 7
fncount <- 4
lbound <- c(80,85,100,120,90, 110, 130)
ubound <- c(220,220,220,220,220, 145, 189)
optmin <- 0
```

Lastly, I ran the optimization function itself:

```
#run optimization
ex1 <- mopsocd(funct_set,gn, varcnt=varcount,fncnt=fncount,
lowerbound=lbound,upperbound=ubound,opt=optmin)
```

**Problem:** However, this produces the following error:

```
Error in if ((max(rowSums(x)) == fncnt) == FALSE) { :
missing value where TRUE/FALSE needed
```

Does anyone know why this error is being produced? Is it because I have used an incorrect format to define the functions? Is it because the ranges (e.g. "lbound" and "ubound") are illogical?

Can someone please show me what I am doing wrong?

Thanks