AI Game Playing and Search

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Artificial Intelligence

Game Playing and Search in AI

By :- Hithesh.G
(1AY19MT013)
INTRODUCTION

 Game playing was one of the first tasks undertaken in Artificial Intelligence. Game theory has its history
from 1950, almost from the days when computers became programmable. The very first game that is been
tackled in AI is chess. Initiators in the field of game theory in AI were Konard Zuse (the inventor of the first
programmable computer and the first programming language), Claude Shannon (the inventor of information
theory), Norbert Wiener (the creator of modern control theory), and Alan Turing. Since then, there has been a
steady progress in the standard of play, to the point that machines have defeated human champions (although
not every time) in chess and backgammon, and are competitive in many other games.
Types of Games

 Perfect Information Game: In which player knows all the possible


moves of himself and opponent and their results. E.g. Chess.
 Imperfect Information Game: In which player does not know all the
possible moves of the opponent. E.g. Bridge since all the cards are not
visible to player.
Definition

 Game playing is a search problem defined by following components


 Initial state: This defines initial configuration of the game and identifies first payer to move.
 Successor function: This identifies which are the possible states that can be achieved from the current state.
This function returns a list of (move, state) pairs, each indicating a legal move and the resulting state.
 Goal test: Which checks whether a given state is a goal state or not. States where the game ends are called as
terminal states.
 Path cost / utility / payoff function: Which gives a numeric value for the terminal states? In chess, the
outcome is win, loss or draw, with values +1, -1, or 0. Some games have wider range of possible outcomes.
Characteristics of game playing

 Unpredictable Opponent: Generally we cannot predict the behavior of the


opponent. Thus we need to find a solution which is a strategy specifying a move
for every possible opponent move or every possible state.
 Time Constraints: Every game has a time constraints. Thus it may be infeasible to
find the best move in this time.
How to Play Game in AI

 Typical structure of the game in the AI is:


 2- person game
 Players alternate moves
 Zero-sum game: one player’s loss is the other’s gain
 Perfect information: both players have access to complete information about the state of the game. No
information is hidden from either player.
 No chance (e. g. using dice) involved.
 E.g. Tic- Tac- Toe, Checkers, Chess, Go, Nim, Othello
Minimax

Game Tree of Tic-Tac-Toe


Minimax

 Games are represented in the form of trees wherein nodes represent all the possible states of a game and
edges represent moves between them. Initial state of the game is represented by root and terminal states by
leaves of the tree. In a normal search problem, the optimal solution would be a sequence of moves leading to
a goal state that is a win. Even a simple game like tic-tac-toe is too complex for us to draw the entire game
tree.
Minimax Algorithm

 Now let us put all this in the form of algorithm. Following is minimax algorithm, which takes current state as
an input and returns a best possible operator to be applied to current state. Essentially this is same as what
we have seen previously in recursive function. But this algorithm is written from the MAX player point of
view.
Minimax Algorithms

Function MINIMAX-DECISION (state) returns an operator


For each op in OPERATORS.[game] do
VALUE [op] = MINIMAX-VALUE (APPLY (op, state), game)
End
Return the op with the highest VALUE [op]
Function MINIMAX-VALUE (state, game) returns a utility value
If TERMINAL-TEST (state) then
Return UTILITY (state)
Else If MAX is to move in state then
Return the highest MINIMAX-VALUE of SUCCESSORS (state)
Else
Return the lowest MINIMAX-VALUE of SUCCESSORS (state)
Applications

 Game theory has vast applications in different fields. Some of the important are mentioned below.
 Entertainment: Game theory is used to define different strategies of different games.
 Economics: Each factor in the market, such as seasonal preferences, buyer choice, changes in supply and
material costs, and other such market factors can be used to describe strategies to maximize the outcome and
thus the profit.
 Military: Game theory can be useful in Military also. Military strategists have turned to game theory to play
"war games". Usually, such games are not zero-sum games, for loses to one side are not won by the other.
 Political science: The properties of n-person non-zero-sum games can be used to study different aspects of
political science and social science. Matters such as distribution of power, interactions between nations, the
distribution of classes and their effects of government, and many other matters can be easily investigated by
breaking the problem down into smaller games, each of whose outcomes affect the final result of a larger
game.
WHAT IS SEARCH ENGINE?

 A search engine is a software program that helps people find the information they are looking for online
