View source: R/optimizer_lhs.R
| optimizer_lhs | R Documentation |
A wrapper around optimizer that runs the optimization from
several starting parameter sets obtained by Latin hypercube sampling (see
lhs_sample) within user-specified ranges, and returns the
best result. Three strategies are available:
'starts'num_starts parameter sets are drawn and
each is used as a starting point for optimizer.
'screen'num_samples parameter sets are drawn (a
much finer sample than for 'starts') and only the likelihood is
computed for each of them. The num_best parameter sets with the
largest likelihood are then used as starting points for
optimizer, skipping parameter sets that are adjacent to a
starting point already used (see min_dist).
'agree'As 'screen', but the number of starting
points is not fixed in advance. The num_agree best parameter sets
are optimized first. If fewer than num_agree of the optimizations
end up at the best maximum found so far (within the tolerances in
tol_agree), the next best parameter set is optimized, and so on,
until num_agree optimizations agree on the best maximum or
max_starts optimizations have been run.
The Latin hypercube spreads the samples over the parameter space, but the
samples with the largest likelihood tend to lie close together, in the
basin of attraction of the same maximum. Optimizing from all of them
mostly repeats the same optimization, and agreement among them says little
about whether the global maximum has been found. Therefore, in strategies
'screen' and 'agree' a sample is skipped when it lies within
min_dist of a starting point that has already been used, in every
parameter. Distances are measured on the sampling scale, with the range of
each parameter scaled to [0, 1]. When no samples remain that are far
enough from all starting points used so far, min_dist is halved.
The column min_dist of runs shows the value in force when
each starting point was chosen.
optimizer_lhs(
fun,
lower,
upper,
strategy = c("starts", "screen", "agree"),
num_starts = 10,
num_samples = 100 * length(lower),
num_best = 10,
num_agree = 3,
max_starts = 10 * num_agree,
min_dist = 0.1,
tol_agree = c(0.001, 0.001),
sampling_scale = "transformed",
trparsopt = NULL,
optimmethod = "simplex",
optimpars = c(1e-04, 1e-04, 1e-06, 1000, 0),
num_cycles = 1,
jitter = 0,
lhs_verbose = 1,
starts_file = NULL,
run_optimizations = TRUE,
num_cores = 1,
cl = NULL,
parallel_phase = c("both", "screening", "optimizations"),
packages = NULL,
...
)
fun |
Function to be optimized (maximized). Like in
|
lower |
Vector with the lower bounds of the parameters on the original (untransformed) scale. If the vector is named, the names are used for the columns of the result. |
upper |
Vector with the upper bounds of the parameters on the original (untransformed) scale. |
strategy |
The multi-start strategy: |
num_starts |
Number of Latin hypercube samples that are used as
starting points (strategy |
num_samples |
Number of Latin hypercube samples for which only the
likelihood is computed (strategies |
num_best |
Number of samples with the largest likelihood that are
used as starting points (strategy |
num_agree |
Number of optimizations that must agree on the best
maximum before the search stops (strategy |
max_starts |
Maximum number of optimizations (strategy
|
min_dist |
Minimum distance between starting points (strategies
|
tol_agree |
Vector of two tolerances used to decide whether two
optimization results are the same: 1) absolute tolerance in the maximum
(log)likelihood and 2) absolute tolerance in each of the parameters on the
transformed scale (the scale on which |
sampling_scale |
The scale on which the strata have equal width. Either a single value used for all parameters, or one value per parameter:
|
trparsopt |
Optional initial guess of the parameters to be optimized
(on the transformed scale), as a vector or as a matrix with one parameter
set per row. It is added to the Latin hypercube samples as a candidate
starting point: for strategy |
optimmethod |
The optimization method used from each starting point;
any of the methods of |
optimpars |
Parameters of the optimization as in
|
num_cycles |
Number of cycles of the optimization. When set to Inf, the optimization will be repeated until the result is, within the tolerance, equal to the starting values, with a maximum of 10 cycles. |
jitter |
Perturbation of an initial parameter value when precisely equal to 0.5; this is only relevant when subplex is chosen. The default value is 0, so no jitter is applied. A recommended value when using it is 1E-5. |
lhs_verbose |
Verbose level of the multi-start procedure itself: 0 = no output, >= 1 shows one line per optimization and a summary. |
starts_file |
Optional name of a file to which the starting points are saved, in the order in which they are optimized; see Details. |
run_optimizations |
Whether to run the optimizations. With
|
num_cores |
Number of R processes used for parallel computing. The
default, 1, means no parallel computing. Ignored when |
cl |
Optional cluster, created with |
parallel_phase |
What to compute in parallel: 'both' (the default), 'screening' (only the likelihood of the samples) or 'optimizations' (only the optimizations). |
packages |
Names of packages to attach on the workers, in addition to
DDD, for functions that call functions from these packages without
|
... |
Any other arguments of the function to be optimized, or
settings of the optimization routine, which are passed on to
|
Parallel computing. With num_cores larger than 1, or with a
cluster supplied as cl, the likelihood of the samples is computed
and/or the optimizations are run in parallel (see parallel_phase).
The workers are separate R processes (a PSOCK cluster, which also works on
Windows), not forked processes, so a likelihood function that is itself
parallelized, for example with multiple threads set by an argument passed
through ..., or with its own cluster, keeps working within each
worker. The total number of cores used is then num_cores times the
number used by one likelihood evaluation. Strategies 'starts' and 'screen'
divide all optimizations over the workers: a worker starts the next
optimization as soon as it has finished one. Strategy 'agree' does the
same when the package future is installed: it starts a new optimization
whenever a worker is free and checks agreement after each finished
optimization. When agreement is reached, optimizations that are still
running are abandoned if the cluster was created by optimizer_lhs,
and finished (and included in the results) if the cluster was supplied as
cl, so that it can be used again. Without the package future,
strategy 'agree' runs the optimizations in batches of as many as there are
workers. With strategy 'agree', the number and order of the optimizations
can therefore differ from those without parallel computing. The Latin
hypercube sample, and hence the candidate starting points, are the same
with and without parallel computing. Each optimization on a worker gets
its own random number seed, drawn from the random number generator of the
main R session, so that optimization methods that use random numbers give
the same result whichever worker runs them. The function to be optimized
and the arguments in
... are sent to the workers once. A function defined by the user
that calls functions from packages other than DDD without :: needs
these packages in packages; a function that uses other objects from
the user's workspace needs a cluster set up by the user (with
parallel::clusterExport) and supplied as cl.
Running the optimizations as separate jobs. On a computer
cluster, it can be more efficient to run each optimization as a separate
job (e.g. a job array in SLURM), because such jobs can start as soon as a
single core is free. With starts_file, the starting points are
saved to a file (with saveRDS), in the order in which they would be
optimized: num_starts Latin hypercube samples for strategy
'starts', the num_best best screened samples for strategy 'screen'
and the max_starts best screened samples for strategy 'agree' (all
at least min_dist apart). With run_optimizations = FALSE, the
function stops after saving them. The file contains a list with the
starting points on the original scale (pars, a matrix with one row
per starting point, which can be used as initparsopt of the maximum
likelihood functions) and on the transformed scale (trpars), a data
frame starts with their positions among the samples, whether they
are Latin hypercube samples or the initial guess, their likelihoods and the
value of min_dist when they were chosen, the
data frame samples with all samples, and the settings.
The function can also be called through optimizer, and hence
through all maximum likelihood functions in this package, by supplying a
list with the settings for optimizer_lhs as optimmethod; see
optimizer.
A list containing
par |
The parameters (on the transformed scale) of the best optimization result. |
fvalues |
The best function value found. |
conv |
The convergence code of the best optimization (0 means converged). |
The first three elements are the same as the output of
optimizer, so optimizer_lhs can be used in its place.
In addition the list contains
runs |
A data frame with one row per optimization, in the order in
which they were run, with the row of |
samples |
A data frame with all candidate starting points on the
original scale, their function values and their source ( |
num_agreeing |
The number of optimizations that agree with the best result. |
agreement_reached |
For strategy |
strategy |
The strategy used. |
With run_optimizations = FALSE, par and fvalues are
those of the first starting point, conv is -99 (which the maximum
likelihood functions recognize as 'no optimization was run'), runs is
NULL and the list contains a data frame starts with the
starting points.
Rampal S. Etienne
optimizer, lhs_sample
# A function with a local maximum at (0.25, 0.25) and the global maximum
# at (0.7, 0.6), both on the transformed scale
fun <- function(trparsopt) {
log(0.5 * exp(-sum((trparsopt - c(0.25, 0.25))^2) / 0.02) +
exp(-sum((trparsopt - c(0.7, 0.6))^2) / 0.01))
}
set.seed(1)
out <- optimizer_lhs(fun = fun,
lower = c(0, 0),
upper = c(Inf, Inf),
strategy = 'agree',
num_samples = 100,
num_agree = 3)
out$par
out$runs
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.