0% found this document useful (0 votes)
112 views

Searching Algorithms-Problem Solving in AI

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Searching Algorithms-Problem Solving in AI

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Problem Solving Agents

Intelligent agents are supposed to maximize its performance


measure. Achieving this can be simplified if the agent can adopt
a goal and aim to satisfy it.

Setting goals help the agent organize its behavior by limiting the
objectives that the agent is trying to achieve and hence the actions it
needs to consider. This Goal formulation based on the current
situation and the agent’s performance measure is the first step in
problem solving.

We consider the agent’s goal to be a set of states. The agent’s task is


to find out actions in the present and in the future that could reach
the goal state from the present state. Problem formulation is the
process of deciding what actions and states to consider, given a goal.

“ An agent with several immediate


options of unknown value can decide
what to do by first examining the
future actions that eventually lead to
states of known value ”

After Goal formulation and problem formulation, the agent has to


look for a sequence of actions that reaches the goal. This process is
called Search. A search algorithm takes a problem as input and
returns a sequence of actions as output.

After the search phase, the agent has to carry out the actions that are
recommended by the search algorithm. This final phase is
called execution phase.

Formulate — Search — Execute

Well Defined Problems and Solutions


Before we get into more about problem formulating phase, we need to first understand
what a problem is in terms of problem-solving agents.
The problem can be defined formally in five components:
1. Initial State
2. Actions
3. Transition Model
4. Goal Test
5. Path Cost

Initial State

The first component that describes the problem is the initial


state that the agent starts in.

For example, if a taxi agent needs to get to location(B) but the taxi is currently at location(A)

then the initial state of the problem would be location(A).

Actions

The second component that describes the problem is a description of


the possible actions available to the agent. Given a
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.

Transition Model

The third component is the description of what each action does


which is called the transition model. It is specified by a
function Result(s , a) that returns the state that results from doing
action a in state s.

The initial state, actions and transition model together define


the state space of a problem which is a set of all states reachable
from the initial state by any sequence of actions. The state space
forms a graph in which the nodes are states and the links between
the nodes are actions.

Goal Test

The goal test determines whether a given state is a goal state or not.
Sometimes there is an explicit set of possible goal states and the test
simply checks whether the given state is one of them. Sometimes the
goal is specified by an abstract property rather than an explicitly
enumerated set of states.

Path Cost

The last component of the problem is the path cost which is a


