https://github.com/mailund/dynprog
DSL for dynamic programming
https://github.com/mailund/dynprog
Last synced: 6 months ago
JSON representation
DSL for dynamic programming
- Host: GitHub
- URL: https://github.com/mailund/dynprog
- Owner: mailund
- Created: 2018-03-01T11:02:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-11T10:29:54.000Z (over 5 years ago)
- Last Synced: 2024-11-29T04:27:41.653Z (6 months ago)
- Language: R
- Size: 94.7 KB
- Stars: 17
- Watchers: 4
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)library(dynprog)
```# dynprog - Domain-specific language for specifying dynamic programming computations
[](https://www.gnu.org/licenses/gpl-3.0.en.html)
[](http://www.repostatus.org/#active)
[](https://www.tidyverse.org/lifecycle/#maturing)
[)`-orange.svg)](/commits/master)
[](commits/master)
[](https://travis-ci.org/mailund/dynprog)
[](https://ci.appveyor.com/project/mailund/dynprog)
[](https://codecov.io/github/mailund/dynprog?branch=master)
[](https://coveralls.io/github/mailund/dynprog?branch=master)
[](https://cran.r-project.org/package=dynprog)
[](https://cran.r-project.org/package=dynprog)
[](https://cran.r-project.org/)The `dynprog` package implements a small domain-specific language for specifying dynamic programming algorithms. It allows you to specify a computation as a recursion and it will then use this recursion to fill out a table and return it to you.
As a very simple example, you can specify a dynamic programming computation of [Fibonnaci numbers](https://en.wikipedia.org/wiki/Fibonacci_number) using
```{r}
fibs <- {
F[1] <- 1
F[2] <- 1
F[n] <- F[n - 1] + F[n - 2]
} %where% {
n <- 1:10
}fibs
```As shown in the example, the expression consists of two parts, the first, before the `%where%` operator, describes a recursion and the second, after the `%where%` operator, the range the variable `n` should iterate over.
Formally, a `dynprog` expression is on the form
```
DYNPROG_EXPR ::= RECURSIONS '%where%' RANGES
RECURSIONS ::= '{' PATTERN_ASSIGNMENTS '}'
RANGES ::= '{' RANGES_ASSIGNMENTS '}'
```where `PATTERN_ASSIGNMENTS` describe the recursion and `RANGES_ASSIGNMENTS` the variables to recurse over and the values those variables should take.
Ranges are the simplest of the two.
```
RANGES_ASSIGNMENTS ::= RANGES_ASSIGNMENT
| RANGES_ASSIGNMENT ';' RANGES_ASSIGNMENTS
RANGES_ASSIGNMENT ::= RANGE_INDEX '<-' RANGE_EXPRESSION
```where `RANGE_INDEX` is just a variable and `RANGE_EXPRESSION` an expression that should evaluate to a list or vector. You can specify more than one variable if these are separated by `;` or newlines (the grammar only says `;` but I am not too formal here). An example with two range variables, of computing the [edit distance](https://en.wikipedia.org/wiki/Edit_distance) between two strings, is shown below.
The actual recursions are specified in `PATTERN_ASSIGNMENTS`:
```
PATTERN_ASSIGNMENTS ::= PATTERN_ASSIGNMENT
| PATTERN_ASSIGNMENT ';' PATTERN_ASSIGNMENTS
```where
```
PATTERN_ASSIGNMENT ::= PATTERN '<-' RECURSION
| PATTERN '<-' RECURSION '?' CONDITION
```
```
PATTERN ::= TABLE '[' INDICES ']'
```Here, `TABLE` is just a variable and `INDICES` should be a comma-separated lists of values/expressions or variables. When recursions are evaluated, the range variables are tested against the patterns. If a pattern contains a range variable as a variable, the variable is free to take any value, but if it takes on a value, the range variable must have that value.
To the right of the assignment in `PATTERN_ASSIGNMENTS` we have `RECURSION`, which cna be any R expression and an optional `CONDITION`, which should be an R expression that evaluates to a boolean value.
The semantics of the recursions are that the patterns are tested in the order they are provided, and if both the patterns match the range variables and the condition evaluates to `TRUE`, then the entry in the table will get assigned the result of evaluating `RECURSION`.
For more information, see
> Mailund, T. (2018) [Domain-Specific Languages in R](https://amzn.to/2DRmFXb),
> Apress. ISBN 1484235878## Installation
You can install the released version of `dynprog` from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("dynprog")
```and the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("mailund/dynprog")
```
## ExampleYou can compute the [edit-distance](https://en.wikipedia.org/wiki/Edit_distance) between two strings like this:
```{r}
x <- c("a", "b", "c")
y <- c("a", "b", "b", "c")
edit <- {
E[1,j] <- j - 1
E[i,1] <- i - 1
E[i,j] <- min(
E[i - 1,j] + 1,
E[i,j - 1] + 1,
E[i - 1,j - 1] + (x[i - 1] != y[j - 1])
)
} %where% {
i <- 1:(length(x) + 1)
j <- 1:(length(y) + 1)
}edit
```