Skip to content

Instantly share code, notes, and snippets.

@jmora
Created July 31, 2011 13:59
Show Gist options
  • Save jmora/1116811 to your computer and use it in GitHub Desktop.
Save jmora/1116811 to your computer and use it in GitHub Desktop.
Heuristic Backtracking - Backtracking that prioritizes branches depending on some heuristic
module HeuristicBacktrac (backtracking) where
import List
hbacktracking::
(partialSolution -> partialSolution -> Ordering) -- heuristic, A* like
-> (partialSolution -> Bool) -- isFinalSolution?
-> (partialSolution -> solution) -- convert
-> (partialSolution -> [partialSolution]) -- next
-> (partialSolution -> Bool) -- isValid?
-> [partialSolution] -- Estado inicial
-> Maybe solution
hbacktracking _ _ _ _ _ [] = Nothing
hbacktracking order isSolution convert next isValid (ps:pss)
| isSolution ps = Just (convert ps)
| otherwise = (hbacktracking order isSolution convert next isValid)
(sortBy order ((filter isValid (next ps)) ++ pss))
@jmora
Copy link
Author

jmora commented Jul 31, 2011

I have some cool pieces of code, specially in haskell, I wrote as an undergraduate. Whenever I find one of them I like to post them here.

I did not test it before posting it, but I guess it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment