Source

nmpuzzle / Search.lhs

Full commit
\documentclass[11pt,draft,a4paper]{article}
\usepackage[draft=false]{hyperref}
\usepackage{xcolor}
\hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}
\usepackage{amsmath}
\usepackage[obeyDraft]{todonotes}
\usepackage[nottoc]{tocbibind}
%include polycode.fmt
%format `on`      = "\operatorname{on}"
%format <$>       = "\, \langle\$\rangle \,"

%format q0        = "q_0"
%format qt        = "q_t"
%format at        = "a_t"
%format nt        = "n_t"

%format expand    = "\delta"
%format nexpanded = "n_{\delta}s"
%format goalp     = "\Phi_F"
\renewcommand{\abstractname}{Summary}
\begin{document}
\title{AI Tree |Search| Library}
\author{Rhys !}
\date{April 2014}
\maketitle

\begin{abstract}
A Haskell\cite{haskell} module for AI tree search is implemented and commented
upon.
\end{abstract}

\tableofcontents

\listoftodos

\section{Setup}
This is the |Search| module, for generic AI tree search. Here we export some
constructors and functions for other code to use.

\begin{code}
module Search  ( FrontierApp
               , Path
               , SearchResult
               , mkNode, mkRoot
               , stateAt, prevStates
               , actions
               , breadthFirst
               , depthFirst
               , aStar
               , greedyBF
               , search) where
\end{code}

Next, we import a few base Haskell modules that contain some useful
functions.\todo[inline]{Acknowledgments: Data.List.Ordered and
Data.List.Split.}

\begin{code}
import Control.Applicative ((<$>))
import Data.Function
import Data.List (sortBy)
import Data.List.Ordered (mergeBy)
\end{code}

\section{The |Path| Algebraic Data Type}
All the search algorithms implemented represent the search space as a tree of
states branching from a root node. The |Path| algebraic data type
(ADT)\footnote{Not to be confused with \emph{abstract} data type. Further
details in \cite{adt}.} is used to represent a node on these trees. The type is
parametric over two other types: |a| to record actions or transformations, and
|q|\footnote{From the set of states $Q$ in (non)deterministic finite state
automata (NFA)\cite{nfa}.} to record state.

