# "Unused Argument Error"

I am working with the R programming language. I defined the following function and I am trying to perform the "random search" algorithm on this function.

``````#load library : https://cran.r-project.org/web/packages/randomsearch/index.html

library(randomsearch)
``````

Then, I defined the function:

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

#define function (4 inputs x, x, x, x and 4 outputs f1, f2, f3, f4)

fn <- function(i) {
x1 <- x[i,1]; x2 <- x[i,2]; x3 <- x[i,3] ; x4 <- x[i,4]
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, 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 > 150,1,0 )))

table_b = data.frame(b_table%>% group_by(cat) %>%
mutate(quant = ifelse(c1 > 300,1,0 )))

table_c = data.frame(c_table%>% group_by(cat) %>%
mutate(quant = ifelse(c1 > 400,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)

if((x3 - x1) < 0. | (x4 - x2) < 0.) {
f <- NaN
f <- NaN
f <- NaN
f <- NaN

}

return (f)
}
``````

Finally, I tried to run the "random search" algorithm on this function:

``````#run algorithm
res = randomsearch(fn, lower = c(80, 80, 80, 80), upper = c(100,120,100,120), minimize = c(TRUE, TRUE, TRUE, TRUE), max.evals = 30)
rs = summary(res)
``````

But this resulted in the following error:

``````Error in fun(x, ...) : unused argument (x)
``````

Does anyone know why this error is being produced? Is it related to the way I have defined the function "fn"?

Thanks

Note: I was able to get this code to run on a simpler function:

``````library(randomsearch)

fun_1  <- function(x) {
var_1 <- sin(x + x)
var_2 <- cos(x - x)
var_3 <- x + x
var_4 <- x + x -7
goal_1 = sum(var_1 + var_2 + var_3 + var_4)
goal_2 = var_1 - var_2 + var_3 + var_4

if (var_1 > 0.6 ) {
goal_1 = 111111111111111111111111111 }
else {
goal_1 = sum(var_1 + var_2 + var_3 + var_4)
}

return(c(goal_1, goal_2))

}

res = randomsearch(fun_1, lower = c(0, 0, 0, 0), upper = c(100,100,100,100), minimize = c(TRUE, TRUE), max.evals = 30)
rs = summary(res)
``````

Now, view the results:

``````head(rs)
Randomsearch Result:
Multiobjective Search Pareto Front
y_1      y_2       x1       x2       x3       x4
30 50.88336 52.88124 69.40536 47.46023 0.458284 5.775114
``````

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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.62446797,"math_prob":0.9991469,"size":2178,"snap":"2022-05-2022-21","text_gpt3_token_len":728,"char_repetition_ratio":0.11545538,"word_repetition_ratio":0.046647232,"special_character_ratio":0.39439854,"punctuation_ratio":0.19196428,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99971396,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-23T03:26:09Z\",\"WARC-Record-ID\":\"<urn:uuid:d02aea6f-7ba8-4256-85a9-f784674e78da>\",\"Content-Length\":\"25382\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8fd4f516-0fb8-41e1-b379-8f608b000e69>\",\"WARC-Concurrent-To\":\"<urn:uuid:fbfbe446-3b13-4f0a-9c05-7428d3d61826>\",\"WARC-IP-Address\":\"167.99.20.217\",\"WARC-Target-URI\":\"https://community.rstudio.com/t/unused-argument-error/111838\",\"WARC-Payload-Digest\":\"sha1:5KNFI3VLJUC6EAS3CT5VYEYGV5RS4YX3\",\"WARC-Block-Digest\":\"sha1:TS3V5BXRP3RFTLNIDCOLBMEVNGWZ6VNJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662552994.41_warc_CC-MAIN-20220523011006-20220523041006-00112.warc.gz\"}"}