Skip to contents

Detect best candidates in the neighbourhood of the ininital condition, based on the tabu size and fitness function

Usage

tabu_search(
  initial_condition,
  fitness_fn,
  neighbor_fn,
  max_tabu_size = 100,
  max_iterations = 1000,
  ...
)

Arguments

initial_condition

Used as an initial baseline for the neighbourhood detection.

fitness_fn

Specification on how to detect the best candidate from the considered neighbourhood.

neighbor_fn

Information on how to detect neighbourhoods.

max_tabu_size

The maximum size of the considered candidates in the neighbourhood.

max_iterations

The number of iterations before to stop if the neighbourhood candidates are not better than initial condition.

...

further arguments passed to fitness_fn

Examples


data(ds_bain_candidate, package = "g6tr.tabu")
data(ds_bain_utilities, package = "g6tr.tabu")

library(dplyr)

ds_bain_candidate_many_multiple_toplevels <-
  ds_bain_candidate %>%
  bind_rows(
    tibble(
      top_level = "value",
      message = 4,
      quality = 1,
      embellish = 1,
      money_option = 3,
      fake_numeric = 8
    )
  )

ds_bain_candidate_thesame_toplevel <-
  ds_bain_candidate_many_multiple_toplevels %>%
  filter(top_level == "value")

bain_meta_design <-
  list(
    value = list(
      message = c(1:2, 4),
      quality = 1,
      embellish = 1,
      money_option = 1:6,
      fake_numeric = c(3, 6, 8, 10)
    ),
    standard = list(
      message = c(1:4),
      quality = 1:2,
      embellish = 1:2,
      money_option = 1:6,
      fake_numeric = c(3, 6, 10)
    ),
    premium = list(
      message = c(1:4),
      quality = 2:4,
      embellish = 2:4,
      money_option = 1:6,
      fake_numeric = c(3, 8, 10)
    )
  )


attr(bain_meta_design, "classes") <-
  list(
    message = "ordered",
    quality = "categorical",
    embellish = "categorical",
    money_option = "ordered",
    fake_numeric = "numeric"
  )

# set the price calculator
bain_price_calculator <- function(candidate) {
  candidate %>%
    mutate(money_option = as.integer(money_option)) %>%
    mutate(
      price = case_when(
        top_level == "value" & money_option == 1 ~ 0.7,
        top_level == "value" & money_option == 2 ~ 0.9,
        top_level == "value" & money_option == 3 ~ 1.1,
        top_level == "value" & money_option == 4 ~ 1.25,
        top_level == "value" & money_option == 5 ~ 1.75,
        top_level == "value" & money_option == 6 ~ 1.99,
        top_level == "standard" & money_option == 1 ~ 2.99,
        top_level == "standard" & money_option == 2 ~ 4.1,
        top_level == "standard" & money_option == 3 ~ 4.5,
        top_level == "standard" & money_option == 4 ~ 4.75,
        top_level == "standard" & money_option == 5 ~ 4.99,
        top_level == "standard" & money_option == 6 ~ 5.15,
        top_level == "premium" & money_option == 1 ~ 5.5,
        top_level == "premium" & money_option == 2 ~ 6.99,
        top_level == "premium" & money_option == 3 ~ 7.95,
        top_level == "premium" & money_option == 4 ~ 8.65,
        top_level == "premium" & money_option == 5 ~ 8.99,
        top_level == "premium" & money_option == 6 ~ 9.75
      )
    ) %>%
    pull(price)
}

neighbours_candidate_lock_message <-
  tabu_neighbors_generator(
    lock_columns = c("message", "top_level"),
    meta_design = bain_meta_design,
    lock_rows = NULL
  )

fitness_share_concepts <-
  tabu_fitness_generator(ds_bain_utilities,
    prohibitions = NULL,
    price_calculator = bain_price_calculator,
    top_level = "top_level",
    measure = "share",
    concepts = c(1, 3), # should we calculate the share for specific concepts
    meta_design = bain_meta_design,
    toplevel_related_variables = "money_option",
    utils_none_name = "NONE"
  )

tabu_search(ds_bain_candidate,
  fitness_share_concepts,
  neighbours_candidate_lock_message,
  max_iterations = 100
)
#> Finding best bundle...
#> ==================================================
#> 
#> Iteration Fitness
#>         0  0.49
#>         1  0.506
#>         2  0.514
#>         3  0.514
#> 
#> Best bundle found. Returning optimal bundle.
#> 
#> # A tibble: 3 × 6
#>   top_level message quality embellish money_option fake_numeric
#>   <chr>       <dbl>   <dbl>     <dbl>        <int>        <dbl>
#> 1 value           1       1         1            1           10
#> 2 standard        1       1         1            1            6
#> 3 premium         1       4         4            1            3
tabu_search(ds_bain_candidate_many_multiple_toplevels,
  fitness_share_concepts,
  neighbours_candidate_lock_message,
  max_iterations = 100
)
#> Finding best bundle...
#> ==================================================
#> 
#> Iteration Fitness
#>         0  0.249
#>         1  0.307
#>         2  0.342
#>         3  0.353
#>         4  0.361
#>         5  0.361
#>         6  0.361
#> 
#> Best bundle found. Returning optimal bundle.
#> 
#> # A tibble: 4 × 6
#>   top_level message quality embellish money_option fake_numeric
#>   <chr>       <dbl>   <dbl>     <dbl>        <dbl>        <dbl>
#> 1 value           1       1         1            1           10
#> 2 standard        1       1         1            1            3
#> 3 premium         1       4         4            1            3
#> 4 value           4       1         1            3            3
tabu_search(ds_bain_candidate_thesame_toplevel,
  fitness_share_concepts,
  neighbours_candidate_lock_message,
  max_iterations = 100
)
#> Finding best bundle...
#> ==================================================
#> 
#> Iteration Fitness
#>         0  0.59
#>         1  0.632
#>         2  0.632
#> 
#> Best bundle found. Returning optimal bundle.
#> 
#> # A tibble: 2 × 6
#>   top_level message quality embellish money_option fake_numeric
#>   <chr>       <dbl>   <dbl>     <dbl>        <dbl>        <dbl>
#> 1 value           1       1         1            1           10
#> 2 value           4       1         1            3            6