Ai Mod2 (Highlighted)
Ai Mod2 (Highlighted)
PROBLEM-SOLVING AGENTS
Problem-solving agents is one kind of goal-based agent it uses atomic representations that is,
states of the world are considered as wholes, with no internal structure visible to the problem-
solving algorithms.
Goal-based agents that use more advanced factored or structured representations are usually
called planning agents.
Goals help organize behavior by limiting the objectives that the agent is trying to achieve and
hence the actions it needs to consider.
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 to goal state some actions are required.
Actions are the operators causing transitions between world states.
Actions should be abstract enough at a certain degree, instead of very detailed. E.g.,
turn left VS turn left 30 degrees, etc. 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
Problem formulation
It is 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 actionsat the level of driving from one major town to another.
Each state therefore corresponds to being in a particular town.
o Initial State: This state requires an initial state for the problem which starts the AI agent
towards a specified goal. In this state new methods also initialize problem domain solving
by a specific class.
o Action: This stage of problem formulation works with function with a specific class taken
from the initial state and all possible actions done in this stage.
o Transition: This stage of problem formulation integrates the actual action done by the
previous action stage and collects the final stage to forward it to their next stage.
o Goal test: This stage determines that the specified goal achieved by the integrated
1
transition model or not, whenever the goal achieves stop the action and forward into the
next stage to determines the cost to achieve the goal.
o Path costing: This component of problem-solving numerical assigned what will be the cost
to achieve the goal. It requires all hardware software and human working cost.
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.
Search
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.
Agent has a “formulate, search, execute” design
Searching Process
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. Ignoring
the percepts breaks the loop between agent and environment.
2
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.
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.
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
Formulating problems
Evaluation Criteria
Problem-Solving Agents
Example: Romania
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
Example Problems
Toy Problem is intended to illustrate or exercise various problem-solving methods. E.g.,
puzzle, chess, etc.
A real-world problem is one whose solutions people actually care about. E.g., Design,
planning, etc.
Toy problems
1. Vacuum World
Compared with the real world, this toy problem has discrete locations, discrete dirt, reliable
cleaning, and it never gets any dirtier.
5
6
2. 8-puzzle
A tile adjacent to the blank space can slide into the space. The object is to reach a specified
goal state
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.
3. 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.
7
There are two main kinds of formulation
◦ An incremental 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
◦ the right formulation makes a big difference to the size of the search space
Incremental formulation
8
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 —a big improvement, but not enough to make the problem tractable
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.
2. 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.
9
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
4. 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.
5. 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.
10
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.
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
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 here is how they choose which state to expand next
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
11
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).
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.
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,
12
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 solution.
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
tothe node, as indicated by the parent pointers.
13
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.
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:
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
The explored set can be implemented with a hash table to allow efficient checking for repeated
states. With a good implementation, insertion and lookup can be done in roughly constant time
14
no matter how many states are stored. One must take care to implement the hash table with the
right notion of equality between states.
15
NOTE
1. Breadth-First search
The root node is expanded first, then all the successors of the root node are expanded
next, thentheir 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 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.
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.
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.
the goal test is applied to a node when it is selected for expansion because the first goal
node that is generated may be on a suboptimal path a test is added in case a better path is
found to anode currently on the frontier.
17
Here the problem is to get from Sibiu to Bucharest. The successors of Sibiu are
Rimnicu Vilcea and Fagaras, with costs 80 and 99, respectively. The least-cost node,
Rimnicu Vilcea, is expanded next, adding Pitesti with cost 80 + 97 = 177. The least-
cost node is now Fagaras, so it is expanded, adding Bucharest with cost 99 + 211 =
310.
Now a goal node has been generated, but uniform-cost search keeps going, choosing
Pitesti for expansion and adding a second path to Bucharest with cost 80+ 97+ 101 =
278. Now the algorithm checks to see if this new path is better than the old one; it is,
so the old one is discarded. Bucharest, now with g-cost 278, is selected for expansion
and the solution is returned.
18
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.
Time Complexity: # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε)) where
C* is the cost of the optimal solution
Space Complexity: # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε))
Uniform-cost search is guided by path costs rather than depths, so its complexity
is not easilycharacterized in terms of b and d. Let C∗ be the cost of the optimal
solution and that every action costs at least ε. Then the algorithm’s worst-case
time and space complexity iswhich can be much greater than bd. This is because
uniform cost search can explore large trees of small steps before exploring paths
involving large and perhaps useful steps.
When all step costs are equal is just bd+1. When all step costs are
the same, uniform-cost search is similar to breadth-first search, except that bfs
stops as soon as it generates a goal, whereas uniform-cost search examines all
the nodes at the goal’s depth to see if one has a lower cost thus uniform-cost
search does strictly more work by expanding nodes at depth d unnecessarily
3. 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 haveno 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
19
Problem solving performance-DFS
1. 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.
2. 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.
3. Time complexity
◦ depth-first graph search is bounded by the size of the state space
20
◦ A depth-first tree search, on the other hand, may generate all of the O(b m) 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
4. 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.
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
4. 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 if it is too small then no solution can be found, if it is too
large then the same problems are suffered from. Anyway, the search is complete but still not
optimal
21
5. 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.
22
Iterative deepening search may seem wasteful because states are generated multiple times but
it is is not too costly. Because search tree with the same (or nearly the same) branching factor
at each level, most of the nodes are in the bottom level, so it does not matter much that the
23
upper levels are generated multiple times. The nodes on the bottom level (depth d) are
generated once, those on the next-to-bottom level are generated twice, and so on, up to the
children of the root, which are generated d times.
So the total number of nodes generated in the worst case is
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
1. 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.
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.
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).
24
Greedy best-first search- Route finding problems in Romania
Here the heuristic is straight line distance, Hsld. If the goal is Bucharest, we need to know the
straight-line distances to Bucharest. hSLD is correlated with actual road distances and is,
therefore, a useful heuristic
25
26
27
A* search: Minimizing the total estimated solution cost
It evaluates nodes by combining g(n), the cost to reach the node, and h(n), the cost to get from
the node to the goal:
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
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.
28
29
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
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.
If h(n) is consistent, then the values of f(n) along any path are nondecreasing. The proof 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) .
Whenever A∗ selects a node n for expansion, the optimal path to that node has been found.
Were this not the case, 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
30
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.
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.
Disadvantages of A* Algorithm
The number of states within the goal contour search space is still exponential in the length of the
solution.
HEURISTIC FUNCTIONS
An 8-puzzle search space has
typical solution length: 20 steps,
Average branching factor: 3
Exhaustive search: 320=3.5 x 109
Bound on unique states: 9! = 362,880
31
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
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
32
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)
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.
33