Get fitness values in the correct form as used by Selector operators from an OptimInstance. This works for both single-criterion and multi-criterion optimization, and entails multiplying objectives with -1 if they are being minimized, since Selector tries to maximize fitness.

mies_get_fitnesses(inst, rows)

Arguments

inst

(OptimInstance)
Optimization instance to evaluate.

rows

optional (integer)
Indices of rows within inst to consider. If this is not given, then the entire archive is used.

Value

numeric

matrix with length(rows) (if rows is given, otherwise nrow(inst$archive$data)) rows and one column for each objective: fitnesses to be maximized.

Examples

set.seed(1)
library("bbotk")
lgr::threshold("warn")

# Define the objective to optimize
objective <- ObjectiveRFun$new(
  fun = function(xs) {
    z <- exp(-xs$x^2 - xs$y^2) + 2 * exp(-(2 - xs$x)^2 - (2 - xs$y)^2)
    list(Obj = z)
  },
  domain = ps(x = p_dbl(-2, 4), y = p_dbl(-2, 4)),
  codomain = ps(Obj = p_dbl(tags = "maximize"))
)

# Get a new OptimInstance
oi <- OptimInstanceSingleCrit$new(objective,
  terminator = trm("evals", n_evals = 100)
)

mies_init_population(inst = oi, mu = 3)

oi$archive
#> <Archive>
#>        x     y dob eol x_id     Obj              timestamp batch_nr
#> 1: -0.41  3.45   1  NA    1 0.00075 2023-09-20 04:41:21.93        1
#> 2:  0.23 -0.79   1  NA    2 0.50760 2023-09-20 04:41:21.93        1
#> 3:  1.44  3.39   1  NA    3 0.21083 2023-09-20 04:41:21.93        1

mies_get_fitnesses(oi, c(2, 3))
#>            Obj
#> [1,] 0.5076046
#> [2,] 0.2108264

###
# Multi-objective, and automatic maximization:
objective2 <- ObjectiveRFun$new(
  fun = function(xs) list(Obj1 = xs$x^2, Obj2 = -xs$y^2),
  domain = ps(x = p_dbl(-2, 4), y = p_dbl(-2, 4)),
  codomain = ps(
    Obj1 = p_dbl(tags = "minimize"),
    Obj2 = p_dbl(tags = "maximize")
  )
)
# Using MultiCrit!
oi <- OptimInstanceMultiCrit$new(objective2,
  terminator = trm("evals", n_evals = 100)
)

mies_init_population(inst = oi, mu = 3)

oi$archive
#> <Archive>
#>      x     y dob eol x_id Obj1  Obj2              timestamp batch_nr
#> 1: 3.7 -1.63   1  NA    1 13.5 -2.65 2023-09-20 04:41:22.00        1
#> 2: 2.0 -0.76   1  NA    2  3.9 -0.58 2023-09-20 04:41:22.00        1
#> 3: 1.8 -0.94   1  NA    3  3.1 -0.88 2023-09-20 04:41:22.00        1

# Note Obj1 has a different sign than in the archive.
mies_get_fitnesses(oi, c(2, 3))
#>           Obj1       Obj2
#> [1,] -3.860387 -0.5839291
#> [2,] -3.149504 -0.8848403