Title: AI
1AI Week 6 Implementing your own AI Planner in
Prolog part II HEURISTICS
- Lee McCluskey room 2/09
- Email lee_at_hud.ac.uk
- http//scom.hud.ac.uk/scomtlm/cha2555/
2Last Week Building your own forward searching
planner IN PROLOG
Each node is modelled as a state sequence of
operators that lead to the state heuristic value
INITIAL STATE
A search algorithm is COMPLETE if it can always
find a solution if there is one A search
algorithm is OPTIMAL if it always finds the
shortest (minimal cost) solution
3RECAP Planning Algorithm in Prolog
breadth-first forward search through state-space
- 1. Store the first node (initial state empty
solution) - Repeat
- 2. pick a node(StateSoln)
- 3. pick an operator and parameter grounding -
O - that can be applied to State - 4. apply O to State to get State
- 5. assert(node(State SolnO))
- 6. if possible backtrack to 3. and make a
different choice. - until a node has been asserted that contains a
solution
4Heuristic Search - Definitions
- Optimal if a solution is found it is best
solution (that is it minimises some metric such
as length of solution or amount of resource
consumed) - Complete - guaranteed to find a solution if there
is one - Efficiency amount of time / space required to
find a solution
5Heuristic Search - Definitions
- BEST - FIRST search repeat the following ..
- collect the set of un-expanded (ie OPEN) nodes
- -- pick a node from the set to expand if a
heuristic function gives it the best value - -- mark it as closed and expand it giving new
open nodes to the set
6Heuristic Search - Definitions
- Variation GREEDY search
- pick a node to expand if it appears to be
nearest the goal - That is pick a node which appears to have the
minimum distance between the node and a goal
node - GREEDY search takes no account of the effort
spent in getting to that node in the first place!
Hence Greedy.
7Heuristics - Definitions
- NON-GREEDY variation of best-first search
- factor in the cost of getting to the current
state.. - Let a heuristic value of node n be given by
- COST(n) g(n) h(n)
- where
- g(n) is the ACTUAL COST of the path to the
current node - h(n) is the ESTIMATED COST of reaching the goal
from n
8Heuristics - Example
- Breadth First Search
- As carried out by the planner in last weeks
practical is best-first in the sense that - COST(n) g(n) h(n)
- where
- g(n) Count of action applications is the COST
of the path to get to node n - h(n) 0
- This makes Breadth First Search OPTIMAL and
COMPLETE but often hopelessly inefficient.
9Admissable Heuistics
- An ADMISSIBLE heuristic evaluation function is
one that supplies an estimate of the cost to
reach a goal state from current state and the
goal - and never overestimates that cost.
- Example Goal - get from current position P to a
Goal position G by the road network. - an ADMISSIBLE estimated cost is the straight line
distance between P and G
10The FAMOUS A Property
- An A search algorithm is one that expands a node
with the lowest cost where - COST(n) g(n) h(n)
- g(n) is the ACTUAL COST of the path to the
current node (usually number of operators/actions
required or amount of resources) - h(n) is an ADMISSIBLE estimated cost of reaching
the goal from n - A algorithms are OPTIMAL and COMPLETE
11Adding Heuristics to Prolog Code
- Heuristic 1 Prune the tree dont visit/expand
the same state twice - eg 5. IF State NOT EXPANDED BEFORE
- THEN assert(node(State SolnO))
- ve In some domains cuts down search
considerably. - Does not affect completeness of search.
- Does not affect optimality in a breadth
first search - - ve overhead in storing and searching through
all previous states. - Might not find ALL solutions
- See website for an implementation of this (
dont expand a node(SSoln1) if a state with
node(SSoln2) is already in the open nodes)
12Adding Heuristics to Prolog Code
- Heuristic 2 greedy search
- COST estimated effort to reach a solution
from the current state eg number of goals still
to be achieved - 5. assert(node(State SolnO COST)
- 2. pick a node(StateSoln COST)
- ---- WHERE COST HAS THE LOWEST VALUE
(ignoring the cost/size of Soln) - eg Evaluate the nodes BEFORE ASSERTING them and
store them with the cost attached
13Heuristic 2 -Example
ABCD solved
1
AB solved
3
AB solved
3
INITIAL STATE
Goalset of subgoals ABCDE
E solved
4
CD solved
3
ABC solved
2
GREADY SEARCH PICK THIS NODE TO EXPAND
CD solved
3
Greedy Scores in Red
14Adding Heuristics to the Planner
- recall node n (state ops) COST(n)g(n)h(n)
- Heuristic 3 COST(n) how many ops it took to
get there (g(n) length of ops) and h(n) 0 - minimise cost(n) Breadth first search.
- Is h(n) admissible Is this A
- Heuristic 4 COST(n) how many ops it took to
get there (g(n) length of ops) h(n) how
many subgoals still to solve. - -ve crude - may work in some domains but not in
others - ve negligible overhead
- Is h(n) admissible Is this A
15Heuristic 4 -Example
ABCD solved
13
AB solved
33
AB solved
33
INITIAL STATE
Goal condition ABCDE
E solved
42
CD solved
32
ABC solved
21
PICK THIS NODE TO EXPAND
CD solved
31
Greedy Scores in Red
16Adding Heuristics the PlanGraph
- Heuristic 5 Relax the problem take away some
of the constraints - To calculate the cost of a node n (state
ops) - Calculate solution plan from state ignoring
delete lists (ie ignoring undoing effects and
interference) of planning operators. Let h(n)
length of shortest relaxed plan - So COST(n) length ops h(n)
- This is admissible as it is always an
underestimation of the distance to goal - it
never over estimates.
17Other improvements to Planner..
- See website another world planner
- the WEB SERVICE COMPOSITION world (simulates a
web agent that needs to plan to achieve goals) - Improvement I have added the ability to put
EVALUABLE predicates in states eg maths
operators assigment
18Summary
- It is easy to add Heuristics to the Breadth first
state space planner to make it Best first - Relaxed problem solving can provide a very good
admissible measure of h(n)