Skip to contents

This function optimizes a function or Objective with a given method.

Usage

bb_optimize(
  x,
  method = "random_search",
  max_evals = 1000,
  max_time = NULL,
  ...
)

# S3 method for `function`
bb_optimize(
  x,
  method = "random_search",
  max_evals = 1000,
  max_time = NULL,
  lower = NULL,
  upper = NULL,
  maximize = FALSE,
  ...
)

# S3 method for Objective
bb_optimize(
  x,
  method = "random_search",
  max_evals = 1000,
  max_time = NULL,
  search_space = NULL,
  ...
)

Arguments

x

(function | Objective).

method

(character(1) | Optimizer)
Key to retrieve optimizer from mlr_optimizers dictionary or Optimizer.

max_evals

(integer(1))
Number of allowed evaluations.

max_time

(integer(1))
Maximum allowed time in seconds.

...

(named list())
Named arguments passed to objective function. Ignored if Objective is optimized.

lower

(numeric())
Lower bounds on the parameters. If named, names are used to create the domain.

upper

(numeric())
Upper bounds on the parameters.

maximize

(logical())
Logical vector used to create the codomain e.g. c(TRUE, FALSE) -> ps(y1 = p_dbl(tags = "maximize"), y2 = pd_dbl(tags = "minimize")). If named, names are used to create the codomain.

search_space

(paradox::ParamSet).

Value

list of

Note

If both max_evals and max_time are NULL, TerminatorNone is used. This is useful if the Optimizer can terminate itself. If both are given, TerminatorCombo is created and the optimization stops if the time or evaluation budget is exhausted.

Examples

# function and bounds
fun = function(xs) {
  -(xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + 10
}

bb_optimize(fun, lower = c(-10, -5), upper = c(10, 5), max_evals = 10)
#> $par
#>           x1      x2
#>        <num>   <num>
#> 1: -8.384997 3.34333
#> 
#> $value
#>       y1 
#> -138.086 
#> 
#> $instance
#> <OptimInstanceSingleCrit>
#> * State:  Optimized
#> * Objective: <ObjectiveRFun:function>
#> * Search Space:
#>        id    class lower upper nlevels
#>    <char>   <char> <num> <num>   <num>
#> 1:     x1 ParamDbl   -10    10     Inf
#> 2:     x2 ParamDbl    -5     5     Inf
#> * Terminator: <TerminatorEvals>
#> * Result:
#>           x1      x2       y1
#>        <num>   <num>    <num>
#> 1: -8.384997 3.34333 -138.086
#> * Archive:
#>              x1        x2          y1
#>           <num>     <num>       <num>
#>  1: -8.38499725  3.343330 -138.086008
#>  2:  2.01521772 -3.427916    9.816657
#>  3: -9.85201118 -0.336065 -137.566718
#>  4: -0.04445223 -2.102328    5.014399
#>  5:  4.65763974  2.725215  -29.841137
#>  6:  7.49201322 -3.250594  -20.225006
#>  7: -9.31517335 -1.796143 -119.482420
#>  8: -1.95343523 -3.043302   -5.631525
#>  9: -1.92923765 -4.363385   -7.297728
#> 10: -2.22597374  4.755478  -68.006298
#> 

# function and constant
fun = function(xs, c) {
  -(xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + c
}

bb_optimize(fun, lower = c(-10, -5), upper = c(10, 5), max_evals = 10, c = 1)
#> $par
#>           x1        x2
#>        <num>     <num>
#> 1: -8.971074 0.3021246
#> 
#> $value
#>        y1 
#> -130.2685 
#> 
#> $instance
#> <OptimInstanceSingleCrit>
#> * State:  Optimized
#> * Objective: <ObjectiveRFun:function>
#> * Search Space:
#>        id    class lower upper nlevels
#>    <char>   <char> <num> <num>   <num>
#> 1:     x1 ParamDbl   -10    10     Inf
#> 2:     x2 ParamDbl    -5     5     Inf
#> * Terminator: <TerminatorEvals>
#> * Result:
#>           x1        x2        y1
#>        <num>     <num>     <num>
#> 1: -8.971074 0.3021246 -130.2685
#> * Archive:
#>            x1          x2          y1
#>         <num>       <num>       <num>
#>  1:  4.706392 -3.04043267   -6.326192
#>  2:  9.610793  2.41521529  -86.248734
#>  3: -8.971074  0.30212464 -130.268502
#>  4:  3.916478  1.88556003  -26.541583
#>  5: -9.375393 -2.74437465 -128.464921
#>  6: -3.983384  1.36465615  -53.851106
#>  7: -0.419509 -0.67828742  -10.244373
#>  8:  4.128677  4.48576576  -59.567954
#>  9: -6.393225 -2.83100124  -69.474780
#> 10:  3.603258 -0.01154389  -10.501307
#> 

# objective
fun = function(xs) {
  c(z = -(xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + 10)
}

# define domain and codomain using a `ParamSet` from paradox
domain = ps(x1 = p_dbl(-10, 10), x2 = p_dbl(-5, 5))
codomain = ps(z = p_dbl(tags = "minimize"))
objective = ObjectiveRFun$new(fun, domain, codomain)

bb_optimize(objective, method = "random_search", max_evals = 10)
#> $par
#>           x1       x2
#>        <num>    <num>
#> 1: -8.079517 2.656002
#> 
#> $value
#>        z 
#> -123.587 
#> 
#> $instance
#> <OptimInstanceSingleCrit>
#> * State:  Optimized
#> * Objective: <ObjectiveRFun:function>
#> * Search Space:
#>        id    class lower upper nlevels
#>    <char>   <char> <num> <num>   <num>
#> 1:     x1 ParamDbl   -10    10     Inf
#> 2:     x2 ParamDbl    -5     5     Inf
#> * Terminator: <TerminatorEvals>
#> * Result:
#>           x1       x2        z
#>        <num>    <num>    <num>
#> 1: -8.079517 2.656002 -123.587
#> * Archive:
#>             x1         x2            z
#>          <num>      <num>        <num>
#>  1: -8.0795168  2.6560016 -123.5870142
#>  2:  5.3934961  4.9071231  -64.0384118
#>  3:  9.4104181 -1.1081724  -48.4933075
#>  4: -0.7762707 -1.8475825    0.9642548
#>  5: -6.5064821  0.3157354  -73.3543394
#>  6: -0.1272597  2.7930863  -28.0850822
#>  7: -5.9164331  2.1339728  -79.0275903
#>  8: -8.6956778 -1.4579320 -106.7754965
#>  9:  6.5039884 -2.2618175  -10.8308250
#> 10:  1.4008990 -1.6428092    7.7991111
#>