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

AI M4 Notes

..

Uploaded by

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

AI M4 Notes

..

Uploaded by

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

Introduction to Problem-Solving using Search Algorithms

Problem-solving refers to artificial intelligence techniques, including various


techniques such as forming efficient algorithms, heuristics, and performing root cause
analysis to find desirable solutions.
In artificial intelligence, problems can be solved by using searching algorithms,
evolutionary computations, knowledge representations, etc.

Search Algorithms
 A search problem consists of:
 A State Space. Set of all possible states where you can be.
 A Start State. The state from where the search begins.
 A Goal State. It is a function which observe the current state and returns
whether the goal state is achieved or not..
 The Solution to a search problem is a sequence of actions, called the plan that
transforms the start state to the goal state.
 This plan is achieved through search algorithms.

Properties of Search Algorithms


Following are the four essential properties of search algorithms to compare the
efficiency of these algorithms:
 Completeness: A search algorithm is said to be complete if it guarantees to
return a solution if at least any solution exists for any random input.
 Optimality: If a solution found for an algorithm is guaranteed to be the best
solution (lowest path cost) among all other solutions, then such a solution for
is said to be an optimal solution.
 Time Complexity: Time complexity is a measure of time for an algorithm to
complete its task.
 Space Complexity: It is the maximum storage space required at any point
during the search, as the complexity of the problem.
1. Uninformed Search Algorithm
 The uninformed search does not contain any domain knowledge such as closeness,
the location of the goal.
 It operates in a brute-force way as it only includes information about how to
traverse the tree and how to identify leaf and goal nodes.
 Uninformed search applies a way in which search tree is searched without any
information about the search space like initial state operators and test for the goal,
so it is also called blind search.
 It examines each node of the tree until it achieves the goal node..
 Uninformed search algorithms are:
 Depth First Search
 Breadth First Search
 Uniform Cost Search
 Each of these algorithms will have:
 A problem graph, containing the start node S and the goal node G.
 A strategy, describing the manner in which the graph will be traversed to get
to G.
 A fringe, which is a data structure used to store all the possible states (nodes)
that you can go from the current states.
 A tree, that results while traversing to the goal node.
 A solution plan, which the sequence of nodes from S to G.
Depth First Search
 Depth-first search is a recursive algorithm for traversing a tree or graph data
structure.
 It is called the depth-first search because it starts from the root node and follows
each path to its greatest depth node before moving to the next path.
 DFS uses a stack data structure for its implementation.
Breadth First Search
 Breadth-first search is the most common search strategy for traversing a tree or
graph.
 This algorithm searches breadth wise in a tree or graph, so it is called breadth-
first search.
 BFS algorithm starts searching from the root node of the tree and expands all
successor node at the current level before moving to nodes of next level.
 The breadth-first search algorithm is an example of a general-graph search
algorithm.
 Breadth-first search implemented using FIFO queue data structure
Uniform Cost Search
 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.
2. Informed Search Algorithm
 Informed search algorithms use domain knowledge.
 In an informed search, problem information is available which can guide the
search.
 Informed search strategies can find a solution more efficiently than an
uninformed search strategy.
 Informed search is also called a Heuristic search.
 A heuristic is a way which might not always be guaranteed for best solutions but
guaranteed to find a good solution in reasonable time.
 Informed search can solve much complex problem which could not be solved in
another way.
 Informed Search algorithms are:
 Greedy Search
 A* Search

 Search Heuristics: In an informed search, a heuristic is a function that estimates


how close a state is to the goal state.
 For example – Manhattan distance, Euclidean distance, etc. (Lesser the distance,
closer the goal.)
 Different heuristics are used in different informed algorithms.

Greedy Search
 Greedy best-first search algorithm always selects the path which appears best at
that moment.
 It is the combination of depth-first search and breadth-first search algorithms.
 It uses the heuristic function and search.
 Best-first search allows us to take the advantages of both algorithms.
 With the help of best-first search, at each step, we can choose the most promising
node.
 In the best first search algorithm, we expand the node which is closest to the goal
node and the closest cost is estimated by heuristic function, i.e.
 h(n)=g(n)
 h(n)= estimated cost from node n to the goal.
A* Search
 A* search is the most commonly known form of best-first search.
 It uses heuristic function h(n), and cost to reach the node n from the start state
g(n).
 It has combined features of UCS and greedy best-first search, by which it solve
the problem efficiently.
 A* search algorithm finds the shortest path through the search space using the
heuristic function.
 This search algorithm expands less search tree and provides optimal result faster.
 A* algorithm is similar to UCS except that it uses g(n)+h(n) instead of g(n).

Adversarial Search
 Adversarial search is a search, where we examine the problem which arises when
we try to plan ahead of the world and other agents are planning against us.
 The environment with more than one agent is termed as multi-agent environment,
in which each agent is an opponent of other agent and playing against each other.
Each agent needs to consider the action of other agent and effect of that action on
their performance.
 So, Searches in which two or more players with conflicting goals are trying to
explore the same search space for the solution, are called adversarial searches,
often known as Games.
Critical Terms
 Initial state: It specifies how the game is set up at the start.
 Player(s): It specifies which player has moved in the state space.
 Action(s): It returns the set of legal moves in state space.
 Result(s, a): It is the transition model, which specifies the result of moves in the
state space.
 Terminal-Test(s): Terminal test is true if the game is over, else it is false at any
case. The state where the game ends is called terminal states.
 Utility(s, p): A utility function gives the final numeric value for a game that ends
