AI MODULE-2
AI MODULE-2
Module -II
Introduction
Goal formulation,based on the current situation and the agent’s performance measure,is the first
step in problem solving.
The agent’s task is to find out which sequence of actions will get to a goal state.
1
Problem formulation is the process of deciding what actions and states to consider given a goal.
Formulate problem:
states: various cities
actions: drive between cities
Find solution:
sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest
Problem formulation
A problem is defined by four items:
initial state e.g., “at Arad"
successor function S(x) = set of action-state pairs
e.g., S(Arad) = {[Arad -> Zerind;Zerind],….}
goal test, can be
explicit, e.g., x = at Bucharest"
implicit, e.g., NoDirt(x)
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.
Search
An agent with several immediate options of unknown value can decide what to do by examining
different possible sequences of actions that leads to the states of known value,and then choosing the
best sequence. The process of looking for sequences actions from the current state to reach the goal
state is called search.
The search algorithm takes a problem as input and returns a solution in the form of action
sequence. Once a solution is found,the execution phase consists of carrying out the recommended
action..
1
function SIMPLE-PROBLEM-SOLVING-AGENT( percept) returns an action
inputs : percept, a percept
static: seq, an action sequence, initially empty
state, some description of the current world state
goal, a goal, initially null
problem, a problem formulation
state UPDATE-STATE(state, percept)
if seq is empty then do
goal FORMULATE-GOAL(state)
problem FORMULATE-PROBLEM(state, goal)
seq SEARCH( problem)
action FIRST(seq);
seq REST(seq)
return action
Figure 1.18 A Simple problem solving agent. It first formulates a goal
problem,searches for a sequence of actions that would solve a problem,and executes the actions
one at a time.
Figure 1.18 shows a simple “formulate,search,execute” design for the agent. Once solution has been
executed,the agent will formulate a new goal.The agent design assumes the Environment is
The agent design assumes the Environment is
• Static : The entire process carried out without paying attention to changes that
might be occurring in the environment.
• Observable : The initial state is known and the agent’s sensor detects all aspects that
are relevant to the choice of action
• Discrete : With respect to the state of the environment and percepts and actions so
that alternate courses of action can be taken
• Deterministic : The next state of the environment is completely determined by the
current state and the actions executed by the agent. Solutions to the problem are
single sequence of actions
An agent carries out its plan with eye closed. This is called an open loop system because ignoring
the percepts breaks the loop between the agent and the environment.
1
A path cost function assigns numeric cost to each action. For the Romania problem the cost
of path might be its length in kilometers.
The step cost of taking action a to go from state x to state y is denoted by c(x,a,y). The step
cost for Romania are shown in figure 1.18. It is assumed that the step costs are non negative.
A solution to the problem is a path from the initial state to a goal state.
An optimal solution has the lowest path cost among all
solutions.
Figure 1.19 A simplified Road Map of part of Romania
EXAMPLE PROBLEMS
The problem solving approach has been applied to a vast array of task environments. Some
best known problems are summarized below. They are distinguished as toy or real-world
problems
A toy problem is intended to illustrate various problem solving methods. It can be easily
used by different researchers to compare the performance of algorithms.
A real world problem is one whose solutions people actually care about.
TOY PROBLEMS
o States: The agent is in one of two locations.,each of which might or might not contain dirt.
Thus there are 2 x 22 = 8 possible world states.
o Initial state: Any state can be designated as initial state.
o Successor function : This generates the legal states that results from trying the three actions
(left, right, suck). The complete state space is shown in figure 2.3
o Goal Test : This tests whether all the squares are clean.
o Path test : Each step costs one ,so that the the path cost is the number of steps in the path.
1
Figure 1.20 The state space for the vacuum world.
Arcs denote actions: L = Left,R = Right,S = Suck
The 8-puzzle
An 8-puzzle consists of a 3x3 board with eight numbered tiles and a blank space. A tile adjacent to
the balank space can slide into the space. The object is to reach the goal state ,as shown in figure 2.4
Example: The 8-puzzle
1
The 24-puzzle (on a 5 x 5 board) has around 1025 states ,and random instances are still quite
difficult to solve optimally with current machines and algorithms.
8-queens problem
The goal of 8-queens problem is to place 8 queens on the chessboard such that no queen
attacks any other.(A queen attacks any piece in the same row,column or diagonal).
Figure 2.5 shows an attempted solution that fails: the queen in the right most column is
attacked by the queen at the top left.
An Incremental formulation involves operators that augments the state description,starting
with an empty state.for 8-queens problem,this means each action adds a queen to the state.
A complete-state formulation starts with all 8 queens on the board and move them around.
In either case the path cost is of no interest because only the final state counts.
REAL-WORLD PROBLEMS
ROUTE-FINDING PROBLEM
Route-finding problem is defined in terms of specified locations and transitions along links
between them. Route-finding algorithms are used in a variety of applications,such as routing in
computer networks,military operations planning,and air line travel planning systems.
1
AIRLINE TRAVEL PROBLEM
The airline travel problem is specifies as follows :
o States : Each is represented by a location(e.g.,an airport) and the current time.
o Initial state : This is specified by the problem.
o Successor function : This returns the states resulting from taking any scheduled
flight(further specified by seat class and location),leaving later than the current time plus
the within-airport transit time,from the current airport to another.
o Goal Test : Are we at the destination by some prespecified time?
o Path cost : This depends upon the monetary cost,waiting time,flight time,customs and
immigration procedures,seat quality,time of dat,type of air plane,frequent-flyer mileage
awards, and so on.
TOURING PROBLEMS
Touring problems are closely related to route-finding problems,but with an important difference.
Consider for example,the problem,”Visit every city at least once” as shown in Romania map.
As with route-finding the actions correspond to trips between adjacent cities. The state space,
however,is quite different.
The initial state would be “In Bucharest; visited{Bucharest}”.
A typical intermediate state would be “In Vaslui;visited {Bucharest,Urziceni,Vaslui}”.
The goal test would check whether the agent is in Bucharest and all 20 cities have been visited.
VLSI layout
A 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 is split into two parts : cell layout and channel
routing.
ROBOT navigation
ROBOT navigation is a generalization of the route-finding problem. Rather than 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 also must be controlled,the search space
becomes multi-dimensional. Advanced techniques are required to make the search space finite.
INTERNET SEARCHING
In recent years there has been increased demand for software robots that perform Internet
searching.,looking for answers to questions,for related information,or for shopping deals. The
1
searching techniques consider internet as a graph of nodes(pages) connected by links.
SEARCHING FOR SOLUTIONS
SEARCH TREE
Having formulated some problems,we now need to solve them. This is done by a search through
the state space. A search tree is generated by the initial state and the successor function that
together define the state space. In general,we may have a search graph rather than a search
tree,when the same state can be reached from multiple paths.
Figure 1.23 shows some of the expansions in the search tree for finding a route from Arad to
Bucharest.
Figure 1.23 Partial search trees for finding a route from Arad to Bucharest. Nodes that have
been expanded are shaded.; nodes that have been generated but not yet expanded are outlined in
bold;nodes that have not yet been generated are shown in faint dashed line
The root of the search tree is a search node corresponding to the initial state,In(Arad). The first
step is to test whether this is a goal state. The current state is expanded by applying the successor
function to the current state,thereby generating a new set of states. In this case,we get three new
states: In(Sibiu),In(Timisoara),and In(Zerind). Now we must choose which of these three
possibilities to consider further. This is the essense of search- following up one option now and
putting the others aside for latter,in case the first choice does not lead to a solution.
Search strategy . The general tree-search algorithm is described informally in Figure 1.24
.
Tree Search
1
The choice of which state to expand is determined by the search strategy. There are an infinite
number paths in this state space ,so the search tree has an infinite number of nodes.
A node is a data structure with five components :
o STATE : a state in the state space to which the node corresponds;
o PARENT-NODE : the node in the search tree that generated this node;
o ACTION : the action that was applied to the parent to generate the node;
o PATH-COST :the cost,denoted by g(n),of the path from initial state to the node,as
indicated by the parent pointers; and
o DEPTH : the number of steps along the path from the initial state.
It is important to remember the distinction between nodes and states. A node is a book keeping
data structure used to represent the search tree. A state corresponds to configuration of the world.
Fringe
Figure 1.25 Nodes are data structures from which the search tree is
constructed. Each has a parent,a state, Arrows point from child to parent.
Fringe is a collection of nodes that have been generated but not yet been expanded. Each element
of the fringe is a leaf node,that is,a node with no successors in the tree.
The collection of these nodes is implemented as a queue.
12
The general tree search algorithm is shown in Figure 2.9
12
UNINFORMED SEARCH STRATGES
Uninformed Search Strategies have no additional information about states beyond that provided
in the problem definition.
Strategies that know whether one non goal state is “more promising” than another are called
Informed search or heuristic search strategies.
Breadth-first search
Breadth-first search is a simple strategy in which the root node is expanded first,then all
successors of the root node are expanded next,then their successors,and so on. In general,all the
nodes are expanded at a given depth in the search tree before any nodes at the next level are
expanded.
Breath-first-search is implemented by calling TREE-SEARCH with an empty fringe that is a
first-in-first-out(FIFO) queue,assuring that the nodes that are visited first will be expanded
first. In otherwards,calling TREE-SEARCH(problem,FIFO-QUEUE()) results in breadth-first-
search. The FIFO queue puts all newly generated successors at the end of the queue,which
means that Shallow nodes are expanded before deeper nodes.
Figure 1.27 Breadth-first search on a simple binary tree. At each stage ,the node to be expanded next
is indicated by a marker.
Properties of breadth-first-search
12
Figure 1.28 Breadth-first-search properties
UNIFORM-COST SEARCH
Instead of expanding the shallowest node,uniform-cost search expands the node n with the
lowest path cost. uniform-cost search does not care about the number of steps a path has,but only
about their total cost.
12
Figure 1.30 Properties of Uniform-cost-search
2.5.1.3 DEPTH-FIRST-SEARCH
Depth-first-search always expands the deepest node in the current fringe of the search tree. The
progress of the search is illustrated in figure 1.31. 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 fringe,so then the search “backs up” to the next shallowest
node that still has unexplored successors.
12
Figure 1.31 Depth-first-search on a binary tree. Nodes that have been expanded and have no
descendants in the fringe can be removed from the memory;these are shown in black. Nodes at
depth 3 are assumed to have no successors and M is the only goal node.
Depth-first-search has very modest memory requirements.It 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 the node has been expanded,it can be removed from the memory,as soon as its
descendants have been fully explored.
For a state space with a branching factor b and maximum depth m,depth-first-search requires
storage of only bm + 1 nodes.
Assuming that nodes at the same depth as the goal node have no successors,we find the depth-first-
search would require 118 kilobytes instead of 10 petabytes,a factor of 10 billion times less space.
Drawback of Depth-first-search
The drawback of depth-first-search is that it can make a wrong choice and get stuck going down
very long(or even infinite) path when a different choice would lead to solution near the root of the
search tree. For example ,depth-first-search will explore the entire left subtree even if node C is a
goal node.
BACKTRACKING SEARCH
A variant of depth-first search called backtracking search uses less memory and only one successor
is generated at a time rather than all successors.; Only O(m) memory is needed rather than O(bm)
DEPTH-LIMITED-SEARCH
The problem of unbounded trees can be alleviated by supplying depth-first-search with a pre-
determined depth limit l.That is,nodes at depth l are treated as if they have no successors. This
approach is called depth-limited-search. The depth limit soves the infinite path problem.
Depth limited search will be nonoptimal if we choose l > d. Its time complexity is O(bl) and its
space compleiy is O(bl). Depth-first-search can be viewed as a special case of depth-limited search
with l = oo
Sometimes,depth limits can be based on knowledge of the problem. For,example,on the map of
Romania there are 20 cities. Therefore,we know that if there is a solution.,it must be of length 19 at
the longest,So l = 10 is a possible choice. However,it oocan be shown that any city can be reached
from any other city in at most 9 steps. This number known as the diameter of the state space,gives
us a better depth limit.
Depth-limited-search can be implemented as a simple modification to the general tree-search
algorithm or to the recursive depth-first-search algorithm. The pseudocode for recursive depth-
12
limited-search is shown in Figure 1.32.
12
It can be noted that the above algorithm can terminate with two kinds of failure : the standard
failure value indicates no solution; the cutoff value indicates no solution within the depth limit.
Depth-limited search = depth-first search with depth limit l,
returns cut off if any path is cut off by depth limit
Figure 1.33 The iterative deepening search algorithm ,which repeatedly applies depth-limited-
search with increasing limits. It terminates when a solution is found or if the depth limited search
resturns failure,meaning that no solution exists.
12
Figure 1.34 Four iterations of iterative deepening search on a binary tree
12
Iterative deepening search
S S S
Limit = 0 A D
Limit = 1
S S S
A D A D
Limit = 2 B D A E
Figure 1.35
Figure 1.36
12
In general,iterative deepening is the prefered uninformed search method when there is a
large search space and the depth of solution is not known.
Bidirectional Search
The idea behind bidirectional search is to run two simultaneous searches – one forward from the
initial state and
the other backward from the goal,
stopping when the two searches meet in the middle (Figure 1.37)
The motivation is that bd/2 + bd/2 much less than ,or in the figure ,the area of the two small circlesis
less than the area of one big circle centered on the start and reaching to the goal.
Figure 1.38 Evaluation of search strategies,b is the branching factor; d is the depth of the
shallowest solution; m is the maximum depth of the search tree; l is the depth limit. Superscript
caveats are as follows: a complete if b is finite; b complete if step costs >= E for positive E; c
optimal if step costs are all identical; d if both directions use breadth-first search.
12
finite size. Considering search tree up to a fixed depth, eliminating repeated states yields an
exponential reduction in search cost. Repeated states, can cause a solvable problem to become
unsolvable if the algorithm does not detect them. Repeated states can be the source of great
inefficiency: identical sub trees will be explored manytimes!
A A
B B B
C C C C C
Figure 1.39
Figure
Figure 1.41 The General graph search algorithm. The set closed can be implemented with a
hash table to allow efficient checking for repeated states.
12
o Using more memory in order to check repeated state
o Algorithms that forget their history are doomed to repeat it.
o Maintain Close-List beside Open-List(fringe)
Strategies for avoiding repeated states
We can modify the general TREE-SEARCH algorithm to include the data structure called the
closed list , which stores every expanded node. The fringe of unexpanded nodes is called the
open list. If the current node matches a node on the closed list, it is discarded instead of being
expanded. The new algorithm is called GRAPH-SEARCH and much more efficient than TREE-
SEARCH. The worst case time and space requirements may be much smaller than O(bd).
SEARCHING WITH PARTIAL INFORMATION
o Different types of incompleteness lead to three distinct problem types:
o Sensorless problems (conformant): If the agent has no sensors at all
o Contingency problem: if the environment if partially observable or if
action are uncertain (adversarial)
o Exploration problems: When the states and actions of the environment
are unknown.
o No sensor
o Initial State(1,2,3,4,5,6,7,8)
o After action [Right] the state (2,4,6,8)
o After action [Suck] the state (4, 8)
o After action [Left] the state (3,7)
o After action [Suck] the state (8)
o Answer : [Right,Suck,Left,Suck] coerce the world into state 7 without any
sensor
o Belief State: Such state that agent belief to be there (SLIDE 7) Partial knowledge
of states and actions:
– sensorless or conformant problem
– Agent may have no idea where it is; solution (if any) is a sequence.
– contingency problem
– Percepts provide new information about current state; solution is a tree or
policy; often interleave search and execution.
– If uncertainty is caused by actions of another agent: adversarial problem
– exploration problem
– When states and actions of the environment are unknown.
Figure
12
12
Figure
– Select actions based on contingencies arising during execution. Time and space
complexity are always considered with respect to some measure of the problem difficulty.
– In theoretical computer science, the typical measure is the size of the state space.
12
In AI, where the graph is represented implicitly by the initial state and successor function, thecomplexity is
expressed in terms of three quantities:
Search-cost - typically depends upon the time complexity but can also include the term ormemory usage.
Total–cost – It combines the search-cost and the path cost of the solution found.
h(n):h(n) = estimated cost of the cheapest path from node n to a goal node.
For example ,in Romania, one might estimate the cost of the cheapest path from Arad to Bucharestvia a
12
Heuristic function are the most common form in which additional knowledge is imparted to the
search algorithm.
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 a solution quickly. It evaluates the nodes by using the heuristic function
f(n) = h(n).
Taking the example of Route-finding problems in Romania , the goal is to reach Bucharest starting
from the city Arad. We need to know the straight-line distances to Bucharest from various cities as
shown in Figure 2.1. For example, the initial state is In(Arad) ,and the straight line distance
heuristic hSLD(In(Arad)) is found to be 366.
Using the straight-line distance heuristic hSLD ,the goal state can be reached faster.
12
Figure 2.2 stages in greedy best-first search for Bucharest using straight-line distance heuristichSLD.
Nodes are labeled with their h-values.
Figure 2.2 shows the progress of greedy best-first search using hSLD to find a path from Arad to
Bucharest. The first node to be expanded from Arad will be Sibiu,because it is closer to Bucharest
than either Zerind or Timisoara. The next node to be expanded will be Fagaras,because it is closest.
Fagaras in turn generates Bucharest,which is the goal.
Properties of greedy search
o Complete?? No–can get stuck in loops, e.g.,
Iasi ! Neamt ! Iasi ! Neamt !
Complete in finite space with repeated-state checking
o Time?? O(bm), but a good heuristic can give dramatic improvement
o Space?? O(bm)—keeps all nodes in memory
12
12
o Optimal?? No
A*
Search
A* Search is the most widely used form of best-first search. The evaluation function f(n) is
obtained by combining
(1) g(n) = the cost to reach the node,and
(2) h(n) = the cost to get from the node to the goal :
f(n) = g(n) + h(n).
A Search is both optimal and complete. A* is optimal if h(n) is an admissible heuristic. The obvious
*
12
Figure 2.5 shows how RBFS reaches Bucharest.
Figure 2.3 Stages in A* Search for Bucharest. Nodes are labeled with f = g + h . The h-values arethe
straight-line distances to Bucharest taken from figure 2.1
12
function RECURSIVE-BEST-FIRST-SEARCH(problem) return a solution or failure
return RFBS(problem,MAKE-NODE(INITIAL-STATE[problem]),∞)
function RFBS( problem, node, f_limit) return a solution or failure and a new f-cost
limit
if GOAL-TEST[problem](STATE[node]) then return node
successors EXPAND(node, problem)
if successors is empty then return failure, ∞
for each s in successors do
f [s] max(g(s) + h(s), f [node])
repeat
best the lowest f-value node in successors
if f [best] > f_limit then return failure, f [best]
alternative the second lowest f-value among successorsresult, f
[best] RBFS(problem, best, min(f_limit, alternative))
if result failure then return result
Figure 2.4 The algorithm for recursive best-first search
12
Figure 2.5 Stages in an RBFS search for the shortest route to Bucharest. The f-limit value for each
recursive call is shown on top of each current node. (a) The path via Rimnicu Vilcea is followed
until the current best leaf (Pitesti) has a value that is worse than the best alternative path (Fagaras).
(b) The recursion unwinds and the best leaf value of the forgotten subtree (417) is backed up to
Rimnicu Vilcea;then Fagaras is expanded,revealing a best leaf value of 450.
(c) The recursion unwinds and the best leaf value of the forgotten subtree (450) is backed upto
Fagaras; then Rimni Vicea is expanded. This time because the best alternative path(through
Timisoara) costs atleast 447,the expansion continues to Bucharest
12
RBFS Evaluation :
RBFS is a bit more efficient than IDA*
– Still excessive node generation (mind changes)
Like A*, optimal if h(n) is admissible
Space complexity is O(bd).
– IDA* retains only one single number (the current f-cost limit)
Time complexity difficult to characterize
– Depends on accuracy if h(n) and how often best path changes.
IDA* en RBFS suffer from too little memory.
Heuristic Functions
A heuristic function or simply a heuristic is a function that ranks alternatives in various search
algorithms at each branching step basing on an available information in order to make a decision
which branch is to be followed during a search.
The 8-puzzle
The 8-puzzle is an example of Heuristic search problem. The object of the puzzle is to slide the tiles
horizontally or vertically into the empty space until the configuration matches the goal
configuration(Figure 2.6)
The average cost for a randomly generated 8-puzzle instance is about 22 steps. The branching factor
is about 3.(When the empty tile is in the middle,there are four possible moves;when it is in the
corner there are two;and when it is along an edge there are three). This means that an exhaustive
search to depth 22 would look at about 322 approximately = 3.1 X 1010 states.
By keeping track of repeated states,we could cut this down by a factor of about 170,000,because
there are only 9!/2 = 181,440 distinct states that are reachable. This is a manageable number ,but the
corresponding number for the 15-puzzle is roughly 1013.
If we want to find the shortest solutions by using A*,we need a heuristic function that never
overestimates the number of steps to the goal.
The two commonly used heuristic functions for the 15-puzzle are:
12
(1) h1 = the number of misplaced tiles.
For figure 2.6 ,all of the eight tiles are out of position, so the start state would have h1 = 8. h1 is an
admissible heuristic.
(2) h2 = the sum of the distances of the tiles from their goal positions. This is called the city
block distance or Manhattan distance.
h2 is admissible ,because all any move can do is move one tile one step closer to the goal.
Tiles 1 to 8 in start state give a Manhattan distance of
h2 = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18.
Figure 2.7 Comparison of search costs and effective branching factors for the ITERATIVE-
DEEPENING-SEARCH and A* Algorithms with h1,and h2. Data are average over 100 instances of
the 8-puzzle,for various solution lengths.
12
Inventing admissible heuristic functions
Relaxed problems
o A problem with fewer restrictions on the actions is called a relaxed problem
o The cost of an optimal solution to a relaxed problem is an admissible heuristic
for theoriginal problem
o If the rules of the 8-puzzle are relaxed so that a tile can move anywhere, then
h1(n) gives theshortest solution
o If the rules are relaxed so that a tile can move to any adjacent square, then
h2(n) gives theshortest solution
32