backtracker | R Documentation |
backtracker
creates a binary search program that can be started by calling the $searchNext
function
It walks a binary tree depth first. For all left nodes choiceLeft
is evaluated, for all right nodes
choiceRight
is evaluated. A solution is found if isSolution
evaluates to TRUE
.
In that case $searchNext
will return all variables in the search environment in a list
If isSolution
evaluates to NULL it will continue to search deaper.
If isSolution
evaluates to FALSE
it stops at the current node and goes up the next search node
backtracker(
isSolution,
choiceLeft,
choiceRight,
list = NULL,
maxdepth = Inf,
maxduration = Inf,
...
)
isSolution |
|
choiceLeft |
|
choiceRight |
|
list |
|
maxdepth |
|
maxduration |
|
... |
named variables that will be added to the search environment |
$searchNext(..., VERBOSE=FALSE)
Search next solution, can
be called repeatedly until there is no solution left. Named variables will be
added to the search environment, this feature can be used to direct the search
in subsequent calls to searchNext
. VERBOSE=TRUE will print all
intermediate search steps and results. It can be used to debug the expressions
in the backtracker
$searchAll(..., VERBOSE=FALSE)
Return all solutions as a list
$reset()
Resets the backtracker
to its initial state.
backtracker object, see Methods for a description of the methods
bt <- backtracker( isSolution= {
if (y == 0) return(TRUE)
if (x == 0) return(FALSE)
}
, choiceLeft = { x <- x - 1; y <- y}
, choiceRight = { y <- y - 1; x <- x}
# starting values for x and y
, x=2
, y=1
)
bt$searchNext(VERBOSE=TRUE)
bt$searchNext(VERBOSE=TRUE)
# next search will return NULL because there is no more solution
bt$searchNext()
bt$reset()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.