1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
#' @title Get the length of the optimization path.
#'
#' @description
#' Dependent parameters whose requirements are not satisfied are represented by a scalar
#' NA in the output.
#'
#' @template arg_op
#' @return `integer(1)`
#' @export
#' @family optpath
getOptPathLength = function(op) {
UseMethod("getOptPathLength")
}
#' @title Get an element from the optimization path.
#'
#' @description
#' Dependent parameters whose requirements are not satisfied are represented by a scalar NA
#' in the elements of `x` of the return value.
#'
#' @template arg_op
#' @param index (`integer(1)`)\cr
#' Index of element.
#' @return List with elements `x` (named `list`), `y` (named `numeric`),
#' `dob` `integer(1)`, `eol` `integer(1)`.
#' The elements `error.message` (`character(1)`),
#' `exec.time` (`numeric(1)`) and `extra` (named `list`) are
#' there if the respective options in [OptPath()] are enabled.
#' @rdname getOptPathEl
#' @export
#' @family optpath
getOptPathEl = function(op, index) {
UseMethod("getOptPathEl")
}
#' Get data.frame of input points (X-space) referring to the param set from the optimization path.
#'
#' @template arg_op
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @return [data.frame].
#' @export
#' @family optpath
getOptPathX = function(op, dob, eol) {
UseMethod("getOptPathX")
}
#' Get y-vector or y-matrix from the optimization path.
#'
#' @template arg_op
#' @param names [character]\cr
#' Names of performance measure.
#' Default is all performance measures in path.
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @param drop (`logical(1)`)\cr
#' Return vector instead of matrix when only one y-column was selected?
#' Default is `TRUE`.
#' @return (`numeric` | `matrix`). The columns of the matrix are always named.
#' @export
#' @family optpath
getOptPathY = function(op, names, dob, eol, drop = TRUE) {
UseMethod("getOptPathY")
}
#' Get date-of-birth vector from the optimization path.
#'
#' @template arg_op
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @return [integer].
#' @export
#' @family optpath
getOptPathDOB = function(op, dob, eol) {
UseMethod("getOptPathDOB")
}
#' Get end-of-life vector from the optimization path.
#'
#' @template arg_op
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @return [integer].
#' @export
#' @family optpath
getOptPathEOL = function(op, dob, eol) {
UseMethod("getOptPathEOL")
}
#' Get error-message vector from the optimization path.
#'
#' @template arg_op
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @return [character].
#' @export
#' @family optpath
getOptPathErrorMessages = function(op, dob, eol) {
UseMethod("getOptPathErrorMessages")
}
#' Get exec-time vector from the optimization path.
#'
#' @template arg_op
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @return [numeric].
#' @export
#' @family optpath
getOptPathExecTimes = function(op, dob, eol) {
UseMethod("getOptPathExecTimes")
}
#' Get column from the optimization path.
#'
#' @template arg_op
#' @param name (`character(1)`)\cr
#' Name of the column.
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @return Single column as a vector.
#' @export
#' @family optpath
getOptPathCol = function(op, name, dob, eol) {
UseMethod("getOptPathCol")
}
#' Get columns from the optimization path.
#'
#' @template arg_op
#' @param names [character]\cr
#' Names of the columns.
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @inheritParams as.data.frame.OptPathDF
#' @return [data.frame].
#' @export
#' @family optpath
getOptPathCols = function(op, names, dob, eol, row.names = NULL) {
UseMethod("getOptPathCols")
}
#' Get index of the best element from optimization path.
#'
#' @template arg_op
#' @param y.name (`character(1)`)\cr
#' Name of target value to decide which element is best.
#' Default is `y.names[1]`.
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @param ties (`character(1)`)\cr
#' How should ties be broken when more than one optimal element is found?
#' \dQuote{all}: return all indices,
#' \dQuote{first}: return first optimal element in path,
#' \dQuote{last}: return last optimal element in path,
#' \dQuote{random}: return random optimal element in path.
#' Default is \dQuote{last}.
#' @return [`integer`]
#' Index or indices into path. See `ties`.
#' @export
#' @family optpath
#' @examples
#' ps = makeParamSet(makeNumericParam("x"))
#' op = makeOptPathDF(par.set = ps, y.names = "y", minimize = TRUE)
#' addOptPathEl(op, x = list(x = 1), y = 5)
#' addOptPathEl(op, x = list(x = 2), y = 3)
#' addOptPathEl(op, x = list(x = 3), y = 9)
#' addOptPathEl(op, x = list(x = 4), y = 3)
#' as.data.frame(op)
#' getOptPathBestIndex(op)
#' getOptPathBestIndex(op, ties = "first")
getOptPathBestIndex = function(op, y.name = op$y.names[1], dob = op$env$dob, eol = op$env$eol, ties = "last") {
assertClass(op, "OptPath")
assertChoice(y.name, choices = op$y.names)
dob = asInteger(dob, any.missing = TRUE)
eol = asInteger(eol, any.missing = TRUE)
assertChoice(ties, c("all", "first", "last", "random"))
life.inds = which(op$env$dob %in% dob & op$env$eol %in% eol)
if (length(life.inds) == 0) {
stop("No element found which matches dob and eol restrictions!")
}
y = getOptPathY(op, y.name)[life.inds]
if (all(is.na(y))) {
best.inds = life.inds
} else {
if (op$minimize[y.name]) {
best.inds = which(min(y, na.rm = TRUE) == y)
} else {
best.inds = which(max(y, na.rm = TRUE) == y)
}
best.inds = life.inds[best.inds]
}
if (length(best.inds) > 1L) {
if (ties == "all") {
return(best.inds)
} else if (ties == "first") {
return(best.inds[1])
} else if (ties == "last") {
return(best.inds[length(best.inds)])
} else if (ties == "random") {
return(best.inds[sample(length(best.inds), 1L)])
}
} else {
return(best.inds)
}
}
#' Get indices of pareto front of optimization path.
#'
#' @template arg_op
#' @param y.names [character]\cr
#' Names of performance measures to construct pareto front for.
#' Default is all performance measures.
#' @template arg_opgetter_dob
#' @template arg_opgetter_eol
#' @param index (`logical(1)`)\cr
#' Return indices into path of front or y-matrix of nondominated points?
#' Default is `FALSE`.
#' @return `matrix` | `integer`. Either matrix (with named columns) of points of
#' front in objective space or indices into path for front.
#' @export
#' @family optpath
#' @examples
#' ps = makeParamSet(makeNumericParam("x"))
#' op = makeOptPathDF(par.set = ps, y.names = c("y1", "y2"), minimize = c(TRUE, TRUE))
#' addOptPathEl(op, x = list(x = 1), y = c(5, 3))
#' addOptPathEl(op, x = list(x = 2), y = c(2, 4))
#' addOptPathEl(op, x = list(x = 3), y = c(9, 4))
#' addOptPathEl(op, x = list(x = 4), y = c(4, 9))
#' as.data.frame(op)
#' getOptPathParetoFront(op)
#' getOptPathParetoFront(op, index = TRUE)
getOptPathParetoFront = function(op, y.names = op$y.names, dob = op$env$dob, eol = op$env$eol, index = FALSE) {
assertClass(op, "OptPath")
assertCharacter(y.names, min.len = 2L)
assertSubset(y.names, op$y.names, empty.ok = FALSE)
dob = asInteger(dob, any.missing = TRUE)
eol = asInteger(eol, any.missing = TRUE)
assertFlag(index, na.ok = TRUE)
requirePackages("emoa", default.method = "load")
life.inds = which(op$env$dob %in% dob & op$env$eol %in% eol)
if (length(life.inds) == 0L) {
stop("No element found which matches dob and eol restrictions!")
}
y = getOptPathY(op, y.names, drop = FALSE)[life.inds, , drop = FALSE]
# multiply columns with -1 if maximize
k = ifelse(op$minimize, 1, -1)
y2 = t(y) * k
# is_dominated has kind of buggy behavoiur if y2 is a row
# (it hinks, we have a 1-dimensional optimization prob und returns the min index)
# so we have to treat this case manually
if (nrow(y2) == 1L) {
nondom = 1L
} else {
nondom = which(!emoa::is_dominated(y2))
}
if (index) {
return(life.inds[nondom])
} else {
return(y[nondom, , drop = FALSE])
}
}
|