title: R Error: Optimization resulting in "unused argument(x)"
body: <p>I am working with the R programming language. I defined the following function and I am trying to perform the &quot;random search&quot; algorithm on this function.</p>
  <p>First, I loaded the library:</p>
  <pre><code>#load library : https://cran.r-project.org/web/packages/randomsearch/index.html 
  
  library(randomsearch)
  </code></pre>
  <p>Then, I defined the function:</p>
  <pre><code># 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[1], x[2], x[3], x[4] and 4 outputs f1, f2, f3, f4)
  
  fn &lt;- function(i) {
      x1 &lt;- x[i,1]; x2 &lt;- x[i,2]; x3 &lt;- x[i,3] ; x4 &lt;- x[i,4]
      f &lt;- numeric(4)
      #bin data according to random criteria
      train_data &lt;- train_data %&gt;%
          mutate(cat = ifelse(a1 &lt;= x1 &amp; b1 &lt;= x3, &quot;a&quot;,
                              ifelse(a1 &lt;= x2 &amp; b1 &lt;= x4, &quot;b&quot;, &quot;c&quot;)))
      
      train_data$cat = as.factor(train_data$cat)
      
      #new splits
      a_table = train_data %&gt;%
          filter(cat == &quot;a&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      b_table = train_data %&gt;%
          filter(cat == &quot;b&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      c_table = train_data %&gt;%
          filter(cat == &quot;c&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      
      #calculate  quantile (&quot;quant&quot;) for each bin
      
      table_a = data.frame(a_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = ifelse(c1 &gt; 150,1,0 )))
      
      table_b = data.frame(b_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = ifelse(c1 &gt; 300,1,0 )))
      
      table_c = data.frame(c_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = ifelse(c1 &gt; 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)
      
      #add some constraints
      if((x3 - x1) &lt; 0. | (x4 - x2) &lt; 0.) {
          f[1] &lt;- NaN
          f[2] &lt;- NaN
          f[3] &lt;- NaN
          f[4] &lt;- NaN
          
      }
      
      return (f)
  }
  </code></pre>
  <p>Finally, I tried to run the &quot;random search&quot; algorithm on this function:</p>
  <pre><code>#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)
  </code></pre>
  <p>But this resulted in the following error:</p>
  <blockquote>
  <p>Error in fun(x, ...) : unused argument (x)</p>
  </blockquote>
  <p>Does anyone know why this error is being produced? Is it related to the way I have defined the function &quot;fn&quot;?</p>
  <p>Thanks</p>