astar: Runs the A* algorithm.

Description Usage Arguments Value Examples

View source: R/astar.R

Description

The search space is defined by a list passed thorugh params.

Usage

1
astar(start, params, max.iters = Inf, ...)

Arguments

start

initial node in the search space

params

a list containing functions necessary to define the search problem: heuristic, distance, neighbours and is_feasible (see example below)

max.iters

maximum number of iterations to run - for debuggin purposes only, defaults to Inf

...

additional parameters passed to each of the functions defined in params

Value

A list containing two components solution and history.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Searching for a path from (0,0), to (0,3) with three points blocked along the way

dirs <- list(c(1,0),c(0,1),c(0,-1),c(-1,0))
params <- list(
 heuristic = function(el, goal) sum((goal-el)^2),
 distance = function(el, parent, parentDistance, ...) parentDistance + 1,
 neighbours = function(el, ...) setdiff( lapply(dirs, function(d) el+d), 
                                         list(c(0,2), c(1,1), c(-1,1))),
 is_feasible = function(node, goal){ identical(node, goal) }
 )

 res <- astar(c(0,0), params, goal = c(0,3))

pzawistowski/astar documentation built on May 26, 2019, 11:34 a.m.