[email protected] Informed search CSM6120 Introduction to Intelligent Systems.

58
[email protected] Informed search CSM6120 Introduction to Intelligent Systems

Transcript of [email protected] Informed search CSM6120 Introduction to Intelligent Systems.

Page 1: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

[email protected]

Informed search

CSM6120Introduction to Intelligent

Systems

Page 2: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Quick review Problem definition

Initial state, goal state, state space, actions, goal function, path cost function

Factors Branching factor, depth of shallowest solution,

maximum depth of any path in search state, complexity, etc.

Uninformed techniques BFS, DFS, Depth-limited, UCS, IDS

Page 3: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Informed search What we’ll look at:

Heuristics

Hill-climbing

Best-first search Greedy search A* search

Page 4: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Heuristics A heuristic is a rule or principle used to guide

a search It provides a way of giving additional knowledge of

the problem to the search algorithm Must provide a reasonably reliable estimate of

how far a state is from a goal, or the cost of reaching the goal via that state

A heuristic evaluation function is a way of calculating or estimating such distances/cost

Page 5: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Heuristics and algorithms A correct algorithm will find you the best

solution given good data and enough time It is precisely specified

A heuristic gives you a workable solution in a reasonable time It gives a guided or directed solution

Page 6: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Evaluation function There are an infinite number of possible

heuristics Criteria is that it returns an assessment of the

point in the search

If an evaluation function is accurate, it will lead directly to the goal

More realistically, this usually ends up as “seemingly-best-search”

Traditionally, the lowest value after evaluation is chosen as we usually want the lowest cost or nearest

Page 7: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Heuristic evaluation functions Estimate of expected utility value from a

current position E.g. value for pieces left in chess Way of judging the value of a position

Humans have to do this as we do not evaluate all possible alternatives These heuristics usually come from years of

human experience

Performance of a game playing program is very dependent on the quality of the function

Page 8: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Heuristics?

Page 9: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Heuristics?

Page 10: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Heuristic evaluation functions Must agree with the ordering a utility function

would give at terminal states (leaf nodes)

Computation must not take long

For non-terminal states, the evaluation function must strongly correlate with the actual chance of ‘winning’

The values can be learned using machine learning techniques

Page 11: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Heuristics for the 8-puzzle Number of tiles out of place (h1)

Manhattan distance (h2) Sum of the distance of each tile from its goal

position Tiles can only move up or down city blocks

0 1 2

3 4 5

6 7

Page 12: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

The 8-puzzle Using a heuristic evaluation function:

h2(n) = sum of the distance each tile is from its goal position

Page 13: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

0 1 2

3 4 5

6 7

0 1 2

3 4 5

6 7

Goal state

h1=1h2=1

h1=5h2=1+1+1+2+2=7

Current state Current state

0 2 5

3 1 7

6 4

Page 14: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Search algorithms Hill climbing

Best-first search Greedy best-first search A*

Page 15: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Iterative improvement Consider all states laid out on the surface of a

landscape

The height at any point corresponds to the result of the evaluation function

Page 16: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Iterative improvement Paths typically not retained - very little

memory needed

Move around the landscape trying to find the lowest valleys - optimal solutions (or the highest peaks if trying to maximise) Useful for hard, practical problems where the state

description itself holds all the information needed for a solution

Find reasonable solutions in a large or infinite state space

Page 17: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Hill-climbing (greedy local) Start with current-state = initial-state Until current-state = goal-state OR there is

no change in current-state do: a) Get the children of current-state and apply

evaluation function to each child b) If one of the children has a better score, then

set current-state to the child with the best score

Loop that moves in the direction of decreasing (increasing) value Terminates when a “dip” (or “peak”) is reached If more than one best direction, the algorithm can

choose at random

Page 18: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Hill-climbing (gradient ascent)

Page 19: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Hill-climbing drawbacks Local minima (maxima)

Local, rather than global minima (maxima)

Plateau Area of state space where the evaluation function

is essentially flat The search will conduct a random walk

Ridges Causes problems when states along the ridge are

