Artificial Intelligence
DSE 3252
Problem Solving
ROHINI R RAO
DEPT OF DATA SCIENCE & COMPUTER APPLICATIONS
JANUARY 2023
Problem Solving Agents
Are goal based agents that use atomic representations
Goal formulation
◦ First step in problem solving
◦ based on the current situation and the agent’s performance measure
◦ Environment is represented by states
◦ Goal state in one in which the goal is satisfied
The agent’s task is to find out how to act, now and in the future, so that it reaches a goal 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 future actions that eventually lead to states of known value
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 2
State Space
◦ forms a directed network or graph in which the nodes are states and the links
between nodes are actions.
◦ A path in the state space is a sequence of states connected by a sequence of
actions.
◦ 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
◦ an optimal solution has the lowest path cost among all solutions
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 3
Example 1 – Vacuum Agent
State Transition Diagram
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 4
Well-defined problems and solutions
A Problem can be defined formally:
1. The initial state that the agent starts in
2. A description of the possible actions
3. Transition model available to the agent
◦ A description of what each action does;
4. The goal test, which determines whether a given state is a goal state
5. A path cost function that assigns a numeric cost to each path.
◦ Successor to refer to any state reachable from a given state by a single action
◦ State space of the problem are defined by the initial state, actions, and transition
model
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 5
Problem Formulation
Example 1 - Vacuum Agent
◦ States: The state is determined by both the agent location and the dirt locations
◦ There are 2 × 22 = 8 possible world states. A larger environment with n locations has n ・ 2n
states.
◦ Initial state: Any state can be designated as the initial state.
◦ Actions: each state has just three actions: Left, Right, and Suck
◦ • Transition model: The actions have their expected effects, except that moving Left
in the leftmost square, moving Right in the rightmost square, and Sucking in a clean
square have no effect.
◦ • Goal test: This checks whether all the squares are clean.
◦ • Path cost: Each step costs 1, so the path cost is the number of steps in the path.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 6
Problem Formulation
Example 2 – 8 puzzle problem
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 7
Problem Formulation
Example 2 – 8 puzzle problem
States: A state description specifies the location of each of the eight tiles and the blank in one of
the nine squares.
• Initial state: Any state can be designated as the initial state.
• Actions: The simplest formulation defines the actions as movements of the blank space-Left,
Right, Up, or Down.
• Transition model: Given a state and action, this returns the resulting state
• Goal test: This checks whether the state matches the goal configuration
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 8
“formulate, search, execute”
◦ The process of looking for a sequence of actions that reaches the goal is called Search
◦ 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 are carried out in the execution phase
◦ The possible action sequences starting at the initial state form a Search Tree:
◦ nodes correspond to states in the state space of the problem.
◦ with the initial state NODE at the root
◦ the branches are actions
◦ The set of all leaf nodes available for expansion at any given point is called the Frontier
( Open List)
◦ Loopy path
◦ result in repeated state
◦ are a special case of the more general concept of redundant paths
◦ To avoid exploring redundant paths remember where one has been.
◦ Tree-Search algorithm can be augmented with a data structure called the explored set
(Closed list), which remembers every expanded node
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 9
Partial Search Tree
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 10
Infrastructure for search algorithms
For each node n of the tree, structure should contains 4
components:
◦ 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 to the node, as indicated by the parent
pointers.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 11
“formulate, search, execute”
◦ The process of looking for a sequence of actions that reaches the goal is called Search
◦ 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 are carried out in the execution phase
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 12
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 13
Uninformed search strategies
Breadth First Search
Shallowest unexpanded node is chosen for expansion
◦ the root node is expanded first, then all the successors of the root node are expanded next, then
their successors, and so on.
◦ Using a FIFO queue for the frontier
◦ goal test is applied to each node when it is generated
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 14
Breadth First Search
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 15
Measuring problem-solving performance
Breadth First Search (BFS)
Completeness: Is the algorithm guaranteed to find a solution when there is one?
◦ Yes, if the shallowest goal node is at some finite depth d & branching factor b is finite.
Optimality: Does the strategy find the optimal solution?
◦ Yes, if the path cost is a nondecreasing function of the depth of the node.
Time complexity: How long does it take to find a solution?
◦ The root of the search tree generates b nodes at the first level,, for a total of b 2 at the second
level and so on. Suppose that the solution is at depth d.
◦ Then the total number of nodes generated is b + b 2 + b3.· · + bd = O(bd)
Space complexity: How much memory is needed to perform the search?
◦ There will be O(bd−1) nodes in the explored set and O(b d) nodes in the frontier
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 16
Time and memory requirements for BFS
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 17
Depth First Search
◦ Instance of graph search which uses LIFO queue
◦ 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 have no 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.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 18
Depth First Search
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 19
Measuring problem-solving performance
Depth First Search (BFS)
Completeness: Is the algorithm guaranteed to find a solution when there is one?
◦ The graph-search version, which avoids repeated states and redundant paths, is complete in
finite state spaces
◦ The tree-search version, on the other hand, is not complete
Optimality: Does the strategy find the optimal solution?
◦ both versions are nonoptimal
Time complexity: How long does it take to find a solution?
◦ all of the O(bm) nodes in the search tree, where m is the maximum depth of any node;
Space complexity: How much memory is needed to perform the search?
◦ requires storage of only O(bm) nodes
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 20
Adversarial Search
In multiagent environments, each agent needs to consider the actions of other agents and how they affect
its own welfare
The unpredictability of these other agents can introduce contingencies into the agent’s problem-solving
process
Competitive environments, in which the agents’ goals are in conflict, giving rise to adversarial search problems—often
known as games.
Mathematical game theory
◦ a branch of economics
◦ views any multiagent environment as a game, provided that the impact of each agent on the others is “significant,”
◦ regardless of whether the agents are cooperative or competitive
In AI, games are
◦ deterministic, turn-taking, two-player,
◦ zero-sum games of perfect information ( deterministic, fully observable environments)
◦ in which two agents act alternately and in which the utility values at the end of the game are always equal and opposite.
this opposition between the agents’ utility functions that makes the situation adversarial
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 21
Game defined as a kind of search
problem
S0: The initial state, which specifies how the game is set up at the start
PLAYER(s): Defines which player has the move in a state.
ACTIONS(s): Returns the set of legal moves in a state.
RESULT(s, a): The transition model, which defines the result of a move.
TERMINAL-TEST(s):
◦ A terminal test, which is true when the game is over and false otherwise. States where the game has ended are called
terminal states.
UTILITY(s, p):
◦ A utility function (also called an objective function or payoff function), defines the final numeric value for a game that
ends in terminal state s for a player p.
◦ In chess, the outcome is a win, loss, or draw, with values +1, 0, or 1/2
◦ A zero-sum game - where the total payoff to all players is the same for every instance of the game.
◦ Chess is zero-sum because every game has payoff of either 0 + 1, 1 + 0 or ½ + ½
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 22
Partial Game Tree for tic-tac-toe
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 23
Optimal Decisions in Games
MAX must find a contingent strategy
◦ which specifies MAX’s move in the initial state
◦ then MAX’s moves in the states resulting from
every possible response by MIN and so on
Assumption: optimal play for MAX assumes that
MIN also plays optimally
Optimum strategy is determined by the minimax
value of each node.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 24
Minimax Algorithm
computes the minimax decision from the current state.
It uses a simple recursive computation of the minimax values of each successor state
The recursion proceeds all the way down to the leaves of the tree, and then the minimax values are backed
up through the tree as the recursion unwinds.
performs a complete depth-first exploration of the game tree
If the maximum depth of the tree is m and there are b legal moves at each point, then
◦ time complexity of the minimax algorithm is O(bm)
◦ The space complexity is O(bm) for an algorithm that generates all actions at once, or O(m) for an algorithm
that generates actions one at a time
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 25
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 26
Psuedocode for Mini-Max Algorithm
function minimax(node, depth, maximizingPlayer) is
if depth ==0 or node is a terminal node then return static evaluation of no
de
if MaximizingPlayer then // for Maximizer Player
maxEval= -infinity
for each child of node do
eval= minimax(child, depth-1, false)
maxEval= max(maxEval,eval) //gives Maximum of the values
return maxEval
else // for Minimizer player
minEval= +infinity
for each child of node do
eval= minimax(child, depth-1, true)
minEval= min(minEval, eval) //gives minimum of the values
return minEval
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 27
•For node D max(-1,- -∞) => max(-1,4
•For Node E max(2, -∞) => max(2, 6)=
•For Node F max(-3, -∞) => max(-3,-5)
•For node G max(0, -∞) = max(0, 7) =
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 28
3 plies of Game tree with 3 players
Multiplayer games usually involve alliances, whether formal or informal, among the players.
Alliances are made and broken as the game proceeds.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 29
Measuring Problem Solving Performance
Mini-Max algorithm:
•Complete-
• Yes. It will definitely find a solution (if exist), in the finite search tree.
•Optimal-
• is optimal if both opponents are playing optimally.
•Time complexity-
• As it performs DFS for the game-tree, so the time complexity is O(bm),
where b is branching factor of the game-tree, and m is the maximum
depth of the tree.
•Space Complexity
• Space complexity of Mini-max algorithm is also similar to DFS which
is O(bm).
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 30
Stages in the calculation of the optimal
decision for the game tree
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 31
general case for
alpha–beta pruning
If Player has a better choice m either at the
parent node of n or at any choice point further
up,
then n will never be reached in actual play.
So once we have found out enough about n (by
examining some of its descendants) to reach this
conclusion, we can prune it.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 32
Alpha Beta Pruning
The problem with minimax search is that the number of game states it has to examine is
exponential in the depth of the tree
Alpha–beta pruning
◦ When applied to a standard minimax tree, it returns the same move as minimax would, but prunes away branchs that
cannot possibly influence the final decision
◦ Simplification of Minimax formula
◦ the value of the root and hence the minimax decision are independent of the values of the pruned leaves x and y.
◦ Alpha–beta pruning has two parameters that describe bounds on the backed-up values that appear anywhere
◦ along the path:
◦ α = the value of the best (i.e., highest-value) choice we have found so far at any choice point along the path for MAX.
◦ β = the value of the best (i.e., lowest-value) choice we have found so far at any choice point along the path for MIN.
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 33
function minimax(node, depth, alpha, beta, maximizing player)
if depth ==0 or node is a terminal node then return static evaluation of node
if MaximizingPlayer then // for Maximizer Player
maxEva= -infinity
for each child of node do
eva= minimax(child, depth-1, alpha, beta, False)
maxEva= max(maxEva, eva)
alpha= max(alpha, maxEva)
if beta<=alpha break
return maxEva
else // for Minimizer player
minEva= +infinity
for each child of node do
eva= minimax(child, depth-1, alpha, beta, true)
minEva= min(minEva, eva)
beta= min(beta, eva)
if beta<=alpha break
return minEva
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 34
Alpha Beta Pruning
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 35
Alpha Beta Search Algorithm
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 36
Improvements on Alpha-Beta Search
Killer Move Heuristics:
◦ The effectiveness of alpha–beta pruning is highly dependent on the order in which the states are examined
◦ If we can examine first the successors that are likely to be best, then alpha–beta needs to examine only O(b m/2)
nodes to pick the best move, instead of O(bm) for minimax
◦ Dynamic move-ordering – moves that were found to be best in the past.
◦ Best moves called killer-moves
Transposition table
◦ repeated states occur frequently because of transpositions—different permutations of the move sequence that
end up in the same position
◦ The hash table of previously seen positions is maintained in a transposition table
◦ Minimax & its related algorithms wont work for games like Chess or Go
◦ Claude Shannon’s paper Programming a Computer for Playing Chess (1950) proposed
◦ Type A strategy – (Wide but shallow) Considers all possible moves to a certain depth and then uses
heuristic evaluation function to estimate utility of states in that depth
◦ Type B strategy – (Deep but narrow) ignores moves that look bad and follows promising paths as far as
possible
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 37
Heuristic Alpha-Beta Tree Search
Cutoff test
• should return true if terminal states but otherwise can cutoff search based on depth or any other heuristic
Eval function
• returns an estimate of expected utility of state s to player p
• Should be strongly correlated to actual chances of winning
• Example: Weighted Linear function
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 38
References
1. Russell S., and Norvig P., Artificial Intelligence A Modern Approach (3e), Pearson 2010
02-03-2023 ARTIFICIAL INTELLIGENCE - ROHINI R RAO 39