met_optimize: General-Purpose Optimization

View source: R/met_optimize.R

met_optimizeR Documentation

General-Purpose Optimization

Description

Performs continuous optimization using metaheuristic or gradient-based optimization algorithms.

Usage

met_optimize(
  fn,
  optimizer = optimizer_pso(),
  lower,
  upper,
  gr = NULL,
  initial = NULL,
  seed = NULL,
  verbose = TRUE,
  ...
)

Arguments

fn

Objective function to be minimized. It must accept a numeric vector as its first argument and return a single numeric value.

optimizer

Optimizer object created by functions such as optimizer_pso(), optimizer_sboa(), optimizer_sgd(), or optimizer_adam().

lower

Numeric vector of lower bounds.

upper

Numeric vector of upper bounds.

gr

Optional gradient function. Required for gradient-based optimizers such as optimizer_sgd() and optimizer_adam(). It must accept a numeric vector as its first argument and return a numeric vector of the same length.

initial

Optional numeric vector of initial parameter values. If NULL, a random initial point is generated within the given bounds for gradient-based optimizers.

seed

Optional random seed.

verbose

Logical. If TRUE, progress information is printed.

...

Additional arguments passed to fn and, when applicable, gr.

Value

An object of class "met_optimize_result".

Examples

sphere <- function(x) sum(x^2)

result <- met_optimize(
  fn = sphere,
  optimizer = optimizer_pso(pop_size = 10, max_iter = 20),
  lower = rep(-5, 2),
  upper = rep(5, 2),
  seed = 123,
  verbose = FALSE
)

result

grad_sphere <- function(x) 2 * x

result_adam <- met_optimize(
  fn = sphere,
  gr = grad_sphere,
  optimizer = optimizer_adam(learning_rate = 0.1, epochs = 20),
  lower = rep(-5, 2),
  upper = rep(5, 2),
  initial = rep(4, 2),
  seed = 123,
  verbose = FALSE
)

result_adam

metANN documentation built on May 16, 2026, 1:06 a.m.