Giter Site home page Giter Site logo

gramevol's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.