using keywords or phrases.
 Search engines are able to return results quickly—even with millions of websites online—by scanning the
Internet continuously and indexing every page they find.
 When a user enters a search term, the search engine looks at the website page titles, contents and keywords it
has indexed and uses algorithms (step-by-step operations) to produce a list of sites—with the most relevant
websites at the top of the list.
 Companies use search engine optimization (SEO) to help search engines recognize their websites as highly
relevant to particular searches. Popular search engines include Google, Bing and Yahoo.
Search Algorithms in AI

Search algorithms are one of the most important areas of Artificial Intelligence. This
topic will explain all about the search algorithms in AI.
 Problem Solving Agents :-
In Artificial Intelligence, Search techniques are universal problem-solving
methods. Rational agents or Problem-solving agents in AI mostly used these
search strategies or algorithms to solve a specific problem and provide the best
result. Problem-solving agents are the goal-based agents and use atomic
representation. In this topic, we will learn various problem-solving search
algorithms.
Search Algorithms Terminology

• Search: Searching is a step by step procedure to solve a search-problem in a given search space. A search
problem can have three main factors:
• Search Space: Search space represents a set of possible solutions, which a system may have.
• Start State: It is a state from where agent begins the search.
• Goal test: It is a function which observe the current state and returns whether the goal state is achieved or not.
• Search tree: A tree representation of search problem is called Search tree. The root of the search tree is the
root node which is corresponding to the initial state.
• Actions: It gives the description of all the available actions to the agent.
• Transition model: A description of what each action do, can be represented as a transition model.
• Path Cost: It is a function which assigns a numeric cost to each path.
• Solution: It is an action sequence which leads from the start node to the goal node.
• Optimal Solution: If a solution has the lowest cost among all solutions.
Properties of Search 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.
Types of search algorithms
Uniformed/Blind Search

 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.
 It can be divided into five main types:
• Breadth-first search
• Uniform cost search
• Depth-first search
• Iterative deepening depth-first search
• Bidirectional Search
Informed Search

 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.
 An example of informed search algorithms is a traveling salesman problem.
1. Greedy Search
2. A* Search
Uniformed Search Algorithms

 Uninformed search is a class of general-purpose search algorithms which operates in brute force-way.
Uninformed search algorithms do not have additional information about state or search space other than
how to traverse the tree, so it is also called blind search.
 Following are the various types of uninformed search algorithms:
1. Breadth-first Search
2. Depth-first Search
3. Depth-limited Search
4. Iterative deepening depth-first search
5. Uniform cost search
6. Bidirectional Search
Breadth First Search

• Breadth-first search is the most common search strategy for traversing a tree or graph. This algorithm searches
breadthwise 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.
 Advantages:
• BFS will provide a solution if any solution exists.
• If there are more than one solutions for a given problem, then BFS will provide the minimal solution which
requires the least number of steps.
 Disadvantages:
• It requires lots of memory since each level of the tree must be saved into memory to expand the next level.
• BFS needs lots of time if the solution is far away from the root node.
Depth First Search

• Depth-first search isa 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.
• The process of the DFS algorithm is similar to the BFS algorithm.
 Advantage:
• DFS requires very less memory as it only needs to store a stack of the nodes on the path from root node to
the current node.
• It takes less time to reach to the goal node than BFS algorithm (if it traverses in the right path).
 Disadvantage:
• There is the possibility that many states keep re-occurring, and there is no guarantee of finding the solution.
• DFS algorithm goes for deep down searching and sometime it may go to the infinite loop.
Depth Limited Search Algorithm

 A depth-limited search algorithm is similar to depth-first search with a predetermined limit. Depth-limited
search can solve the drawback of the infinite path in the Depth-first search. In this algorithm, the node at the
depth limit will treat as it has no successor nodes further.
 Depth-limited search can be terminated with two Conditions of failure:
• Standard failure value: It indicates that problem does not have any solution.
• Cutoff failure value: It defines no solution for the problem within a given depth limit.
 Advantages:
 Depth-limited search is Memory efficient.
 Disadvantages:
• Depth-limited search also has a disadvantage of incompleteness.
• It may not be optimal if the problem has more than one solution.
Uniform Cost Search Algorithm

 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.
 Advantages:
