Learn R Programming

hmsr

The HMS (Hierarchic Memetic Strategy) is a composite global optimization strategy consisting of a multi-population evolutionary strategy and some auxiliary methods. The HMS makes use of a dynamically-evolving data structure that provides an organization among the component populations. It is a tree with a fixed maximal height and variable internal node degree. Each component population is governed by a particular evolutionary engine. This package provides a simple R implementation with examples of using different genetic algorithms as the population engines.

Relevant literature

  • J. Sawicki, M. Łoś, M. Smołka, R. Schaefer. Understanding measure-driven algorithms solving irreversibly ill-conditioned problems. Natural Computing 21:289-315, 2022. doi: 10.1007/s11047-020-09836-w

This package does not provide an implementation of a Simple Genetic Algorithm to be used in demes. However the default configuration uses SGA implementation from GA package available on CRAN.

Installation

You can install the released version of hmsr from CRAN:

install.packages("hmsr")

or the development version from this repository:

install.packages("devtools")
library("devtools")
devtools::install_github("WojtAcht/hms")

Usage

Default configuration

To run the HMS with a default configuration the only arguments that have to be provided are the bounds of the domain: lower and upper

library(hmsr)
f <- function(x)  (x^2+x)*cos(x)
HMS <- hms(fitness = f, lower = -10, upper = 10)
HMS@best_solution
#> [1] 6.560544

Custom configuration

There are number of parameters used for strategy configuration. The sample below runs HMS on the Eggholder function - a benchmark for optimization problems.

library(smoof)
Eggholder <- smoof::makeEggholderFunction()
lower <- rep(-512, 2)
upper <- rep(512, 2)
sigma <- list(rep(200, 2), rep(100, 2), rep(50, 2))
ga_config <- list(
    list(
        pmutation = 0.4,
        mutation = rtnorm_mutation(lower, upper, sigma[[1]])
    ),
    list(
        pmutation = 0.2,
        mutation = rtnorm_mutation(lower, upper, sigma[[2]])
    ),
    list(
        pmutation = 0.2,
        mutation = rtnorm_mutation(lower, upper, sigma[[3]])
    )
)
HMS <- hms(
    fitness = Eggholder,
    minimize = TRUE,
    tree_height = 3,
    lower = lower,
    upper = upper,
    run_metaepoch = ga_metaepoch(ga_config),
    population_sizes = c(50, 30, 15),
    sigma = sigma,
    gsc = gsc_max_fitness_evaluations(20000),
    sc = sc_max_metric(euclidean_distance, c(40, 20, 10)),
    lsc = lsc_metaepochs_without_improvement(15),
    monitor_level = "none",
    with_gradient_method = TRUE
)

To see the structure of demes tree at the end of the evaluation, use printTree function

printTree(HMS)
#> f(476.52, 431.37) = 943.07 evaluations: 7675 A
#> ├-- spr: (507.90, 402.59); f(480.26, 430.74) = 956.02 evaluations: 1955 A
#> |   └-- spr: (507.49, 400.58); f(482.35, 432.88) = 956.92 evaluations: 879 A
#> ├-- spr: (479.43, 436.05); f(478.82, 429.39) = 954.55 evaluations: 2341 A
#> |   └-- spr: (480.59, 430.58); f(482.35, 432.88) = 956.92 evaluations: 1075 A
#> └-- spr: (365.96, 500.10); f(507.31, 401.35) = 938.19 evaluations: 4293 A
#>     └-- spr: (441.83, 454.04); ***f(512.00, 404.23) = 959.64*** evaluations: 2000 A

To display a plot showing how best solution has changed during the evaluation use plot(HMS).

Copy Link

Version

Install

install.packages('hmsr')

Monthly Downloads

160

Version

1.0.1

License

MIT + file LICENSE

Maintainer

Wojciech Achtelik

Last Published

October 25th, 2023

Functions in hmsr (1.0.1)

plot,hms-method

Plot method for "hms" class.
saveMetaepochsPopulations,hms-method

saveMetaepochsPopulations
printBlockedSprouts

printBlockedSprouts method for "hms" class.
printTree,hms-method

printTree method for class "hms".
plotPopulation

plotPopulation method for "hms" class.
printTree

printTree method for class "hms".
rtnorm_mutation

Factory function that creates normal mutation function
sc_max_metric

Default sprouting condition based on given metric.
show,hms-method

Show method for class "hms".
print,hms-method

Print method for class "hms".
printBlockedSprouts,hms-method

printBlockedSprouts method for "hms" class.
saveMetaepochsPopulations

saveMetaepochsPopulations method for "hms" class.
summary,hms-method

Summary method for class "hms".
gsc_metaepochs_count

Factory function for a global stopping condition that stops the computation after given number of metaepochs.
hms-class

A S4 class representing a result of hms.
MetaepochSnapshot-class

A S4 class representing a snapshot of one metaepoch.
default_run_gradient_method

Function that runs gradient method for one deme. Wrapper function for stats::optim.
ga_metaepoch

Function that runs one GA metaepoch. Wrapper function for GA::ga.
euclidean_distance

Euclidean distance
ecr_metaepoch

Function that runs one ecr metaepoch. Wrapper function for ecr::ecr.
hms

Maximization (or minimization) of a fitness function using Hierarchic Memetic Strategy.
gsc_max_fitness_evaluations

Factory function for a global stopping condition that stops the computation after fitness function has been evaluated given number of times.
gsc_trivial

Factory function for a global stopping condition that never stops the computation. It results in hms running until there are no more active demes.
plotActiveDemes,hms-method

plotActiveDemes method for "hms" class.
plotActiveDemes

plotActiveDemes method for "hms" class.
manhattan_distance

Manhattan distance
plotPopulation,hms-method

plotPopulation method for "hms" class.
lsc_max_fitness_evaluations

Factory function for a local stopping condition that stops a deme after given number of fitness function evaluations has been made in that deme.
lsc_metaepochs_without_improvement

Factory function for a local stopping condition that stops a deme after given number of consecutive metaeopochs without an improvement of the best solution found in that deme.
lsc_metaepochs_without_active_child

Factory function for a local stopping condition that stops a deme after given number of metaepochs have past since last metaepoch during which this deme had an active child.
lsc_trivial

Factory function for a trivial local stopping condition that lets a deme be active forever. It is usually used in the root of a hms tree.