Title: Introduction To Minimum Cost Problems
1Introduction To Minimum Cost Problems Uniform
Cost Search
- G51IAI Introduction to AI
- Andrew Parkes
- http//www.cs.nott.ac.uk/ajp/
2Coursework Issues
- Filesystem just means a small set of files and
folders (not reimplement VFAT) folder tree,
just something to function as a small test for
the later parts - Have another look at the coursework site for
standard CSiT submission process - Submit files for cw on marian type cw help
- Submit paper via letterbox outside entry to main
computer lab
3PLEASE NOTE
- NO LECTURE WED. 18th Oct. 2006
- (I am away on a 2-day course aboutDecision
Making under Uncertainty)
4Shortest Path Problems
- Such problems are common in real-life problems
- e.g. select routes for planes so as to minimise
fuel use - edge fuel used on a segment after taking
advantage of local winds - In the course, we will see other, less obvious,
examples which are also hidden shortest path
problems - Such shortest path problems are a key part of
this course
5Weighted Graphs
- Shortest path problems use weighted graphs
- These are graphs in which each edge is given a
number called its weight - The weight of an edge is intended to be its
cost, or value, or length, etc - Paths then have a weight which is just the sum of
the weights of the edges in the path
6Shortest Path Problems
- Suppose we want to find the shortest journey from
Nottingham to Edinburgh - Road system converts to a weighted graph
- edge segments of roads between intersection
- nodes intersections
- weights or costs of edges distance between
intersections/nodes - Shortest path in a graph
- no longer doing trees
- might have multiple paths
- might have no path
- Shortest journey from Nottingham (N) to Edinburgh
(E) converts to - given the weighted graph
- find the shortest path from node N to node E
7Desired Search Properties
- Completeness will find a goal if one is legally
reachable - Optimality will find
- goal with shortest path to it
- and (usually) also find the shortest path itself
- Systematic dont do the same work too many
times - but there is no generally agreed exact definition
- sometimes taken to mean to never repeat the same
work
8Weighted Trees
- As usual, trees are easier to understand than
graphs so will start with rooted trees first
9Finding Min-Cost Paths
- Blind Search
- Try to search shortest paths first, until find
one that reaches a goal - How to achieve this?
- Do we already have anything that that almost
works in some cases? - Yes. BFS does the job in some cases
10BFS and min-cost paths
- Suppose that we have a tree in which all the
weights are one - Weight of a path is its length
- Weight of a path from the root to a node N is
just the depth of node N - But
- BFS searches all nodes of depth d before any of
depth d1 - hence, in this case, BFS finds minimum cost
(mincost) goals
11BFS in Tree with unit weights
- Suppose that E and C are goal nodes
- Costs of nodes are just their depth
- The mincost goal is C
- DFS would find E first
- not the mincost goal
- simple DFS is not optimal and complete
- BFS would search all the c1 nodes before it
searches the c2 node, - would correctly find goal C
c0
c1
c2
c3
c4
12Trees with non-unit weights
- Suppose that some edges have cost gt 1
- Think of the cost as a depth
- Redraw the tree so that nodes are at the depth
given by their cost - The mincost goal is now E
- Want to do a BFS but in which the search
pattern is by cost not depth
F
c0
J
B
c1
3
H
E
c2
C
I
A
c3
D
G
c4
13Uniform Cost Search
- Search Pattern smaller cost nodes before larger
- Fringe in red
- Visited in blue
root
outline of tree
increasing cost
14Uniform Cost Search (UCS)
- Cost of a node n, is the total cost of the path
from the root of the tree to n - usually write this as g or g(n)
- Search all nodes of cost c before those of cost
c1 - How to implement?
- In BFS the effect of the depth was implicitly
handled adding new nodes to the end of the
queue
15Breadth First Search
- fringe ? MAKE-EMPTY-QUEUE()
- fringe ? INSERT( root_node )
- found ? false // boolean flag
- loop
- if fringe is empty then return found // finished
the search - node ? REMOVE-FIRST(fringe)
- if node is a goal
- print node // if want to list all matches to
the goal - found ? true // so we remember we succeeded
- (if only want first goal then return true)
- L ? EXPAND(node)
- fringe ? INSERT-ALL-AT-END( L, fringe ) //
insert at end for BFS -
16Uniform Cost Search (UCS)
- Search all nodes of cost c before those of cost
c1 - In BFS deeper nodes always arrive after shallower
nodes - In UCS the costs of new nodes do not have such a
nice pattern of always increasing - In UCS we need to
- explicitly store the cost g of a node
- explicitly use such costs in deciding the
ordering in the queue
17Uniform Cost Search (UCS)
- Explicitly store the cost of a node with that
node convention g is the path-cost - Always remove the smallest cost node first
- if using REMOVE-FIRST then sort the queue in
increasing order - alternatively, instead of REMOVE-FIRST, just scan
all queue and use REMOVE-SMALLEST-COST - Called a priority queue as highest priority
removed first, not just by order of arrival
18Uniform Cost Search in Tree
- fringe ? MAKE-EMPTY-QUEUE()
- fringe ? INSERT( root_node ) // with g0
- loop
- if fringe is empty then return false // finished
without goal - node ? REMOVE-SMALLEST-COST(fringe)
- if node is a goal
- print node and g
- return true // that found a goal
- Lg ? EXPAND(node) // Lg is set of children with
their g costs //
NOTE do not check Lg for goals here!! - fringe ? INSERT-ALL(Lg, fringe )
-
19From Trees To Graphs
- Most real-life examples
- journey planning, etc
- are graphs not trees
- more accurately, they are weighted graphs
20Weighted Graph Example
- This is still not a map!
- The positions of the nodes are not directly
meaningful - (Though often try to draw them at least
approximately correctly)
3
5
6
2
7
11
5
21Graph vs. Trees
- In rooted trees
- we have (implicitly) downwards edges and
children - In contrast in a graph
- there is no automatic meaning to away from the
start node - Implications
- change notation from children to neigbours or
successors - have to be more careful to avoid loops
22Graph Search Neighbours
- Neighbour
- Given a node n then a neighbour is any other node
that is connected to n by an edge - m is a neighbor of n if and only if (n,m) 2 E
- Neighbourhood
- the neighbourhood of n is just the set of
neighbours to n - note that it is just an (unordered) set
- no concept of first or second element
- it does not have any order unless we chose to
impose one - if the nodes have labels then we might chose to
create a list of neighbours with a specific
ordering - e.g. by using a lexicographical (dictionary)
ordering - or any other method of ordering we like
23Uniform Cost Search in Graphs
- Search Pattern smaller cost nodes before larger
- Fringe in red
- Visited in blue
- Blind still ignores goals
outline of graph
goals
start
increasing path cost from start
24Preventing Loopy Paths
- Graphs have neighbours instead of children
- hence paths can go backwards, A?B?C?B
- can get paths with loops A?B?C?D?B
- Can also have multiple paths to a node, though
just want the mincost path - Both problems arise because we rediscover nodes
that the search has already discovere - Need to consider methods to suppress nodes that
have already been visited
25Preventing Loopy Paths
- If already found a path to a node but the new
path has higher cost - do not need to consider the new path
- consider it anyway
- saves the effort of implementing a check for
whether a node already had a path to it - algorithm is still complete and optimal
- but at runtime, the implementation will probably
use a lot more time and memory
26Preventing Loopy Paths
- Practical Options (in order of power and
implementation cost) - Prevent paths returning to the immediately
previous node - Prevent return to node higher on the path (just
prevent loops) - Full suppress anything on the union of visited
and fringe lists (unless the new path is shorter)
27Uniform Cost Search in Graph
- fringe ? MAKE-EMPTY-QUEUE()
- fringe ? INSERT( root_node ) // with g0
- loop
- if fringe is empty then return false // finished
without goal - node ? REMOVE-SMALLEST-COST(fringe)
- if node is a goal
- print node and g
- return true // that found a goal
- Lg ? EXPAND(node) // Lg is set of neighbours with
their g costs //
NOTE do not check Lg for goals here!! - fringe ? INSERT-IF-NEW(Lg, fringe ) // ignore
revisited nodes // unless is with new
better g -
28Keeping the Path
- Suppose that the output not only wants the
mincost goal, and the cost, but also wants the
path - Implementation needs extra book-keeping
- Have to keep back pointers
- for each node keep a pointer to the previous node
on the min cost path to that node - by following the back pointers from any node n
can recreate the min cost path from the start to
n - storing all these previous nodes and the pointers
can require a lot of extra memory and runtime
29Uniform cost search
- A breadth-first search finds the shallowest goal
state and will therefore be the cheapest solution
provided the path cost is a function of the depth
of the solution. But, if this is not the case,
then breadth-first search is not guaranteed to
find the best (i.e. cheapest solution). - Uniform cost search remedies this by expanding
the lowest cost node on the fringe, where cost is
the path cost, g(n). - In the following slides those values that are
attached to paths are the cost of using that
path.
30Consider the following problem
A
10
1
5
5
B
S
G
5
15
C
We wish to find the shortest route from node S to
node G that is, node S is the initial state and
node G is the goal state. In terms of path cost,
we can clearly see that the route SBG is the
cheapest route. However, if we let breadth-first
search loose on the problem it will find the
non-optimal path SAG, assuming that A is the
first node to be expanded at level 1. Press space
to see a UCS of the same node set
31We start with our initial state and expand it
Node S is removed from the queue and the revealed
nodes are added to the queue. The queue is then
sorted on path cost. Nodes with cheaper path cost
have priority.In this case the queue will be Node
A (1), node B (5), followed by node C (15). Press
space.
We now expand the node at the front of the queue,
node A. Press space to continue.
Node A is removed from the queue and the revealed
node (node G) is added to the queue. The queue is
again sorted on path cost. Note, we have now
found a goal state but do not recognise it as it
is not at the front of the queue. Node B is the
cheaper node. Press space.
Once node B has been expanded it is removed from
the queue and the revealed node (node G) is
added. The queue is again sorted on path cost.
Note, node G now appears in the queue twice, once
as G10 and once as G11. As G10 is at the front of
the queue, we now proceed to goal state. Press
space.
A
A
10
1
5
5
S
B
S
G
B
G
G
G
G
G
The goal state is achieved and the path S-B-G is
returned. In relation to path cost, UCS has found
the optimal route. Press space to end.
15
C
Press space to begin the search
Size of Queue 0
Queue Empty
Queue S
Size of Queue 1
Size of Queue 3
Queue A, B, C
Queue B, G11, C
Queue G10, G11, C15
Queue Empty
Size of Queue 0
Nodes expanded 0
Current action Waiting.
Current level n/a
Current action Expanding
Current level 0
Nodes expanded 1
Current level 1
Nodes expanded 2
Current action Backtracking
Current level 0
Current level 1
Current action Expanding
Nodes expanded 3
Current level 2
FINISHED SEARCH
UNIFORM COST SEARCH PATTERN
32Summary
- Added costs (weights) to the edges
- Modified BFS to create Uniform Cost Search (UCS)
that - finds optimal (minimum cost) paths to goal(s)
- Expectations
- that you can run UCS by hand
- you can predict the order in which UCS expands
nodes - that you can implement UCS (coursework two)
33Questions?