not directly connected - the only choice at each point on the ridge requires uphill (downhill) movement

Page 20: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Best-first search Like hill climbing, but eventually tries all paths

as it uses list of nodes yet to be explored

Start with priority-queue = initial-state While priority-queue not empty do:

a) Remove best node from the priority-queue b) If it is the goal node, return success. Otherwise

find its successors c) Apply evaluation function to successors and

add to priority-queue

Page 21: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Best-first example

Page 22: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Best-first search Different best-first strategies have different

evaluation functions

Some use heuristics only, others also use cost functions: f(n) = g(n) + h(n)

For Greedy and A*, our heuristic is: Heuristic function h(n) = estimated cost of the

cheapest path from node n to a goal node For now, we will introduce the constraint that if n

is a goal node, then h(n) = 0

Page 23: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Greedy best-first search Greedy BFS tries to expand the node that is

‘closest’ to the goal assuming it will lead to a solution quickly f(n) = h(n) aka “greedy search”

Differs from hill-climbing – allows backtracking

Implementation Expand the “most desirable” node into the frontier

queue Sort the queue in decreasing order

Page 24: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Route planning: heuristic??

Page 25: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Route planning - GBFS

Page 26: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Greedy best-first search

Page 27: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Route planning

Page 28: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Greedy best-first search

Page 29: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Greedy best-first search

This happens to be the same search path that hill-climbing would produce, as there’s no backtracking involved (a solution is found by expanding the first choice node only, each time).

Page 30: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Greedy best-first search Complete

No, GBFS can get stuck in loops (e.g. bouncing back and forth between cities)

Time complexity O(bm) but a good heuristic can have dramatic

improvement

Space complexity O(bm) – keeps all the nodes in memory

Optimal No! (A – S – F – B = 450, shorter journey is

possible)

Page 31: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Practical 2 Implement greedy best-first search for

pathfinding

Look at code for AStarPathFinder.java

Page 32: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search A* (A star) is the most widely known form of

Best-First search It evaluates nodes by combining g(n) and h(n)

f(n) = g(n) + h(n)

where g(n) = cost so far to reach n h(n) = estimated cost to goal from n f(n) = estimated total cost of path through n

start

h(n)

g(n)

n

goal

Page 33: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search When h(n) = h*(n) (h*(n) is actual cost to

goal) Only nodes in the correct path are expanded Optimal solution is found

When h(n) < h*(n) Additional nodes are expanded Optimal solution is found

When h(n) > h*(n) Optimal solution can be overlooked

Page 34: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Route planning - A*

Page 35: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search

Page 36: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search

Page 37: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search

Page 38: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search

Page 39: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search

Page 40: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search Complete and optimal if h(n) does not

overestimate the true cost of a solution through n

Time complexity Exponential in [relative error of h x length of solution] The better the heuristic, the better the time

Best case h is perfect, O(d) Worst case h = 0, O(bd) same as BFS, UCS

Space complexity Keeps all nodes in memory and save in case of repetition This is O(bd) or worse A* usually runs out of space before it runs out of time

Page 41: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* exercise

Node Coordinates SL Distance to KA (5,9) 8.0B (3,8) 7.3C (8,8) 7.6D (5,7) 6.0E (7,6) 5.4F (4,5) 4.1G (6,5) 4.1H (3,3) 2.8I (5,3) 2.0J (7,2) 2.2K (5,1) 0.0

Page 42: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Solution to A* exercise

Page 43: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

GBFS exercise

Node Coordinates DistanceA (5,9) 8.0B (3,8) 7.3C (8,8) 7.6D (5,7) 6.0E (7,6) 5.4F (4,5) 4.1G (6,5) 4.1H (3,3) 2.8I (5,3) 2.0J (7,2) 2.2K (5,1) 0.0

Page 44: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Solution

Page 45: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

To think about...f(n) = g(n) + h(n)

What algorithm does A* emulate if we set h(n) = -g(n) - depth(n) h(n) = 0

Can you make A* behave like Breadth-First Search?

Page 46: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

