University of Dar Es Salaam Coict: Department of Computer Science & Eng
University of Dar Es Salaam Coict: Department of Computer Science & Eng
University of Dar Es Salaam Coict: Department of Computer Science & Eng
COICT
Department of Computer Science & Eng.
Lecturer: Nyamwihula, W.
Block B: Room B09
Mobile: 0784281242/0754588001
Email: [email protected]
Intro: Search and AI
In solving problems, we sometimes have to
search through many possible ways of doing
something.
We may know all the possible actions our
Hospital Newsagent
Library church
Park University
How do we systematically and exhaustively search
possible routes, in order to find, say, route from
library to university?
Search Space
The set of all possible states reachable from the initial state defines
the search space.
We can represent the search space as a tree.
library
school hospital
park newsagent
factory
university church
We refer to nodes connected to and “under” a node in the tree as
“successor nodes”.
Simple Search Techniques
How do we search this tree to find a possible
route from library to University?
May use simple systematic search
techniques, which try every possibility in
systematic way.
Breadth first search - Try shortest paths first.
Depth first search - Follow a path as far as it
goes, and when reach dead end, backup and
try last encountered alternative.
Breadth first search
Explore nodes in tree order: library, school,
hospital, factory, park, newsagent, uni, church.
(conventionally explore left to right at each level)
library
school hospital
park newsagent
factory
university
church
Depth first search
Nodes explored in order: library, school,
factory, hospital, park, newsagent,
university.
library
school hospital
park newsagent
factory
university
Algorithms for breadth first and depth first search.
Very easy to implement algorithms to do
these kinds of search.
Both algorithms keep track of the list of
nodes found, but for which routes from
them have yet to be considered.
E.g., [school, hospital] -have found school and
hospital in tree, but not yet considered the
nodes connected to these.
List is sometimes referred to as an agenda.
But implemented using stack for depth first,
queue for breadth first.
Algorithm for breadth first:
open(robot, door)
pickup(robot, Object).
Initial state:
in(robot, room1) etc.
Robot planning search tree
Me
Rob Beer
Me Me
Rob Beer Rob Beer
{0, 0}
Fill 4 gallon Fill 3 gallon
{4, 0} {0, 3}
{4, 3} {1, 3}
.. And so on.
So..
To solve a moderately complex puzzle what
we can do is:
Express it in terms of search.
Decide how “problem state” may be expressed
formally.
Decide how to encode primitive actions as rules
for getting from one state to another.
Use a standard tree/graph search
algorithm/program, which uses a general
“successor state” function which you define for
your problem.
Heuristic search algorithms.
Depth first and breadth first search turn out to be too
inefficient for really complex problems.
Instead we turn to “heuristic search” methods, which don’t
search the whole search space, but focus on promising
areas.
Simplest is best first search. We define some “heuristic
evaluation function” to say roughly how close a node is to
our target.
E.g., map search: heuristic might be “as the crow flies”
gallon jug.
Best first search algorithm
Best first search algorithm almost same as
depth/breadth.. But we use a priority queue,
where nodes with best scores are taken off
the queue first.
While queue not empty and not found do:
Remove the BEST node N from queue.
If N is a goal state, then found = TRUE.
Find all the successor nodes of N, assign them
a score, and put them on the queue..
Best first search
Order nodes searched: Library, hospital,
park, newsagent, university.
Library (6)
University (0)
Other heuristic search methods
Hill climbing: always choose successor
node with highest score.
A*: Score based on predicted total path
“cost”, so sum of
actual cost/distance from initial to current node,
predicted cost/distance to target node.
Summary
General search methods can be used to
solve complex problems.
Problems are formulated in terms of initial
and target state, and the primitive actions
that take you from one state to next.
May need to use heuristic search for
complex problems, as search space can
be too large.