The Objective
object specifies the framework for an objective function
for numerical optimization.
Active bindings
objective_name
A
character
, a label for the objective function.fixed_arguments
A
character
, the names of the fixed arguments (if any).seconds
A
numeric
, a time limit in seconds. Computations are interrupted prematurely ifseconds
is exceeded.No time limit if
seconds = Inf
(the default).Note the limitations documented in
setTimeLimit
.hide_warnings
Either
TRUE
to hide warnings when evaluating the objective function, orFALSE
(default) if not.verbose
Either
TRUE
(default) to print status messages, orFALSE
to hide those.npar
An
integer
vector, defining the length of each target argument.
Methods
Method new()
Creates a new Objective
object.
Usage
Objective$new(objective, target, npar, ...)
Arguments
objective
A
function
to be optimized thathas at least one argument that receives a
numeric
vector
and returns a single
numeric
value.
target
A
character
, the argument names ofobjective
that get optimized. These arguments must receive anumeric
vector
.npar
A
integer
of the same length astarget
, defining the length of the respectivenumeric
vector
argument....
Optionally additional arguments to
objective
that are fixed during the optimization.
Method set_argument()
Set a fixed function argument.
Method get_argument()
Get a fixed function argument.
Method remove_argument()
Remove a fixed function argument.
Method validate()
Validate an Objective
object.
Method evaluate()
Evaluate the objective function.
Method print()
Print details of the Objective
object.
Examples
### define log-likelihood function of Gaussian mixture model
llk <- function(mu, sd, lambda, data){
sd <- exp(sd)
lambda <- plogis(lambda)
sum(log(lambda * dnorm(data, mu[1], sd[1]) + (1 - lambda) * dnorm(data, mu[2], sd[2])))
}
### the log-likelihood function is supposed to be optimized over the first
### three arguments, the 'data' argument is constant
objective <- Objective$new(
objective = llk, target = c("mu", "sd", "lambda"), npar = c(2, 2, 1),
data = faithful$eruptions
)
### evaluate the objective function at 1:5 (1:2 is passed to mu, 3:4 to sd,
### and 5 to lambda)
objective$evaluate(1:5)
#> [1] -1069.623