A* search - Mario http://aigamedev.com/open/interviews/mario-

ai/ Control of Super Mario by an A* search Source code available Various videos and explanations Written in Java

Page 47: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Admissible heuristics A heuristic h(n) is admissible if for every node n,

h(n) ≤ h*(n), where h*(n) is the true cost to reach the goal state from n An admissible heuristic never overestimates the cost to

reach the goal Example: hSLD(n) (never overestimates the actual road

distance)

Theorem: If h(n) is admissible, A* is optimal (for tree-search)

Page 48: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Optimality of A* (proof) Suppose some suboptimal goal G2 has been generated and

is in the frontier. Let n be an unexpanded node in the frontier such that n is on a shortest path to an optimal goal G

f(G2) = g(G2) since h(G2) = 0 (true for any goal state)

g(G2) > g(G) since G2 is suboptimal f(G) = g(G) since h(G) = 0 f(G2) > f(G) from above

Page 49: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Optimality of A* (proof)

f(G2) > f(G) h(n) ≤ h*(n) since h is admissible g(n) + h(n) ≤ g(n) + h*(n) f(n) ≤ f(G) (f(G) = g(G) = g(n) + h*(n))

Hence f(G2) > f(n), and A* will never select G2 for expansion

Page 50: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Admissible heuristic example: for the 8-puzzle

h1(n) = number of misplaced tilesh2(n) = total Manhattan distance i.e. no of squares from desired location of each tile

h1(S) = ??h2(S) = ??

Heuristic functions

Page 51: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Admissible heuristic example: for the 8-puzzle

h1(n) = number of misplaced tilesh2(n) = total Manhattan distance i.e. no of squares from desired location of each tile

h1(S) = 6h2(S) = 4+0+3+3+1+0+2+1 = 14

Heuristic functions

Page 52: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Dominance/Informedness if h2(n) h1(n) for all n (both admissible)

then h2 dominates h1 and is better for search

Typical search costs: (8 puzzle, d = solution length) d = 12 IDS = 3,644,035 nodes

A*(h1) = 227 nodes A*(h2) = 73 nodes

d = 24 IDS 54,000,000,000 nodes A*(h1) = 39,135 nodes

A*(h2) = 1,641 nodes

Heuristic functions

Page 53: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Admissible heuristicexample: for the 8-puzzle

h1(n) = number of misplaced tilesh2(n) = total Manhattan distance i.e. no of squares from desired location of each tile

h1(S) = 6h2(S) = 4+0+3+3+1+0+2+1 = 14

But how do we come up with a heuristic?

Heuristic functions

Page 54: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Admissible heuristics can be derived from the exact solution cost of a relaxed version of the problem E.g. If the rules of the 8-puzzle are relaxed so that a

tile can move anywhere, then h1(n) gives the shortest solution

If the rules are relaxed so that a tile can move to any adjacent square, then h2(n) gives the shortest solution

Key point: the optimal solution cost of a relaxed problem is no greater than the optimal solution cost of the real problem

Relaxed problems

Page 55: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Choosing a strategy What sort of search problem is this?

How big is the search space?

What is known about the search space?

What methods are available for this kind of search?

How well do each of the methods work for each kind of problem?

Page 56: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Which method? Exhaustive search for small finite spaces when

it is essential that the optimal solution is found

A* for medium-sized spaces if heuristic knowledge is available

Random search for large evenly distributed homogeneous spaces

Hill climbing for discrete spaces where a sub-optimal solution is acceptable

Page 57: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Summary What is search for?

How do we define/represent a problem?

How do we find a solution to a problem?

Are we doing this in the best way possible?

What if search space is too large? Can use other approaches, e.g. GAs, ACO, PSO...

Page 58: Rkj@aber.ac.uk Informed search CSM6120 Introduction to Intelligent Systems.

Finally Try the A* exercise on the course website

(solutions will be made available later)

Next seminar on Monday at 9:30am

See the algorithms in action: http://web.archive.org/web/20110719085935/http://www.s

tefan-baur.de/cs.web.mashup.pathfinding.html