Learn R Programming

editrules (version 2.2-0)

backtracker: Backtracker: a flexible and generic binary search program

Description

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

Usage

backtracker(isSolution, choiceLeft, choiceRight,
    list = NULL, maxdepth = Inf, maxduration = Inf, ...)

Arguments

Value

backtracker object, see Methods for a description of the methods

Details

Methods{ [object Object],[object Object],[object Object] }

Examples

Run this code
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()

# backtracker also works with iterators
if (require(iterators)){
   it <- iter(bt)
   nextElem(it)
   #nextElem(it)
}

Run the code above in your browser using DataLab