| optimizeR-package | R Documentation |
Provides a unified object-oriented framework for numerical optimizers in R. Supports minimization and maximization with any optimizer, optimization over more than one function argument, computation time measurement, and time limits for long optimization tasks.
Maintainer: Lennart Oelschläger oelschlaeger.lennart@gmail.com (ORCID)
Other contributors:
Marius Ötting marius.oetting@uni-bielefeld.de (ORCID) [contributor]
Useful links:
Report bugs at https://github.com/loelschlaeger/optimizeR/issues
### Task: compare minimization with 'stats::nlm' and 'pracma::nelder_mead'
# 1. define objective function and initial values
objective <- TestFunctions::TF_ackley
initial <- c(3, 3)
# 2. get overview of optimizers available in dictionary
optimizer_dictionary$keys
# 3. define 'nlm' optimizer
nlm <- Optimizer$new(which = "stats::nlm")
# 4. define the 'pracma::nelder_mead' optimizer
# (not contained in the dictionary)
nelder_mead <- Optimizer$new(which = "custom")
nelder_mead$definition(
algorithm = pracma::nelder_mead, # optimization function
arg_objective = "fn", # objective function argument name
arg_initial = "x0", # argument name for the initial values
out_value = "fmin", # element for the optimal function value
out_parameter = "xmin", # element for the optimal parameters
direction = "min" # optimizer minimizes
)
# 5. compare the minimization results
nlm$minimize(objective, initial)
nelder_mead$minimize(objective, initial)
## ------------------------------------------------
## Method `Optimizer$minimize`
## ------------------------------------------------
Optimizer$new("stats::nlm")$
minimize(objective = function(x) x^4 + 3*x - 5, initial = 2)
## ------------------------------------------------
## Method `Optimizer$maximize`
## ------------------------------------------------
Optimizer$new("stats::nlm")$
maximize(objective = function(x) -x^4 + 3*x - 5, initial = 2)
## ------------------------------------------------
## Method `Optimizer$optimize`
## ------------------------------------------------
objective <- function(x) -x^4 + 3*x - 5
optimizer <- Optimizer$new("stats::nlm")
optimizer$optimize(
objective = objective, initial = 2, direction = "min"
)
optimizer$optimize(
objective = objective, initial = 2, direction = "max"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.