README.md

seqopt

Travis build
status AppVeyor build
status Coverage
status

seqopt is an R package for finding optimal sequences with dynamic programming. Given a list of timepoints and corresponding lists of possible states, seq_opt() efficiently finds an optimal state sequence that minimises an arbitrary transition cost function.

Installation

if (!require(devtools)) install.packages("devtools")
devtools:install_github("pmcharrison/seqopt")

Examples

library(seqopt)

# 3 time points, each of which can take values from 1 to 5
x <- lapply(1:3, function(x) 1:5)
print(x)
#> [[1]]
#> [1] 1 2 3 4 5
#> 
#> [[2]]
#> [1] 1 2 3 4 5
#> 
#> [[3]]
#> [1] 1 2 3 4 5

# Define two cost functions.
# The first enforces always-increasing state values.
# The second prefers small differences between successive states.
cost_funs <- list(
  cost_fun(context_sensitive = TRUE, 
           function(a, b) if (b <= a) Inf else 0),
  cost_fun(context_sensitive = TRUE, 
           function(a, b) abs(b - a))
)

# seq_opt finds the optimal sequence satisfying these constraints.
res <- seq_opt(x, cost_funs)
print(unlist(res))
#> [1] 1 2 3

# Now add a cost function preferring large state values.
cost_funs[[3]] <- cost_fun(context_sensitive = FALSE,
                           function(a) - a)
res <- seq_opt(x, cost_funs)
print(unlist(res))
#> [1] 3 4 5


pmcharrison/seqopt documentation built on May 22, 2019, 6:35 p.m.