in terminal states s for player p. It is also called payoff function. For Chess, the
outcomes are a win, loss, or draw and its payoff values are +1, 0, ½. And for tic-
tac-toe, utility values are +1, -1, and 0.
Mini-Max Algorithm in Artificial Intelligence
 Mini-max algorithm is a recursive or backtracking algorithm which is used in
decision-making and game theory. It provides an optimal move for the player
assuming that opponent is also playing optimally.
 Mini-Max algorithm uses recursion to search through the game-tree.
 Min-Max algorithm is mostly used for game playing in AI. Such as Chess,
Checkers, tic-tac-toe, go, and various tow-players game. This Algorithm
computes the minimax decision for the current state.
 In this algorithm two players play the game, one is called MAX and other is
called MIN.
 Both the players fight it as the opponent player gets the minimum benefit while
they get the maximum benefit.
 Both Players of the game are opponent of each other, where MAX will select the
maximized value and MIN will select the minimized value.
 The minimax algorithm performs a depth-first search algorithm for the
exploration of the complete game tree.
 The minimax algorithm proceeds all the way down to the terminal node of the
tree, then backtrack the tree as the recursion.
Working Of Mini-Max Algorithm
 The working of the minimax algorithm can be easily described using an example.
Below we have taken an example of game-tree which is representing the two-
player game.
 In this example, there are two players one is called Maximizer and other is called
Minimizer.
 Maximizer will try to get the Maximum possible score, and Minimizer will try to
get the minimum possible score.
 This algorithm applies DFS, so in this game-tree, we have to go all the way
through the leaves to reach the terminal nodes.
 At the terminal node, the terminal values are given so we will compare those
value and backtrack the tree until the initial state occurs.
if MaximizingPlayer then // for Maximizer Player
maxEva= -infinity
for each child of node do
eva= minimax(child, depth-1, false)
maxEva= max(maxEva,eva) //gives Maximum of the values
return maxEva

else // for Minimizer player


minEva= +infinity
for each child of node do
eva= minimax(child, depth-1, true)
minEva= min(minEva, eva) //gives minimum of the values
return minEva

Step 2:
Build a bot to play Last Coin Standing
 Coin game winner where every player has three choices
 A and B are playing a game.
 In the beginning, there are n coins.
 Given two more numbers x and y.
 In each move, a player can pick x or y or 1 coin.
 A always starts the game.
 The player who picks the last coin wins the game or the person who is not able to
pick any coin loses the game.
 For a given value of n, find whether A will win the game or not if both are
playing optimally.
 Examples:
 Input:n=5,x=3,y=4
 Output:A
 There are 5 coins,every player can pick 1 or 3 or 4 coins on his/her turn.
 A can win by picking 3 coins in first chance.
 Now 2 coins will be left so B will pick one coin and now A can win by
picking the last coin.
 Input:2,3,4
 Output:B

 Let us take few example values of n for x = 4, y = 5.


 n = 0 A can not pick any coin so he losses
 n = 1 A can pick 1 coin and win the game
 n = 2 A can pick only 1 coin. Now B will pick 1 coin and win the game
 n = 3 A can pick only 1 coin. Now B will have to choose from 2 coins so A will
win.
 n=4 A will win the game by picking 4 coins
 n = 5, A will choose 5 coins and win the game.
 n=6 A will pick 4 coins.Remaining coins are 2.Now B will have to choose from 2
coins so A will win.
 n=7 A will pick 5 coins.Remaining coins are 2.Now B will have to choose from 2
coins so A will win.
 n=6 A will pick 4 coins.Remaining coins are 2.
In the case n = 2 A was a loser
But for n-x coins,A is the loser. for n coins,A is the winner.

 n=7 A will pick 5 coins.Remaining coins are 2.


In the case n = 2 A was a loser
But for n-y coins,A is the loser. for n coins,A is the winner.
 For n-1 coins
Consider n=3,Remaining coins are 2.
For the case of n=2, A is the loser
But for n coins A is the winner.

 A will win for n coins when


A loses for n-1 coins
A loses for n-x coins
A loses for n-y coins

 We can observe that A wins game for n coins only when B loses for coins n-1 or
n-x or n-y.

Steps to design a Last Coin Standing Game Using AI


Step1: Import the required packages
Step2: Inherit the class from the TwoPlayerGame class to handle all operations of the
game
Step3: Define the following functions
 define the players and the player who is going to start the game
 define the number of coins in the game
 Define the maximum number of coins a player can take in a move
 Define possible moves
 Define the removal of the coins
 Define who took the last coin.
 Define when to stop the game, that is when somebody wins
 Define how to compute the score.
 Define the number of coins remaining in the pile.
Step4: Display the no.of coins in the game(function 2)
Step5: Start the game by choosing the no.of coins to remove (function 3&4)
Step6: Remove the coins from the existing pile of coins(function 5)
Step7: Wait for the AI to make its move , show the remaining coins in the pile
(function9)
Step8: Repeat the steps 5,6,7 until there is no coins left in the pile
Step9: Show who took the last coin (function6,7)
Step10: Display the winner

Steps to build a Tic Tac Toe game


Step1: Define the following functions
 Define an empty Tic-tac-toe board (3X3)
 Define a Function to display the board
 Define a Function to check for a win
 Check rows
 check columns
 check diagonals
 Define a Function to check if the board is full (a tie)
 Define a Function for the AI to make a move
 Define Minimax algorithm
Step2: Display the tic-tac-toe board (function 1&2)
Step3: Start the game by making a move.wait for the AI to make its move(function 5)
Step4: Check for a win (function 3),tie(function 4)
Step5: use minimax algorithm (function 6) & Display the winner (AI or Human)

You might also like