• Uniform cost search is optimal because at every state the path with the least cost is chosen.
 Disadvantages:
• It does not care about the number of steps involve in searching and only concerned about path
cost. Due to which this algorithm may be stuck in an infinite loop.
Iterative Deeping Depth

 The iterative deepening algorithm is a combination of DFS and BFS algorithms. This search algorithm finds
out the best depth limit and does it by gradually increasing the limit until a goal is found.
 This algorithm performs depth-first search up to a certain "depth limit", and it keeps increasing the depth
limit after each iteration until the goal node is found.
 This Search algorithm combines the benefits of Breadth-first search's fast search and depth-first search's
memory efficiency.
 The iterative search algorithm is useful uninformed search when search space is large, and depth of goal
node is unknown.
 Advantages:
• Itcombines the benefits of BFS and DFS search algorithm in terms of fast search and memory efficiency.
 Disadvantages:
• The main drawback of IDDFS is that it repeats all the work of the previous phase.
Bidirectional Search Algorithm

 Bidirectional search algorithm runs two simultaneous searches, one form initial state called as
forward-search and other from goal node called as backward-search, to find the goal node.
Bidirectional search replaces one single search graph with two small subgraphs in which one starts the
search from an initial vertex and other starts from goal vertex. The search stops when these two
graphs intersect each other.
 Bidirectional search can use search techniques such as BFS, DFS, DLS, etc.
 Advantages:
• Bidirectional search is fast.
• Bidirectional search requires less memory
 Disadvantages:
• Implementation of the bidirectional search tree is difficult.
• In bidirectional search, one should know the goal state in advance.
Informed Search Algorithm

 So far we have talked about the uninformed search algorithms which looked through search space
for all possible solutions of the problem without having any additional knowledge about search
space. But 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.
 Heuristics function: Heuristic is a function which is used in Informed Search, and it finds the
most promising path. It takes the current state of the agent as its input and produces the estimation
of how close agent is from the goal. The heuristic method, however, might not always give the
best solution, but it guaranteed to find a good solution in reasonable time. Heuristic function
estimates how close a state is to the goal. It is represented by h(n), and it calculates the cost of an
optimal path between the pair of states. The value of the heuristic function is always positive.
Pure Heuristic Search

 Pure heuristic search is the simplest form of heuristic search algorithms. It expands nodes based on their
heuristic value h(n). It maintains two lists, OPEN and CLOSED list. In the CLOSED list, it places those
nodes which have already expanded and in the OPEN list, it places nodes which have yet not been expanded.
 On each iteration, each node n with the lowest heuristic value is expanded and generates all its successors
and n is placed to the closed list. The algorithm continues unit a goal state is found.
 In the informed search we will discuss two main algorithms which are given below:
• Best First Search Algorithm(Greedy search)
• A* Search Algorithm
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. f(n)= g(n). Were, h(n)= estimated cost from
node n to the goal.
 The greedy best first algorithm is implemented by the priority queue.
Greedy Search

 Best first search algorithm:


• Step 1: Place the starting node into the OPEN list.
• Step 2: If the OPEN list is empty, Stop and return failure.
• Step 3: Remove the node n, from the OPEN list which has the lowest value of h(n), and places it in the
CLOSED list.
• Step 4: Expand the node n, and generate the successors of node n.
• Step 5: Check each successor of node n, and find whether any node is a goal node or not. If any successor
node is goal node, then return success and terminate the search, else proceed to Step 6.
• Step 6: For each successor node, algorithm checks for evaluation function f(n), and then check if the node
has been in either OPEN or CLOSED list. If the node has not been in both list, then add it to the OPEN list.
• Step 7: Return to Step 2.
Greedy Search

 Advantages:
• Best first search can switch between BFS and DFS by gaining the advantages of both the algorithms.
• This algorithm is more efficient than BFS and DFS algorithms.
 Disadvantages:
• It can behave as an unguided depth-first search in the worst case scenario.
• It can get stuck in a loop as DFS.
• This algorithm is not optimal.
A* Search Algorithm

 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).
 In A* search algorithm, we use search heuristic as well as the cost to reach the node. Hence we can combine
both costs as following, and this sum is called as a fitness number.
Algorithm of A* search:

Step1: Place the starting node in the OPEN list.


Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and
stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation
function (g+h), if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list. For
each successor n', check whether n' is already in the OPEN or CLOSED list, if not then
compute evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the
back pointer which reflects the lowest g(n') value.
Step 6: Return to Step 2
A* Search

 Advantages:
• A* search algorithm is the best algorithm than other search algorithms.
• A* search algorithm is optimal and complete.
• This algorithm can solve very complex problems.
 Disadvantages:
• It does not always produce the shortest path as it mostly based on heuristics and approximation.
• A* search algorithm has some complexity issues.
• The main drawback of A* is memory requirement as it keeps all generated nodes in the memory, so it is not
practical for various large-scale problems.
EXAMPLE- GOOGLE SEARCH ENGINE

AI’s used in Google search engines are:-


 Rank Brain
 Neural Matching
 BERT (Bidirectional Encoder Representations from Transformers)
 MUM (Multitask Unified Model)
Rank Brain

 It starts with RankBrain, Google’s first attempt at using AI in search dates back to 2015. Google told
us RankBrain helps Google understand how words are related to concepts and can take a broad
query and better define how that query relates to real-world concepts. While it launched in 2015 and
was used in 15% of queries, Google said it is now, in 2022, widely used in many queries and in all
languages and regions. RankBrain does specifically help Google rank search results and is part of the
ranking algorithm.
• Looks at the query and content language
• Works for all languages
• Very commonly used for many queries
 Example:-Here is an example provided by Google of how RankBrain is used, if you search for “what’s
the title of the consumer at the highest level of a food chain,” Google’s systems learn from seeing
those words on various pages that the concept of a food chain may have to do with animals, and not
human consumers. By understanding and matching these words to their related concepts, RankBrain
helps Google understand that you’re looking for what’s commonly referred to as an “apex predator.”
Neural Matching

 Neural matching was the next AI Google released for search, it was released in 2018 and then
expanded to the local search results in 2019. In fact, we have an article explaining the 
differences between RankBrain and neural matching over here. Google told us neural matching
helps Google understand how queries relate to pages by looking at the entire query or content on
the page and understanding it within the context of that page or query. Today, neural matching is
used in many, if not most, queries, for all languages, in all regions, across most verticals of
search. Neural matching does specifically help Google rank search results and is part of the
ranking algorithm.
 Example:- Here is an example provided by Google of how neural matching is used, if you search for
“insights how to manage a green,” for example. Google said “if a friend asked you this, you’d
probably be stumped.” “But with neural matching, we’re able to make sense of this quizzical search.
By looking at the broader representations of concepts in the query — management, leadership,
personality and more — neural matching can decipher that this searcher is looking for
management tips based on a popular, color-based personality guide,” Google told us.
BERT

 BERT, Bidirectional Encoder Representations from Transformers, came in 2019, it is a neural


network-based technique for natural language processing pre-training. Google told us BERT helps
Google understand how combinations of words express different meanings and intents, including
looking at the sequence of words on a page, so even seemingly unimportant words in your queries
are counted for. When BERT launched, it was used in 10% of all English queries but expanded to 
more languages and used in almost all English queries early on. Today it is used in most queries
and is supported in all languages. BERT does specifically help Google rank search results and is
part of the ranking algorithm.
 Example:- Here is an example provided by Google of how BERT is used, if you search for “if you
search for “can you get medicine for someone pharmacy,” BERT helps us understand that you’re
trying to figure out if you can pick up medicine for someone else. Before BERT, we took that short
preposition for granted, mostly surfacing results about how to fill a prescription,” Google told us.
MUM

  MUM, Multitask Unified Model, is Google’s most recent AI in search. MUM was introduced in 2021
 and then expanded again at the end of 2021 for more applications, with a lot of promising uses for
it in the future. Google told us that MUM helps Google not just with understanding languages but
also generating languages, so it can be used to understand variations in new terms and languages.
MUM is not used for any ranking purposes right now in Google Search but does support all
languages and regions.
 Currently, MUM is used to improve searches for COVID-19 vaccine information, and Google said it
is “looking forward to offering more intuitive ways to search using a combination of 
both text and images in Google Lens in the coming months.”
THANK YOU

You might also like