function that assigns a numeric cost to each path. The problem-
solving agent chooses a cost function that reflects its own
performance measure.
The solution to the problem is an action sequence that leads from
initial state to goal state and the solution quality is measured by the
path cost function. An optimal solution has the lowest path cost
among all the solutions.
Searching is the universal technique of problem solving in AI.
There are some single-player games such as tile games, Sudoku,
crossword, etc. The search algorithms help you to search for a
particular position in such games.
[

artificial intelligence techniques are used widely to automate systems


that can use the resource and time efficiently. Some of the well-known
problems experienced in everyday life are games and puzzles. Using AI
techniques, we can solve these problems efficiently.
In this sense, some of the most common problems resolved by AI are:
 Travelling Salesman Problem
 Tower of Hanoi Problem
 Water-Jug Problem
 N-Queen Problem
 Chess
 Sudoku
 Crypt-arithmetic Problems
 Magic Squares
 Logical Puzzles and so on.

Measuring Problem-Solving Performance


Completeness
A search algorithm is said to be complete when it gives a solution or returns any
solution for a given random input.
Cost Optimality
If a solution found is best (lowest path cost) among all the solutions identified,
then that solution is said to be an optimal one.
Time complexity
The time taken by an algorithm to complete its task is called time complexity. If
the algorithm completes a task in a lesser amount of time, then it is an efficient
one.
Space complexity
It is the maximum storage or memory taken by the algorithm at any time while
searching.
These properties are also used to compare the efficiency of the different types of
searching algorithms.

Types of search algorithms


Based on the search problems, we can classify the search algorithm as
 Uninformed search – (Agent cannot estimate how far it is from the Goal)
 Informed search – (Agent estimates How far it is from the Goal)
Uninformed search
 The uninformed search algorithm does not have any domain
knowledge such as closeness, location of the goal state, etc.
 it behaves in a brute-force way.
It only knows the information about how to traverse the given tree
and how to find the goal state.
 This algorithm is also known as the Blind search algorithm or
Brute -Force algorithm.

1. Breadth-first search
2. Depth-first search
3. Depth-limited search
4. Iterative deepening depth-first search
5. Bidirectional search
6. Uniform cost search
Breadth-First Search
It starts from the root node, explores the neighbouring nodes first and
moves towards the next level neighbours. It generates one tree at a time
until the solution is found. It can be implemented using FIFO queue data
structure. This method provides shortest path to the solution.
Bidirectional Search
It searches forward from initial state and backward from goal state till both meet
to identify a common state.
The path from initial state is concatenated with the inverse path from the goal
state. Each search is done only up to half of the total path.
It executes two simultaneous searches called forward-search and
backwards-search and reaches the goal state. Here, the graph is
divided into two smaller sub-graphs.

In one graph, the search is started from the initial start state and in
the other graph, the search is started from the goal state. When these
two nodes intersect each other, the search will be terminated.

Consider the below graph.

Here, the start state is E and the goal state is G. In one sub-graph, the search

starts from E and in the other, the search starts from G. E will go to B and then

A. G will go to C and then A. Here, both the traversal meets at A

and hence the traversal ends.


The path of traversal is

E —-> B —-> A —-> C —-> G

Iterative Deepening Depth-First Search


It performs depth-first search to level 1, starts over, executes a complete depth-
first search to level 2, and continues in such way till the solution is found.

Uniform-cost Search Algorithm:


Sorting is done in increasing cost of the path to a node. It always expands the least
cost node. It is identical to Breadth First search if each transition has the same cost.
Uniform-cost search is a searching algorithm used for traversing a weighted tree or
graph. This algorithm comes into play when a different cost is available for each
edge. The primary goal of the uniform-cost search is to find a path to the goal
node which has the lowest cumulative cost.
Uniform-cost search expands nodes according to their path costs form the root
node. It can be used to solve any graph/tree where the optimal cost is in demand. A
uniform-cost search algorithm is implemented by the priority queue. It gives
maximum priority to the lowest cumulative cost. Uniform cost search is equivalent
to BFS algorithm if the path cost of all edges is the same.

Informed search algorithm


 Informed search algorithm contains an array of knowledge such as how far we are
from the goal, path cost, how to reach to goal node, etc.
 This knowledge help agents to explore less to the search space and find more
efficiently the goal node.
 The informed search algorithm is more useful for large search space. Informed
search algorithm uses the idea of heuristic, so it is also called Heuristic search.

informed search algorithms require details such as distance to


reach the goal, steps to reach the goal, cost of the paths which
makes this algorithm more efficient.

1. Greedy best-first search algorithm

Greedy best-first search uses the properties of both depth-first search and

breadth-first search. Greedy best-first search traverses the node by selecting the

path which appears best at the moment. The closest path is selected by using

the heuristic function.

Consider the below

graph with the heuristic values.

Here, A is the start node and H is the goal node.

Greedy best-first search first starts with A and then examines the next neighbour

B and C. Here, the heuristics of B is 12 and C is 4. The best path at the moment

is C and hence it goes to C. From C, it explores the neighbours F and G. the


heuristics of F is 8 and G is 2. Hence it goes to G. From G, it goes to H whose

heuristic is 0 which is also our goal state.

The path of traversal is

A —-> C —-> G —-> H


A* search algorithm

A* search algorithm is a combination of both uniform cost search and greedy

best-first search algorithms. It uses the advantages of both with better memory

usage. It uses a heuristic function to find the shortest path. A* search algorithm

uses the sum of both the cost and heuristic of the node to find the best path.

Consider the following graph with the heuristics values as follows.

Let A be the start node and H be the goal node.

First, the algorithm will start with A. From A, it can go to B, C, H.

Note the point that A* search uses the sum of path cost and heuristics value to

determine the path.

Here, from A to B, the sum of cost and heuristics is 1 + 3 = 4.


From A to C, it is 2 + 4 = 6.

From A to H, it is 7 + 0 = 7.

Here, the lowest cost is 4 and the path A to B is chosen. The other paths will be

on hold.

Now, from B, it can go to D or E.

From A to B to D, the cost is 1 + 4 + 2 = 7.

From A to B to E, it is 1 + 6 + 6 = 13.

The lowest cost is 7. Path A to B to D is chosen and compared with other paths

which are on hold.

Here, path A to C is of less cost. That is 6.

Hence, A to C is chosen and other paths are kept on hold.

From C, it can now go to F or G.

From A to C to F, the cost is 2 + 3 + 3 = 8.

From A to C to G, the cost is 2 + 2 + 1 = 5.

The lowest cost is 5 which is also lesser than other paths which are on hold.

Hence, path A to G is chosen.

From G, it can go to H whose cost is 2 + 2 + 2 + 0 = 6.

Here, 6 is lesser than other paths cost which is on hold.


Also, H is our goal state. The algorithm will terminate here.

The path of traversal is

A —-> C —-> G —-> H

Heuristic Functions :

A heuristic function in artificial intelligence, also known as a heuristic or simply a


heuristic, is an evaluation function used to estimate the cost or potential of
reaching a goal state from a given state in a problem-solving domain.

Heuristics are typically rules of thumb or approximate strategies that guide the
search for a solution. They provide a way to assess the desirability of different
options without exhaustively exploring every possibility.

Heuristics are used to make informed decisions in situations where it's


computationally expensive to search through all possible states or actions. They
help prioritize the exploration of more promising paths
Solving 8-Puzzle using A*
Algorithm.
Let’s start with what I mean by an “8-Puzzle” problem.

N-Puzzle or sliding puzzle is a popular puzzle that consists of N


tiles where N can be 8, 15, 24, and so on. In our example N = 8. The
puzzle is divided into sqrt(N+1) rows and sqrt(N+1) columns. Eg. 15-
Puzzle will have 4 rows and 4 columns and an 8-Puzzle will have 3
rows and 3 columns. The puzzle consists of N tiles and one empty
space where the tiles can be moved. Start and Goal configurations
(also called state) of the puzzle are provided. The puzzle can be
solved by moving the tiles one by one in the single empty space and
thus achieving the Goal configuration.

Fig 1. Start and Goal configurations of an 8-Puzzle.

The tiles in the initial(start) state can be moved in the empty


space in a particular order and thus achieve the goal state.
Rules for solving the puzzle.

Instead of moving the tiles in the empty space, we can visualize


moving the empty space in place of the tile, basically swapping the
tile with the empty space. The empty space can only move in four
directions viz.,

1. Up
2.Down
3. Right or
4. Left

The empty space cannot move diagonally and can take only one
step at a time (i.e. move the empty space one position at a time).

Basically, there are two types of searching techniques :


1. Uninformed Search and
2. Informed Search

How A* solves the 8-Puzzle problem.

We first move the empty space in all the possible directions in the
start state and calculate the f-score for each state. This is called
expanding the current state.
After expanding the current state, it is pushed into the closed list
and the newly generated states are pushed into the open list. A state
with the least f-score is selected and expanded again. This process
continues until the goal state occurs as the current state.
Basically, here we are providing the algorithm a measure to choose
its actions. The algorithm chooses the best possible action and
proceeds in that path.
This solves the issue of generating redundant child states, as the
algorithm will expand the node with the least f-score.

You might also like