# Evaluation of vectorized objective function returned a malformed matrix

I am working with the R programming language. I am using the "mco" library to perform "constrained multi-objective optimization" (https://cran.r-project.org/web/packages/mco/mco.pdf)

``````#load libraries
library(dplyr)
library(mco)

# 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 defined the following function that I want to optimize (7 inputs, 4 outputs):

``````#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(7)

#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, 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 )))

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

f[4] = mean(final_table\$quant)

#count number of rows in each table
n5 = data.frame(table_a %>%
summarise(count = n()))

n6 = data.frame(table_b %>%
summarise(count = n()))

n7 = data.frame(table_c %>%
summarise(count = n()))

return (f);
}
``````

Next, I defined a series of constraints:

``````gn <- function(x,f) {
g1 <- x[3] - x[1]
g2<- x[4] - x[2]
g3 <- x[7] - x[6]
g4 <- x[6] - x[5]
g5 <- n5 > 20
g6 <- n6 > 20
g7 <- n7 > 20
return(c(g1,g2,g3,g4, g5, g6, g7))
}
``````

Finally, I ran the optimization algorithm (idim = input dimension, odim = output dimension, cdim = dimension of the constraints)

``````optimization <- nsga2(funct_set, idim = 7, odim = 4 , constraints = gn, cdim = 7,

generations=150,
popsize=100,
cprob=0.7,
cdist=20,
mprob=0.2,
mdist=20,
lower.bounds=rep(80,80,80,80, 100,200,300),
upper.bounds=rep(120,120,120,120,200,300,400)
)
``````

Problem: Unfortunately, this produces an error:

``````Evaluation of vectorized objective function returned a malformed matrix. Expected 4 rows and 100 columns but got 7 rows and 100 columns.
``````

Does anyone know why this error is being produced? Is it because the "gn" object can not accept inputs such as "n5, n6, n7"?

Thanks

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.