| Objective {optimizeR} | R Documentation |
Specify objective function
Description
The Objective object specifies the framework for an objective function
for numerical optimization.
Value
An Objective object.
Active bindings
objective_nameA
character, a label for the objective function.fixed_argumentsA
character, the names of the fixed arguments (if any).secondsA
numeric, a time limit in seconds. Computations are interrupted prematurely ifsecondsis exceeded.No time limit if
seconds = Inf(the default).Note the limitations documented in
setTimeLimit.hide_warningsEither
TRUEto hide warnings when evaluating the objective function, orFALSE(default) if not.verboseEither
TRUE(default) to print status messages, orFALSEto hide those.nparAn
integervector, defining the length of each target argument.output_templateA template of the expected output value, used for the
validatemethod.
Methods
Public methods
Method new()
Creates a new Objective object.
Usage
Objective$new(f, target = NULL, npar, ...)
Arguments
fA
functionto be optimized.It is expected that
fhas at least onenumericargument.Further, it is expected that the return value of
fis of the structurenumeric(1), i.e. a singlenumericvalue (although this can be altered via theoutput_templatefield).targetA
character, the argument name(s) offthat get optimized.All target arguments must receive a
numericvector.Can be
NULL(default), then it is the first argument off.nparA
integerof the same length astarget, defining the length of the respectivenumericvectorargument....Optionally additional arguments to
fthat are fixed during the optimization.
Returns
A new Objective object.
Method set_argument()
Set a fixed function argument.
Usage
Objective$set_argument(..., overwrite = TRUE, verbose = self$verbose)
Arguments
...Optionally additional arguments to
fthat are fixed during the optimization.overwriteEither
TRUE(default) to allow overwriting, orFALSEif not.verboseEither
TRUE(default) to print status messages, orFALSEto hide those.
Returns
Invisibly the Objective object.
Method get_argument()
Get a fixed function argument.
Usage
Objective$get_argument(argument_name, verbose = self$verbose)
Arguments
argument_nameA
character, a name of an argument forf.verboseEither
TRUE(default) to print status messages, orFALSEto hide those.
Returns
The argument value.
Method remove_argument()
Remove a fixed function argument.
Usage
Objective$remove_argument(argument_name, verbose = self$verbose)
Arguments
argument_nameA
character, a name of an argument forf.verboseEither
TRUE(default) to print status messages, orFALSEto hide those.
Returns
Invisibly the Objective object.
Method validate()
Validate an Objective object.
Usage
Objective$validate(.at)
Arguments
.atA
numericof lengthsum(self$npar), the values for the target arguments written in a single vector.
Returns
Invisibly the Objective object.
Method evaluate()
Evaluate the objective function.
Usage
Objective$evaluate(.at, .negate = FALSE, ...)
Arguments
.atA
numericof lengthsum(self$npar), the values for the target arguments written in a single vector..negateEither
TRUEto negate thenumericreturn value off, orFALSE(default) else....Optionally additional arguments to
fthat are fixed during the optimization.
Returns
The objective value.
Method print()
Print details of the Objective object.
Usage
Objective$print()
Returns
Invisibly the Objective object.
Method clone()
The objects of this class are cloneable with this method.
Usage
Objective$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
### 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)