AI-Module-2
AI-Module-2
3
Goal formulation
First step in problem solving is goal formulation
It is based on the current situation and the agent’s performance measure
The goal is formulated
◦as a set of world states, in which the goal is satisfied
Reaching from initial state, goal state
◦Actions are required
Actions are the operators
Actions at a certain degree, instead of very detailed
◦E.g: With such high level of detail there is too much uncertainty in the world and there
would be too many steps in a solution for agent to find a solution
4
Problem formulation
The process of deciding
◦what actions and states to consider
E.g., driving Ernakulam to Chennai
◦in-between states and actions defined
◦States: Some places in Ernakulam and Chennai
◦Actions: Turn left, Turn right, go straight, accelerate & brake, etc.
agent will consider actions at the level of driving from one major town to
another
Each state therefore corresponds to being in a particular town.
5
an agent with several immediate options of unknown value
can decide what to do by first examining future actions
that eventually lead to states of known value.
6
Properties of the Environment
properties of the environment
◦ Observable: agent always knows the current state
◦ Discrete: at any given state there are only finitely many actions to
choose from
◦ Known: agent knows which states are reached by each action.
◦ Deterministic: each action has exactly one outcome
7
Sear
ch
The process of looking for a sequence of actions that reaches the
goal is called search.
A search algorithm takes a problem as input and returns a solution
in the form of an action sequence.
Once a solution is found, the actions it recommends can be carried
out. This is called the execution phase.
8
Searching
1.Process
Formulate a goal and a problem to solve,
2. the agent calls a search procedure to solve it
3. Agent uses the solution to guide its actions,
4. do whatever the solution recommends
5. remove that step from the sequence.
6. Once the solution has been executed, the agent will formulate a
new goal.
9
Open-loop
system
while the agent is executing the solution sequence it ignores its percepts
when choosing an action because it knows in advance what they will be
An agent that carries out its plans with its eyes closed, so to speak,
must be quite certain of what is going on is an open loop.
10
Well-defined problems and
solutions
A problem can be defined formally by following components:
1 The initial state that the agent starts in
.
A description of the possible actions available to the agent.
2 ◦ Given a particular state s, ACTIONS(s) returns the set of actions that can be executed in s. We say that each of
. these actions is applicable in s.
◦ For example, from the state In(Ernakulam), the applicable actions are {Go(Thrissur), Go(Palakkad),
Go(Kozhikod)}.
3. Transition model: description of what each action does, specified by a function RESULT(s, a) that returns the
state that results from doing action a in state s
4. Successor: any state reachable from a given state by a single action
◦ RESULT(In(Ernakulam),Go(Thrissur)) = In(Thrissur) .
5. state space: the set of all states reachable from the initial state by any sequence of actions. forms a
directed network or graph in which the nodes are states and the links between nodes are actions.
11
6. A path in the state space is a sequence of states connected by a sequence of actions
7. The goal test, which determines whether a given state is a goal state {In(Chennai)}
8. A path cost function that assigns a numeric cost to each path
cost of a path can be described as the sum of the costs of the individual actions along the path.3 The step
cost of taking action a in state s to reach state s is denoted by c(s, a, s ).
A solution to a problem is an action sequence that
leads from the initial state to a goal state.
Solution quality is measured by the
path cost function, and an optimal solution has
the lowest path cost among all solutions
12
Formulating problems
Anything else besides the four components for problem formulation
Abstraction
◦the process to take out the irrelevant information
◦leave the most essential parts to the description of the states
( Remove detail from representation)
◦ Conclusion: Only the most important parts that are contributing
to searching are used
The choice of a good abstraction thus involves removing as much detail as possible
while retaining validity and ensuring that the abstract actions are easy to carry out.
Were it not for the ability to construct useful abstractions, intelligent agents
would be completely swamped by the real world.
13
Evaluation Criteria
formulation of a problem as search task basic search
strategies
important properties of search strategies selection of search
strategies for specific tasks
(The ordering of the nodes in FRINGE defines the search
strategy)
14
Problem-Solving
Agents
agents whose task is to solve a particular problem (steps)
◦goal formulation
◦what is the goal state
◦what are important characteristics of the goal state
◦how does the agent know that it has reached the goal
◦are there several possible goal states
◦are they equal or are some more preferable
◦problem formulation
◦what are the possible states of the world relevant for solving the problem
◦what information is accessible to the agent
◦how can the agent progress from state to state
15
16
1. Formulate Goal
- Be In Amman
2. Formulate Problem
- States : Cities
- actions : Drive Between Cities
3. Find Solution
17
1. Problem : To Go from Ajlun to Amman
2. Initial State : Ajlween
3. Operator : Go from One City To another .
4. State Space : {Jarash , Salat , irbed,……..}
5. Goal Test : are the agent in Amman.
6. Path Cost Function : Get The Cost From The Map.
Am} }
8. State Set Space : {Ajlun Jarash Amman}
18
Example: Romania
On holiday in Romania; currently in
Arad. Flight leaves tomorrow from
Bucharest
Formulate goal:
◦be in Bucharest
◦
Formulate problem:
◦states: various cities
◦actions: drive between cities
Find solution:
◦ sequence of cities, e.g., Arad, Sibiu, Fagaras,
Bucharest
◦
19
Single-state problem formulation
A problem is defined by four items:
initial state e.g., "at Arad"
1. actions or successor function S(x) = set of action–state pairs
◦ e.g., S(Arad) = {<Arad Zerind, Zerind>, … }
2. goal test, can be
◦ explicit, e.g., x = "at Bucharest"
◦ implicit, e.g., Checkmate(x)
3. path cost (additive)
◦ e.g., sum of distances, number of actions executed, etc.
◦ c(x,a,y) is the step cost, assumed to be ≥ 0
A solution is a sequence of actions leading from the initial state to a goal state
20
Example Problems
Distinguishing between toy and real-world problems
Toy Problem is intended to illustrate or exercise various problem-
solving methods. E.g., puzzle, chess, etc.
21
Toy problems- Vacuum
World
Compared with the real world, this toy problem has discrete locations, discrete dirt,
reliable cleaning, and it never gets any dirtier.
22
Number of states:
8 Initial state: Any
Number of
actions:
4 suck,
left, right,
noOp
Goal: clean up all
dirt
Goal states: {7, 8}
Path Cost:
Each step costs 1
23
24
8-puzzle
A tile adjacent to the blank space can slide into the space. The object is to reach a specified
goal state
25
26
The 8-puzzle has 9!/2 = 181, 440 reachable states and is easily solved.
The 15-puzzle (on a 4×4 board) has around 1.3 trillion states, and
random
instances can be solved optimally in a few milliseconds by the best
search algorithms.
The 24-puzzle (on a 5 × 5 board) has around 1025 states, and random
instances take several hours to solve optimally.
27
8-queens
problem
The goal of the 8-queens problem is to place eight queens on a chessboard such that no queen attacks
any other.
A queen attacks any piece in the same row, column or diagonal.
28
There are two main kinds of
◦An incremental formulation
formulation
involves operators that augment the state description starting from an empty
state
Each action adds a queen to the state
States:
any arrangement of 0 to 8 queens on board
Successor function:
add a queen to any empty square
◦A complete-state formulation
starts with all 8 queens on the board
move the queens individually around
States:
any arrangement of 8 queens, one per column in the leftmost columns
Operators: move an attacked queen to a row, not attacked by any other
othe right formulation makes a big difference to the size of the search space
29
Incremental formulation
In this formulation, we have 64 · 63 ··· 57 ≈ 1.8 × 1014 possible sequences to investigate. A better formulation would prohibit
placing a queen in any square that is already attacked:
This formulation reduces the 8-queens state space from 1.8 × 1014 to just 2,057, and solutions are easy to find. On the
other hand, for 100 queens the reduction is from roughly 10400 states to about 1052 states (Exercise 3.5)—a big
improvement, but not enough to make the problem tractable
30
Real-world problems- Route-
Finding problem
Route-finding problems
Touring problems
Traveling Salesman problem
VLSI layout problem
Robot navigation
Automatic assembly sequencing
Internet searching
31
Airline travel problems
A really good system should include contingency plans such as backup reservations on alternate flights to the
extent that these are justified by the cost and likelihood of failure of the original plan.
32
Touring problems
“Visit every city in at least once, starting and ending in Bucharest.”
actions correspond to trips between adjacent cities
Each state must include not just the current location but also the set of cities
the agent has visited.
So the initial state would be In(Bucharest), Visited({Bucharest}), a typical
intermediate state would be In(Vaslui), Visited({Bucharest, Urziceni, Vaslui}),
goal test would check whether the agent is in Bucharest and all 20 cities
have been visited.
33
Traveling salesperson problem
The traveling salesperson problem (TSP) is a touring problem in which each city
must be visited exactly once.
The aim is to find the shortest tour.
The problem is known to be NP-hard, but an enormous amount of effort
has been expended to improve the capabilities of TSP algorithms.
In addition to planning trips for traveling salespersons, these algorithms have
been used for tasks such as planning movements of automatic circuit-board
drills and of stocking machines on shop floors
34
VLSI layout
VLSI layout problem requires positioning millions of components and connections on a chip
to minimize area, minimize circuit delays, minimize stray capacitances, and maximize
manufacturing yield.
The layout problem comes after the logical design phase and is usually split into two parts:
◦ cell layout and channel routing.
Cell layout
◦ the primitive components of the circuit are grouped into cells, each of which performs some
recognized
function.
◦ Each cell has a fixed footprint and requires a certain number of connections to each of the other
cells.
◦ The aim is to place the cells on the chip so that they do not overlap and so that there is room for
the connecting wires to be placed between the cells.
Channel routing
◦ finds a specific route for each wire through the gaps between the cells.
35
Robot navigation
Robot navigation is a generalization of the route-finding problem
Rather than following a discrete set of routes, a robot can move in a continuous
space with
an infinite set of possible actions and states.
For a circular robot moving on a flat surface, the space is essentially two-
dimensional.
When the robot has arms and legs or wheels that must also be controlled, the
search space becomes many-dimensional.
Advanced techniques are required just to make the search space finite.
36
Automatic assembly sequencing
Aim is to find an order in which to assemble the parts of some object.
If the wrong order is chosen, there will be no way to add some part later in the
sequence without undoing some of the work already done.
Checking a step in the sequence for feasibility is a difficult geometrical search problem
closely related to robot navigation.
Thus, the generation of legal actions is the expensive part of assembly sequencing.
Any practical algorithm must avoid exploring all but a tiny fraction of the state space.
protein design is an automatic assembly problem in which the goal is to find a
sequence of amino acids that will fold into a three-dimensional protein with the right
properties to cure some disease.
37
Searching for solution
A solution is an action sequence, so search algorithms work by
considering various possible action sequences.
The possible action sequences starting at the initial state form a search tree
with the initial state at the root.
The branches are actions and the nodes correspond to states in the state
space of the problem.
38
Search tree
for finding
a route
from Arad
to
Bucharest
39
The root node of the tree corresponds to the initial state,
In(Arad).
The first step is to test whether this is a goal state.
Then we need to consider taking various actions. We do this by expanding the current
state;
applying each legal action to the current state, thereby generating a new set of states.
In this case, we add three branches from the parent node In(Arad) leading to three
new
child nodes: In(Sibiu), In(Timisoara), and In(Zerind).
Now we must choose which of these three possibilities to consider further.
The process of expanding nodes on the frontier continues until either a solution is
found
or there are no more states to expand.
search strategy: how they choose which state to expand next
40
loopy path: path from Arad to Sibiu and back to Arad again! We say that In(Arad) is
a
repeated state in the search tree, generated in this case by a loopy path.
Considering such loopy paths means that the complete search tree for Romania is infinite
because there is no limit to how often one can traverse a loop
loops can cause certain algorithms to fail, making otherwise solvable problems
unsolvable. there is no need to consider loopy paths.
We can rely on more than intuition for this: because path costs are additive and step
costs are nonnegative, a loopy path to any given state is never better than the same path
with the loop removed.
redundant paths: exist whenever there is more than one way to get from one state to
another eg, the paths
Arad–Sibiu (140 km long) and Arad–Zerind–Oradea–Sibiu (297 km long).
41
Frontier
We reach a state when we identify a path from the start state to it. But, we say
that we expanded it if we had followed all its outward edges and reached all its
children.
So, we can also think of a search as a sequence of expansions, and we first have
to reach a state before expanding it.
Frontier is the reached but unexpanded states because we can expand only
Them.
a search strategy has two components:
•rule(s) to decide whether or not to place the node in the frontier
•rule(s) to choose the next frontier node for expansion
42
TREE-SEARCH algorithm
with a data structure called the explored set (also known as the closed list),
which remembers every expanded node.
Newly generated nodes that match previously generated nodes ones in the
explored set or the frontier can be discarded instead of being added to the
frontier
43
44
GRAPH-SEARCH algorithm
Each state appears in the graph only once. But, it may appear
in the tree multiple times
contains at most one copy of each state, so we can think of
it
as growing a tree directly on the state-space graph,
Don’t add a node if its state has already been expanded or a
node pointing to the same state is already in the frontier.
so that every path from the initial state to an
unexplored state has to pass through a state in the
frontier.
As every step moves a state from the frontier into
the explored region while moving some states from
the unexplored region into the frontier,
we see that the algorithm is systematically examining
the
states in the state space, one by one, until it finds a 45
solution.
46
Infrastructure for search
structure algorithms
Search algorithms require a data
to keep track of the search
tree that is being constructed. For
each node n of the tree, we have a
structure that contains four
components:
n.STATE: the state in the state space to
which the node corresponds;
n.PARENT: the node in the search tree
that generated this node;
n.ACTION: the action that was
applied to
the parent to generate the node;
n.PATH-COST: the cost, traditionally
denoted by g(n), of the path from the
initial state to the node, as indicated
by the parent pointers.
47
PARENT pointers string the nodes together into a tree structure.
These pointers also allow the solution path to be extracted when a goal node is found;
we use the SOLUTION function to return the sequence of actions obtained by
following parent pointers back to the root.
A node is a bookkeeping data structure used to represent the search
tree. A state corresponds to a configuration of the world.
nodes are on particular paths, as defined by PARENT pointers, whereas
states are not.
Two different nodes can contain the same world state if that state is generated via
two different search paths.
48
Given the components for a parent node, it is easy to see how to compute
the necessary components for a child node. The function CHILD-NODE takes
a parent node and an action and returns the resulting child node:
49
Search Algorithm using Queue Data Structure
The frontier needs to be stored in such a way that the search algorithm can easily choose the next
node to expand according to its preferred strategy
The operations on a queue are as follows:
• EMPTY?(queue) returns true only if there are no more elements in the queue.
• POP(queue) removes the first element of the queue and returns it.
• INSERT(element, queue) inserts an element and returns the resulting queue
Three common variants of queue are
◦first-in, first-out or FIFO queue, which pops the oldest element of the queue;
◦last-in, first-out or LIFO queue (also known as a stack), which pops the newest element of the
queue
◦ priority queue, which pops the element of the queue with the highest priority according to
some
ordering function.
50
The explored set can be implemented with a hash table to allow
efficient checking for repeated states.
One must take care to implement the hash table with the right
notion of equality between states
51
Measuring problem-solving performance
We can evaluate an algorithm’s performance in four ways:
• Completeness: Is the algorithm guaranteed to find a solution when there is
one?
• Optimality: Does the strategy find the optimal solution?
• Time complexity: How long does it take to find a solution?
• Space complexity: How much memory is needed to perform the search?
Complexity is expressed in terms of three quantities: b, the branching factor or
maximum number of successors of any node; d, the depth of the shallowest goal
node and m, the maximum length of any path in the state space.
52
53
DIFFERENT TYPES OF SEARCHING
54
Uninformed Search Strategies
no additional information about states beyond that provided in the
problem definition
they that do not take into account the location of the goal. These
algorithms ignore where they are going until they find a goal and
report success.
55
1. Breadth-First search
root node is expanded first, then all the successors of the root node are expanded next, then their
successors, and so on.
all the nodes are expanded at a given depth in the search tree before any nodes at the next level are
expanded.
Breadth-first search is an instance of the general graph-search algorithm in which the shallowest unexpanded
node is chosen for expansion.
This is achieved very simply by using a FIFO queue for the frontier new nodes (which are always deeper than
their parents) go to the back of the queue and old nodes, which are shallower than the new nodes, get
expanded first the goal test is applied to each node when it is generated rather than when it is selected for
expansion breadth-first search always has the shallowest path to every node on the frontier.
56
57
58
Breadth-first
search
A S D
B D A E
C E B B F
E
11
D F B F C E A C G
14 17 15 15 13
G C F
G
G 25
19 19 17
59
Problem solving performance-BFS
complete—if the shallowest goal node is at some finite depth d, breadth-first search will
eventually find it after generating all shallower nodes not optimal one: breadth-first search is
optimal if the path cost is a nondecreasing function of the depth of the node. The most common
such scenario is that all actions have the same cost.
Time Complexity: The root of the search tree generates b nodes at the first level, each of which
generates b more nodes, for a total of b2 at the second level. Each of these generates b more nodes,
yielding b3 nodes at the third level, and so on. Now suppose that the solution is at depth d. In the worst
case, it is the last node generated at that level. Then the total number of nodes generated is b + b2 + b3 +
··· + bd = O(bd).
Space Complexity: every node generated remains in memory. There will be O(bd−1) nodes in the explored
60
61
Summary
the memory requirements are a bigger problem for breadth-first search than
is the execution time
One might wait 13 days for the solution to an important problem with search
depth 12, but no personal computer has the petabyte of memory it would
take.
If your problem has a solution at depth 16, then it will take about 350 years
for breadth-first search to find it.
In general, exponential-complexity search problems cannot be solved by
uninformed methods for any but the smallest instances.
62
2. Uniform-cost search
Instead of expanding the shallowest node, uniform-cost search
expands the node n with the lowest path cost g(n). This is done by
storing the frontier as a priority queue ordered by g.
65
Problem solving performance-UCS
Complete: guaranteed provided the cost of every step exceeds some small
positive constant ε
Optimal: optimal path to that node has been found. because step costs are
nonnegative, paths never get shorter as nodes are added. uniform-cost search
expands nodes in order of their optimal path cost.
67
Depth-first search
Depth-first search always expands the deepest node
in the current frontier of the search tree.
The search proceeds immediately to the deepest
level of the search tree, where the nodes have no
successors.
As those nodes are expanded, they are dropped from
the frontier, so then the search “backs up” to the next
deepest node that still has unexplored successors.
Depth-first search uses a LIFO queue
A LIFO queue means that the most recently
generated node is chosen for expansion. This
must be the deepest unexpanded node because it
is one deeper than its parent—which, in turn, was
the deepest unexpanded node when it was selected
68
Depth-first search
S
A D
B D A E
C E B B F
E
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17
G 25
69
Problem solving performance-DFS
Completeness:
◦depth-first search is implemented with a recursive function that calls itself on each of its
children in turn.
◦The properties of depth-first search depend strongly on whether the graph-search or
◦tree-search version is used.
◦The graph-search version, which avoids repeated states and redundant paths, is complete
in finite state spaces because it will eventually expand every node.
◦The tree-search version, on the other hand, is not complete
◦Depth-first tree search can be modified at no extra memory cost so that it checks new
states against those on the path from the root to the current node;
◦this avoids infinite loops in finite state spaces but does not avoid the proliferation of
redundant paths.
◦In infinite state spaces, both versions fail if an infinite non-goal path is encountered.
70
Not optimal
◦depth- first search will explore the entire left subtree even if node C is a goal node.
◦If node J were also a goal node, then depth-first search would return it as a solution
instead of C, which would be a better solution; hence, depth-first search is not optimal.
Time complexity
◦depth-first graph search is bounded by the size of the state space
◦A depth-first tree search, on the other hand, may generate all of the O(bm) nodes in the
search tree, where m is the maximum depth of any node; this can be much greater than
the size of the state space.
◦m itself can be much larger than d (the depth of the shallowest solution) and is infinite if
the tree is unbounded.
71
Space complexity
◦a depth-first tree search needs to store only a single path from the root to a leaf node,
along with the remaining unexpanded sibling nodes for each node on the path.
◦Once a node has been expanded, it can be removed from memory as soon as all its
descendants have been fully explored.
◦For a state space with branching factor b and maximum depth m, depth-first search
requires storage of only O(bm) nodes.
◦assuming that nodes at the same depth as the goal node have no successors, we find that
depth-first search would require 156 kilobytes instead of 10 exabytes at depth d = 16,
a factor of 7 trillion times less space.
72
Backtracking search
A variant of depth-first search called backtracking search uses still less memory
only one successor is generated at a time rather than all successors; each
partially expanded node remembers which successor to generate next.
Only O(m) memory is needed rather than O(bm).
Backtracking search facilitates the idea of generating a successor by
modifying
the current state description directly rather than copying it first.
This reduces the memory requirements to just one state description and O(m)
actions.
For this to work, we must be able to undo each modification when we go back to
generate the next successor
73
Depth-limited
search
depth-first search with a predetermined depth limit l
nodes at depth l are treated as if they have no
successors. The depth limit solves the infinite-path
problem.
It also introduces an additional source of incompleteness if we choose l<d,
that is, the shallowest goal is beyond the depth limit.
Depth-limited search will also be non optimal if we choose l
>d. Its time complexity is O(bl ) and its space complexity is O(bl
)
Depth-first search can be viewed as a special case of depth-
limited search with
l=∞. 74
Depth-limited search
It is depth-first search
◦with a predefined maximum depth
◦However, it is usually not easy to define the suitable maximum
depth
◦too small no solution can be found
◦too large the same problems are suffered from
75
Depth-limited
search depth = 3
S
A D 3
6
B D A E
C E B B F
E
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
76
77
Iterative deepening
depth- first search
Iterative deepening search is often
used in combination with depth-first
tree search, that finds the best
depth limit.
It does this by gradually increasing
the limit—first 0, then 1, then 2,
and so on until a goal is found.
This will occur when the depth
limit reaches d, the depth of the
shallowest goal node
78
79
Iterative deepening search may s e e m w a s t e f u l b e c a u s e s t a t e s
Vis it http s://w ww. yo utu b e.c o m/ c/s ha rik atr fo rm y vide o le ctu re s
which gives a time complexity of O(bd) asymptotically the same as breadth-first search.
Iterative deepening search is analogous to breadth-first search in that it explores a
complete layer of new nodes at each iteration before going on to the next layer
iterative deepening is the preferred uninformed search method when the search space is
large and the depth of the solution is not known
80
optimal
complete
Time and space complexities
◦reasonable
81
Complete? Yes
Space? O(bd)
82
83
INFORMED (HEURISTIC)
SEARCH STRATEGIES
problem-specific knowledge beyond the definition of the problem itself can find
solutions more efficiently than an uninformed strategy
84
BEST-FIRST SEARCH
Best-first search is an instance of the general TREE-SEARCH or GRAPH-SEARCH algorithm in
which a node is selected for expansion based on an evaluation function, f(n).
The evaluation function is construed as a cost estimate, so the node with the lowest evaluation
is expanded first.
The implementation of best-first graph search is identical to that for uniform-cost search except for
the use of f instead of g to order the priority queue
The choice of f determines the search strategy.
Most best-first algorithms include as a component of f a heuristic function, denoted h(n): h(n)
= estimated cost of the cheapest path from the state at node n to a goal state.
if n is a goal node, then h(n)=0.
For example, in Romania, one might estimate the cost of the cheapest path from Arad to
Bucharest via the straight-line distance from Arad to Bucharest.
85
Greedy best-first
search
Greedy best-first search tries to expand the node that is closest to the goal, on the
grounds that this is likely to lead to a solution quickly.
It evaluates nodes by using just the heuristic function; that is, f(n) = h(n).
86
Greedy best-first search- Route
finding problems in Romania
straight line distance heuristic, Hsld
87
88
89
90
91
92
93
94
95
96
97
A* search: Minimizing
the total estimated
solution
It evaluates nodes bycost
combining g(n), the cost to reach the node, and h(n), the
cost to get from the node to the goal:
f(n) = g(n) + h(n) .
Since g(n) gives the path cost from the start node to node n, and h(n) is the
estimated cost of the cheapest path from n to the goal, we have
f(n) = estimated cost of the cheapest solution through n
98
if we are trying to find the cheapest solution, a reasonable thing to try first is
the node with the lowest value of g(n) + h(n).
It turns out that this strategy is more than just reasonable: provided that
the heuristic function h(n) satisfies certain conditions,
A∗ search is both complete and optimal.
The algorithm is identical to UNIFORM-COST-SEARCH except that A∗ uses g +
h instead of g.
99
Progress of an A∗ tree
search
100
101
102
103
104
Bucharest first appears on the frontier here but it is not selected for expansion
(450) is higher
because than that of Pitesti (417).
its f-cost
Another way to say this is that there might be a solution through Pitesti whose cost is
as low as 417, so the algorithm will not settle for a solution that costs 450.
105
106
Conditions for optimality:
Admissibility
The first condition we require for optimality is that h(n) be an admissible heuristic.
An admissible heuristic is one that never overestimates the cost to reach the
goal. g(n) is the actual cost to reach n along the current path, and
f(n) = g(n) + h(n), we have as an immediate consequence that f(n) never
overestimates the true cost of a solution along the current path through n.
Admissible heuristics are by nature optimistic because they think the cost of
solving the
problem is less than it actually is
◦Eg, straight-line distance hSLD that we used in getting to Bucharest is n
admissible heuristic
◦because the shortest path between any two points is a straight line
◦Straight line cannot be an overestimate
107
Conditions for optimality:
Consistency
It is required only for applications of A∗ to graph search
A heuristic h(n) is consistent if, for every node n and every successor n of n
generated by any action a, the estimated cost of reaching the goal from n is no
greater than the step cost of getting to n plus the estimated cost of reaching the goal
from n :
h(n) ≤ c(n, a, n ) + h(n )
This is a form of the general triangle inequality, which stipulates that each side of a
triangle cannot be longer than the sum of the other two sides
◦Here, the triangle is formed by n, n , and the goal Gn closest to n.
◦For an admissible heuristic, the inequality makes perfect sense:
if there were a route from n to Gn via n that was cheaper than h(n), that would violate
the
property that h(n) is a lower bound on the cost to reach Gn.
108
Optimality of A*
the tree-search version of A∗ is optimal if h(n) is admissible, while the graph-search version
is optimal if h(n) is consistent.
A∗ expands no nodes with f(n) > C∗—for example, Timisoara is not expanded in even
though it is
a child of the root
the subtree below Timisoara is pruned; because hSLD is admissible, the algorithm can
safely ignore this subtree while still guaranteeing optimality
Pruning eliminates possibilities from consideration without having to examine
them A∗ is optimally efficient for any given consistent heuristic.
◦That is, no other optimal algorithm is guaranteed to expand fewer nodes than
A∗
◦This is because any algorithm that does not expand all nodes with f(n) < C∗
runs the risk of
missing the optimal solution.
109
if h(n) is consistent, then the
values of f(n) along any path
are
The proofnondecreasing.
follows directly from the definition of consistency.
Suppose n is a successor of n; then g(n ) = g(n) + c(n, a, n ) for some action a,
and we have
f(n ) = g(n ) + h(n ) = g(n) + c(n, a, n ) + h(n ) ≥ g(n) + h(n) = f(n) .
110
whenever A∗ selects a node n for
expansion, the optimal path to that
node has
Were this not been
the case, found.
there would have to be another frontier node n on
the optimal path from the start node to n
because f is nondecreasing along any path, n would have lower f-cost than n
and would have been selected first
111
the sequence of nodes expanded by A∗ using GRAPH-SEARCH is
in nondecreasing order of f(n).
Hence, the first goal node selected for expansion must be an
optimal solution because f is the true cost for goal nodes (which
have h = 0) and all later goal nodes will be at least as expensive.
112
Contours in the state space
Inside the contour labeled
400, all nodes have f(n)
less than or equal to 400,
and so on.
Then, because A∗
expands the frontier node
of lowest f-cost, we can
see that an A∗ search
fans out from the start
node, adding nodes in
concentric bands of
increasing f-cost.
113
If C∗ is the cost of
the
optimal solution path, then we can say the following:
• A∗ expands all nodes with f(n) < C∗.
•A∗ might then expand some of the nodes right on the “goal contour”
(where f(n) = C∗) before selecting a goal node.
114
Disadvantages of A*
Algorithm
the number of states within the goal contour search space is still exponential
in the length of the solution.
115
HEURISTIC FUNCTIONS
•8-puzzle search space
◦Typical solution length: 20
steps
◦Average branching factor: 3
◦Exhaustive search: 320=3.5 x 109
◦Bound on unique states: 9!
= 362,880
116
Admissible Heuristics
h1 = the number of misplaced tiles. For Figure 3.28, all of the eight tiles are
out of position, so the start state would have h1 = 8. h1 is an admissible
heuristic because it is clear that any tile that is out of place must be moved at
least once.
•h2 = the sum of the distances of the tiles from their goal positions. Because
tiles cannot move along diagonals, the distance we will count is the sum of the
horizontal and vertical distances. This is sometimes called the city block
distance or Manhattan distance. h2 is also admissible because all any move can
do is move one tile one step closer to the goal. Tiles 1 to 8 in the start state give
a Manhattan distance of
◦h2 = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18
117
Heuristic Performance
Experiments on sample problems can determine the number of nodes searched and CPU
time for different strategies.
One other useful measure is effective branching factor: If a method expands N nodes to
find solution of depth d, and a uniform tree of depth d would require a branching factor of
b* to contain N nodes, the effective branching factor is b*
◦N = 1 + b* + (b*)2 + ...+ (b*)d
118
Experimental Results on 8-puzzle
problems
119
Quality of Heuristics
Since A* expands all nodes whose f value is less than that of an optimal solution, it is
always better to use a heuristic with a higher value as long as it does not over-estimate.
Therefore h2 is uniformly better than h1 , or h2 dominates h1 .
A heuristic should also be easy to compute, otherwise the overhead of computing the
heuristic could outweigh the time saved by reducing search (e.g. using full breadth-first search
to estimate distance wouldn’t help)
120
Inventing Heuristics
Many good heuristics can be invented by considering relaxed versions of the
problem (abstractions).
For 8-puzzle: A tile can move from square A to B if A is adjacent to B and B is blank
◦(a) A tile can move from square A to B if A is adjacent to B.
◦(b) A tile can move from square A to B if B is blank. (c) A tile can move from square A to B.
If there are a number of features that indicate a promising or unpromising state, a weighted
sum of these features can be useful. Learning methods can be used to set weights.
121