AI M4 Notes
AI M4 Notes
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.
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
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
We can observe that A wins game for n coins only when B loses for coins n-1 or
n-x or n-y.