## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5)

## ----setup--------------------------------------------------------------------
library(paretoinfer)

## ----problem------------------------------------------------------------------
means <- rbind(A1 = c(0.15, 0.70), A2 = c(0.40, 0.40), A3 = c(0.70, 0.15),
               A4 = c(0.42, 0.43), A5 = c(0.60, 0.60), A6 = c(0.30, 0.80))
simulate <- function(alternative, n, h = 0.1) {
  mu <- means[alternative, ]
  data.frame(y1 = runif(n, mu[1] - h, mu[1] + h), y2 = runif(n, mu[2] - h, mu[2] + h))
}

## ----budget-------------------------------------------------------------------
design <- pareto_design(alternatives = rownames(means), objectives = c("y1", "y2"),
                        bounds = c(0, 1), alpha = 0.05, epsilon = 0.05,
                        cost = c(1, 1, 1, 1, 5, 5), budget = 400)
set.seed(2)
state <- pareto_run(design, simulate, batch = 5, initial = 5)
state
plausible_pareto_set(state)$table[, c("alternative", "status", "n_evaluations", "cost")]

## ----report-------------------------------------------------------------------
pareto_report(state)

## ----larger-------------------------------------------------------------------
design2 <- pareto_design(alternatives = rownames(means), objectives = c("y1", "y2"),
                         bounds = c(0, 1), alpha = 0.05, epsilon = 0.05,
                         cost = c(1, 1, 1, 1, 5, 5), budget = 4000)
set.seed(2)
state2 <- pareto_run(design2, simulate, batch = 5, initial = 5)
glance(state2)[, c("n_evaluations", "total_cost", "n_dominated", "identified", "stopping_reason")]
plausible_pareto_set(state2)$table[, c("alternative", "status", "dominated_by", "n_evaluations", "cost")]

## ----manual-------------------------------------------------------------------
design3 <- pareto_design(alternatives = rownames(means), objectives = c("y1", "y2"),
                         bounds = c(0, 1), alpha = 0.05, epsilon = 0.05, cost = c(1, 1, 1, 1, 5, 5))
set.seed(3)
state3 <- initialize_pareto(design3)
for (k in design3$alternatives) {
  ev <- simulate(k, 5); ev$alternative <- k
  state3 <- update_objectives(state3, ev)
}
repeat {
  sets <- plausible_pareto_set(state3)
  if (sets$identified || state3$total_cost >= 1500) break
  nxt <- choose_next_evaluation(state3)
  ev <- simulate(nxt$alternative, 5); ev$alternative <- nxt$alternative
  state3 <- update_objectives(state3, ev)
}
state3

