R: Error in fn(unlist(x), …) : unused argument (unlist(x))

I am using the "mlrMBO" library in R (CRAN - Package mlrMBO). I am trying to learn how to run an optimization algorithm on some arbitrary function that I defined:

#Load library
library(mlrMBO)

#define objective function
obj.fn = makeMultiObjectiveFunction(
  name = "My test function",
  fn = function(x1, x2, x3, x4) {
    var_1 <- sin(x1 + x2)
    var_2 <- cos(x1 - x2)
    var_3 <- x1 + x4
    var_4 <- x3 + x4 -7
        goal_1 = sum(var_1 + var_2 + var_3 + var_4)
    goal_2 = var_1 + var_2 - var_3 + var_4
    goal_3 = var_1 + var_2 - var_3 + 2*var_4

    return(c(goal_1, goal_2, goal_3))

},
#define acceptable ranges
par.set = makeParamSet(
     makeNumericParam("x1", lower = 20, upper = 40),
     makeNumericParam("x2", lower = 30, upper = 45),
     makeNumericParam("x3", lower = 10, upper = 20),
     makeNumericParam("x4", lower = 10, upper = 50),
#define constraints
    forbidden = expression(x2 >x1 | x3 > x4)
),

minimize=TRUE
)

#create control gird
 control=makeMBOControl(propose.points=1, final.method="best.predicted", final.evals=10)
 control=setMBOControlTermination(control, iters=10)
 control=setMBOControlInfill(control, crit=makeMBOInfillCritEI())

#perform optimization
lrn=makeMBOLearner(control, obj.fun)

This code returns the following error:

Error in fn(unlist(x), ...) : unused argument (unlist(x))

Does anyone know why this error is being produced? I have tried to look at various reference material (e.g. https://cran.r-project.org/web/packages/mlrMBO/mlrMBO.pdf ), but so far I am unable to understand why this error is being produced.

Can someone please show me what am I doing wrong?

Thanks

first, your error relates only to makeMultoObjectiveFunction() call.
It seems you are missing n.objectives, and supplying a suitable minimize param to match.

#define objective function
obj.fn = makeMultiObjectiveFunction(
  name = "My test function",
  fn = function(x1, x2, x3, x4) {
    var_1 <- sin(x1 + x2)
    var_2 <- cos(x1 - x2)
    var_3 <- x1 + x4
    var_4 <- x3 + x4 -7
    goal_1 = sum(var_1 + var_2 + var_3 + var_4)
    goal_2 = var_1 + var_2 - var_3 + var_4
    goal_3 = var_1 + var_2 - var_3 + 2*var_4
    
    return(c(goal_1, goal_2, goal_3))
    
  },
  n.objectives = 3L,
  #define acceptable ranges
  par.set = makeParamSet(
    makeNumericParam("x1", lower = 20, upper = 40),
    makeNumericParam("x2", lower = 30, upper = 45),
    makeNumericParam("x3", lower = 10, upper = 20),
    makeNumericParam("x4", lower = 10, upper = 50)
    #define constraints
   , forbidden = expression(x2 >x1 | x3 > x4)
  ),
  
  minimize=rep(TRUE,3)
)
1 Like

Thank yo so much for your answer! After running the corrected version of the code, along with

I tried to execute everything:



res = mbo(obj.fun, design = NULL, learner = lrn, control = ctrl, show.info = TRUE)

But I got this error:

Warning in generateDesign(n.params * 4L, par.set, fun = lhs::maximinLHS) :
  generateDesign could only produce 0 points instead of 16!
Computing y column(s) for design. Not provided.

Error in as.data.frame.OptPathDF(opt.path, include.rest = FALSE) : 
  No elements where selected (via 'dob' and 'eol')!

Do you know why this is happening?

Thank you so much!

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.