The type stores a list of actions and states in `reverse chronological order',
ie. with the root state at the end of the list. Since there is no action
associated with the initial state (|q0|), the actions list will always have one
less element than the states list.

\begin{code}
data Path a q =  Path  { actions :: [a]
                       , states :: [q] }
\end{code}

Next, a couple of constructor-like functions for |Path| are defined; one to
create a root node and one to create a child node.
\begin{code}
mkRoot :: q -> Path a q
mkRoot q0 = Path [] [q0]

mkNode :: Path a q -> a -> q -> Path a q
mkNode (Path as qs) at qt = Path (at:as) (qt:qs)
\end{code}

\section{Search Methods}\todo[inline]{Custom strategies.}
The difference between the search methods implemented is in how they order the
frontier\footnote{The frontier is the list of nodes yet to be examined.}, and
thus the order of node examination. In this implementation, search methods are
defined as a frontier modification function. All methods are guaranteed to be
stable \cite{stable}, ie. the relative order of the nodes provided by the
|expand| function will be preserved in the frontier.

To shorten type signatures, a |FrontierApp| alias is declared. This alias
is polymorphic over the action |a| and state |q| types, like |Path|, and
represents a function that combines the current frontier (hereupon |frontier|)
and a list of new nodes (hereupon |nexpanded|\footnote{From \emph{n}ode,
$\delta$ (see section \ref{search}) and the common Haskell suffix \emph{s} for
lists.}) to create a new frontier.

\begin{code}
type FrontierApp a q = [Path a q] -> [Path a q] -> [Path a q]
\end{code}

\subsection{Uninformed Methods}
The two uninformed methods are trivial to define: a breadth first search will
add new nodes to the end of the list, while a depth-first search will add them
to the start of the list.

\begin{code}
breadthFirst, depthFirst :: FrontierApp a q
breadthFirst = (++)
depthFirst = flip (++)
\end{code}

\subsection{Informed Methods}
The two informed methods are slightly more complex, since they take additional
factors into account.

Firstly, greedy breadth first search (GBFS). GBFS takes only one additional
factor into account, namely an estimated distance to the a goal node, typically
designated $h(n)$ where $n$ represents the current node. |greedyBF| takes a
heuristic function of type |q -> Integer|, and returns a |FrontierApp| that
sorts the frontier using that function.

\begin{code}
greedyBF :: (Ord m, Num m) => (q -> m) -> FrontierApp a q
greedyBF h frontier nexpanded = mergeUSOn h' nexpanded frontier where
  h' = h . stateAt
\end{code}

A* search takes both $h(n)$ and a known path cost $g(n)$ into account. |g|
accepts the action type |a| instead of the state |q| to allow for simple state
sharing, if the caller desires\footnote{Obviously, the function could be
broadened to |q -> a -> Integer| if required in a later version.}.

\begin{code}
-- TODO: presorted version
aStar :: (Ord m, Num m) => (a -> m) -> (q -> m) -> FrontierApp a q
aStar g h frontier nexpanded = mergeUSOn f nexpanded frontier where
    f (Path (at:_) (qt:_)) = h qt + g at
\end{code}

\section{Running a Search}\label{search}
Using the above definitions, the |search| function is fairly trivial, simply
expanding the head of the frontier until a goal state is reached or the frontier
is emptied.

|search| takes a |FrontierApp| value |app|, expansion function
|expand|\footnote{\label{delta}In reference to the transition functions of
NFA.}, a goal predicate |goalp|\footnote{From set-builder notation
$\Phi$\cite{set-notation} and the NFA goal set $F$\cite{nfa}.} and an initial
state |q0|\footnote{ibid.}. The function returns a |Maybe| value because the
goal state may not be achievable.

\begin{code}
type SearchResult a q = (Int, Maybe (Path a q))

search :: (Eq q) =>
    FrontierApp a q ->
    (Path a q -> [Path a q]) ->
    (q -> Bool) ->
    q ->
    SearchResult a q
\end{code}

The root of the search tree is defined as |q0| and the recursive function |go|
is called with the root-populated frontier.\todo[inline]{Note counter.}

\begin{code}
search app expand goalp q0 = go 0 [mkRoot q0] where
      go procCount (nt@(Path as (qt:qs)):ns)
\end{code}

For each head of the frontier, |nt|, |go|:

\begin{itemize}
    \item Makes a goal check and returns |nt| if |goalp| is satisfied
\begin{code}
          | goalp qt      = (1 + procCount + length ns, Just nt)
\end{code}
          or
    \item Checks if |nt| is a duplicate state and ignores it if so
\begin{code}
          | qt `elem` qs  = go (procCount + 1) ns
\end{code}
          or
    \item Expands the node with |expand| and adds the resulting nodes to the
          frontier using |app|.
\begin{code}
          | otherwise     = go (procCount + 1) (app ns (expand nt))
\end{code}
\end{itemize}

If the frontier has been emptied, there is no solution and |Nothing| is
returned.

\begin{code}
      go procCount [] = (procCount, Nothing)
\end{code}

\appendix
\section{Utility Definitions}
\begin{itemize}
% The following code is hidden from the TeX output, since it's just for testing.
%if False
    \item A |Show| instance so |Path| values can be pretty-printed.
\begin{code}
instance (Show a, Show q) => Show (Path a q) where
    show (Path as qs) = showl as qs where
      showl  []       (q0:[])  =  show q0
      showl  (at:as)  (qt:qs)  =  showl as qs ++ " " ++
                                  show at ++ " -> " ++ show qt
\end{code}
%endif
    \item Sorting and merging ordered lists on a transform. The underlying
          |sortBy| and |mergeBy| functions are stable, and thus are all the
          functions below.\todo[inline]{Check sort performance.}
\begin{code}
sortOn :: (Ord b) => (a -> b) -> [a] -> [a]
sortOn f = sortBy (compare `on` f)

mergeOn :: (Ord b) => (a -> b) -> [a] -> [a] -> [a]
mergeOn f = mergeBy (compare `on` f)

mergeUSOn :: (Ord b) => (a -> b) -> [a] -> [a] -> [a]
mergeUSOn f uxs = mergeOn f (sortOn f uxs)
\end{code}

  \item |stateAt| returns the state value of a node and |prevStates| returns all
        previous states from the current node to the root node.
\begin{code}
stateAt :: Path a q -> q
stateAt = head . states

prevStates :: Path a q -> [q]
prevStates = tail . states
\end{code}
\end{itemize}

\begin{thebibliography}{9}

\bibitem{haskell}
  HaskellWiki,
  `The Haskell Programming Language'
  \url{http://www.haskell.org/haskellwiki/Haskell}

\bibitem{adt}
  HaskellWiki,
  `Algebraic data type'
  \url{http://www.haskell.org/haskellwiki/Algebraic_data_type}

\bibitem{set-notation}
  Wikipedia,
  `Set-builder notation'
  \url{https://en.wikipedia.org/wiki/Set-builder_notation}

\bibitem{nfa}
  Almeida M, Moreira N, Reis R,
  `On the performance of automata minimization algorithms',
  p. 3
  \url{http://www.dcc.fc.up.pt/dcc/Pubs/TReports/TR07/dcc-2007-03.pdf}

\bibitem{stable}
  Wikipedia,
  `Sorting algorithm',
  section `Stability'
  \url{https://en.wikipedia.org/wiki/Sorting_algorithm#Stability}

\end{thebibliography}
\end{document}