MyNixOS website logo
Description

Grammatical Evolution for R.

A native R implementation of grammatical evolution (GE). GE facilitates the discovery of programs that can achieve a desired goal. This is done by performing an evolutionary optimisation over a population of R expressions generated via a user-defined context-free grammar (CFG) and cost function.

CRAN_Status_Badge Downloads

gramEvol: Grammatical Evolution for R

This package includes source code and documentation of gramEvol: Grammatical Evolution for R.

gramEvol implements grammatical evolution (GE) in native R syntax. It allows discovering programs that can achieve a desired goal, by performing an evolutionary optimization over a population of R expressions generated via a user-defined grammar. Functions are provided for creating and manipulating context-free grammars (CFGs), random search, exhaustive search, and evolutionary optimization. Users are only required to define their program structure via a grammar, and a cost function to evaluate the fitness of each program.

Installation

You can install this package from CRAN:

install.packages("gramEvol")

You can install the latest version from Github:

if (!require("devtools")) install.packages("devtools")
devtools::install_github("fnoorian/gramEvol")

Usage

A tutorial on implementing GE programs is included in the package's vignette (PDF version).

More information regarding GE and its application in parameter optimization can be found in this paper in the Journal of Statistical Software.

Example

This example implements the Kepler law rediscovery problem, as discussed in section 3.1 of the vignette.

library("gramEvol")

# grammar definition for generic symbolic regression
grammarDef <- CreateGrammar(list(
  expr  = grule(op(expr, expr), func(expr), var),
  func  = grule(sin, cos, log, sqrt),
  op    = grule(`+`, `-`, `*`), # define unary operators
  var   = grule(distance, distance^n, n),
  n     = gvrule(1:4) # this is shorthand for grule(1,2,3,4)
))

# cost function and data
planets <- c("Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus")
distance <- c(0.72, 1.00, 1.52, 5.20, 9.53, 19.10)
period <- c(0.61, 1.00, 1.84, 11.90, 29.40, 83.50)

SymRegCostFunc <- function(expr) {
     result <- eval(expr)
  
       if (any(is.nan(result)))
           return(Inf)
  
       return (mean(log(1 + abs(period - result))))
}

# run GE
ge <- GrammaticalEvolution(grammarDef, SymRegCostFunc, iterations = 50)
print(ge)

# use the best expression
best.expression <- ge$best$expression
print(ge$best$expressions)
print(data.frame(distance, period, Kepler = sqrt(distance^3), GE = eval(best.expression)))

Contact Information

Release and Development

The latest release and developmental versions of this package are available on: https://github.com/fnoorian/gramEvol

License

All files in this package, including the documentation and vignettes, are distributed under GNU GPL v2.0 or later license. For full terms of this license visit https://www.gnu.org/licenses/gpl-2.0.html.

Metadata

Version

2.1-4

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows