Objective | R Documentation |
The Objective
object specifies the framework for an objective function
for numerical optimization.
An Objective
object.
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 if seconds
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,
or FALSE
(default) if not.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
npar
An integer
vector, defining the length of each target argument.
output_template
A template of the expected output value, used for the validate
method.
new()
Creates a new Objective
object.
Objective$new(f, target = NULL, npar, ...)
f
A function
to be optimized.
It is expected that f
has at least one numeric
argument.
Further, it is expected that the return value of f
is of the
structure numeric(1)
, i.e. a single numeric
value (although
this can be altered via the output_template
field).
target
A character
, the argument name(s) of f
that get optimized.
All target arguments must receive a numeric
vector
.
Can be NULL
(default), then it is the first argument of f
.
npar
A integer
of the same length as target
, defining the length
of the respective numeric
vector
argument.
...
Optionally additional arguments to f
that are fixed during
the optimization.
A new Objective
object.
set_argument()
Set a fixed function argument.
Objective$set_argument(..., overwrite = TRUE, verbose = self$verbose)
...
Optionally additional arguments to f
that are fixed during
the optimization.
overwrite
Either TRUE
(default) to allow overwriting, or FALSE
if not.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
Invisibly the Objective
object.
get_argument()
Get a fixed function argument.
Objective$get_argument(argument_name, verbose = self$verbose)
argument_name
A character
, a name of an argument for f
.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
The argument value.
remove_argument()
Remove a fixed function argument.
Objective$remove_argument(argument_name, verbose = self$verbose)
argument_name
A character
, a name of an argument for f
.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
Invisibly the Objective
object.
validate()
Validate an Objective
object.
Objective$validate(.at)
.at
A numeric
of length sum(self$npar)
, the values for the target
arguments written in a single vector.
Invisibly the Objective
object.
evaluate()
Evaluate the objective function.
Objective$evaluate(.at, .negate = FALSE, ...)
.at
A numeric
of length sum(self$npar)
, the values for the target
arguments written in a single vector.
.negate
Either TRUE
to negate the numeric
return value of
f
, or FALSE
(default) else.
...
Optionally additional arguments to f
that are fixed during
the optimization.
The objective value.
print()
Print details of the Objective
object.
Objective$print()
Invisibly the Objective
object.
clone()
The objects of this class are cloneable with this method.
Objective$clone(deep = FALSE)
deep
Whether to make a deep clone.
### define log-likelihood function of Gaussian mixture model
llk <- function(mu, sd, lambda, data) {
sd <- exp(sd)
lambda <- plogis(lambda)
cluster_1 <- lambda * dnorm(data, mu[1], sd[1])
cluster_2 <- (1 - lambda) * dnorm(data, mu[2], sd[2])
sum(log(cluster_1 + cluster_2))
}
### the log-likelihood function is supposed to be optimized over the first
### three arguments, the 'data' argument is constant
objective <- Objective$new(
f = 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.