Chapter 3
Chapter 3
CHAPTER 3:
Solving Problems by Searching and
Constraint Satisfaction Problem
Compiled by Aliazar D.
BSc & MSc in Software Engineering
[email protected]
What is problem in context
of AI?
3
Problem
• It is a collection of information that the agent will use to
decide what to do.
• Problems have the general form:
Given such-and-such information, find x
• A huge variety of problems are addressed in AI, both:
"Given a list of cities and the distances
Toy problems between each pair of cities, what is the
shortest possible route that visits each city
• e.g. 8-puzzle, vacuum cleaner world,… exactly once and returns to the origin city?"
Real-life problems
• E.g. Route finding, Traveling sales person, etc.
4
Route finding is the ability to perceive spatial relations between objects and navigate between said objects literally or within a written maplike scenario.
Example
The agent is expected to clean all the dirt in the given locations
5
Solving a problem
• Formalize the problem: Identify the collection of information that the
agent will use to decide what to do.
• Define states.
States describe distinguishable stages during the problem-solving process.
Example:- What are the various states in route finding problem?
The various cities including the location of the agent.
• Define the available operators/rules for getting from one state to the
next.
Operators cause an action that brings transitions from one state to
another by applying on a current state.
• Suggest a suitable representation for the problem space/state space.
Graph, table, list, set, … or a combination of them.
6
State space of the problem
• The state space defines the set of all relevant states reachable from
the initial state by (any) sequence of actions through iterative
application of all permutations and combinations of operators,
• State space of the problem includes the various states
Initial state: defines where the agent starts in or begins with
Goal state: describes the situation we want to achieve
Transition states: other states in between initial and goal states
• AKA search space/problem space and can be represented in a
tree/graph.
Example – Find the state space for route finding problem
• Think of the states reachable from the initial state.
7
State Space for Route Finding
Romania Ethiopia Aksum
100
200 Mekele
Gondar 180 80
Lalibela
110 250
150
Bahr dar
Dessie
170
8
Awasa
Steps in problem solving
• Goal formulation
is a step that specifies exactly what the agent is trying to achieve?
this step narrows down the state-space that the agent has to look at.
• Problem formulation
is a step that puts down the actions and states that the agent has to consider given a goal (avoiding
any redundant states), like:
The initial state
The allowable actions etc…
• Search
is the process of looking for the various sequence of actions that lead to a goal state, evaluating
them and choosing the optimal sequence.
• Execute
is the final step that the agent executes the chosen sequence of actions to get it to the solution/goal
9
Example: Route Finding*
Scenario: On holiday in Romania: currently in Arad.
Flight leaves tomorrow from
Bucharest.
Since then no seats available for the next six
weeks. The ticket is non-refundable, visa is about to expire, …
• Formalize problem:
Define the possible states involved to solve
the problem
• Formulate goal:
Be in Bucharest
• Formulate problem:
Initial state: in Arad
States: various cities
Operators/actions: driving between cities
• Find solution:
Optimal sequence of cities, e.g., Arad, Sibiu,
10
Fagaras, Bucharest
Aksum
100
Mekele
Activity!
200 Scenario: Behailu was in Aksum
Gondar 80 city and he wanted to go to
180
Lalibela Awasa for holiday.
110 250 Find a short route to drive to
150 Awasa.
Bahr dar
Dessie
170
400
330
Jima Formalize problem:
Addis Ababa
100
430 Adama 370
Formulate goal:
Formulate problem:
Gambela 230 320 Nekemt Initial state:
States:
Operators/actions:
Awasa Find solution: 11
The 8 puzzle problem*
14
Vacuum Cleaner * State Space
15
Knowledge and types of problems
• Example:
Route finding problem:
Reach Bucharest airport on time (IsCity(x, Bucharest))
Coloring problem:
All circle green(IsGreen(x, y, z) 24
Path cost function
• A function that assigns a cost to a path (sequence of
actions).
Is often denoted by g. Usually, it is the sum of the costs of the
individual actions along the path (from one state to another
state)
A sequence of actions in the state space. For example, we
may prefer paths with fewer or less costly actions
• Example:
Route finding problem:
• Path cost from initial to goal state
Coloring problem: 25
Example problems
We can group well defined problems into two:
• Toy Problems
Are problems that are useful to test and demonstrate methodologies
Concise and exact description (abstract version) of real problem
Can be used by researchers to compare researchers the performance
of different algorithms
Example:
The vacuum Cleaner world, The 8-puzzle, The 8-queens problem
• Real-World Problem
More difficult and complex to solve, and there is no single agreed-
upon description
Have much greater commercial/economic impact if solved
Example: 26
The vacuum world
• Problem formulation
State : one or both of the eight states shown in the Figure
Operators: move left, move right, suck
Goal test: no dirt left in any square
Path cost: each action costs 1
27
The 8-puzzle
• Problem formulation
State: the location of each of the eight tiles in one of the nine squares
Operators: blank space moves left, right, up, or down
Goal test: state matches the right figure (goal state)
Path cost: each step costs 1, since we are moving one step each time.
5 4 1 2 3
6 1 8 8 4
7 3 2 7 6 5
28
Activity!
29
Jealous Husbands Problem
The are three married couples, must cross a river using a
boat which can carry at most two people, with the
constraint that no woman can be in the presence of
another man unless her husband is also present.
Under this constraint, there cannot be both women and
men present on a bank with
women outnumbering men, since
if there were, these women would
be without their husbands.
The boat cannot cross the river by
itself with no people on board
30
Missionary-and-cannibal problem:
Three missionaries and three cannibals are on one side of a river that
they wish to cross. There is a boat that can hold one or two people.
Find an action sequence that brings everyone safely to the opposite
bank (i.e. Cross the river).
But you must never leave a group of missionaries outnumbered by
cannibals on the same bank (in any place).
The boat cannot cross the river by itself with no people on board.
And, in some variations, one of the cannibals has only one arm and
cannot row.
1) Identify the set of states and operators
2) Show using suitable representation the state space
of the problem
31
Monkey and Banana Problem
Problem Statement
Suppose the problem is as given below :−
• A hungry monkey is in a room, and
he is near the door.
• The monkey is on the floor.
• Bananas have been hung from the
center of the ceiling of the room.
• There is a block (or chair) present in
the room near the window.
• The monkey wants the banana, but
cannot reach it. 32
So how can the monkey get the bananas?
33
Monkey and Banana Problem Solutions
• if the monkey is clever enough, he can come to the block,
drag the block to the center, climb on it, and get the
banana. Below are few observations in this case −
• Monkey can reach the block, if both of them are at the
same level. From the image, we can see that both the
monkey and the block are on the floor.
• If the block position is not at the center, then monkey can
drag it to the center.
• If monkey and the block both are on the floor, and block is
at the center, then the monkey can climb up on the block.
So the vertical position of the monkey will be changed.
• When the monkey is on the block, and block is at the
34
center, then the monkey can get the bananas.
More AI Problems and Solutions….
Uninformed Search
Examples of Search Problems
The searching process is like building the search tree that is super imposed over the state space
A search tree is a representation in which nodes denote paths and branches connect paths. The node with no
parent is the root node. The nodes with no children are called leaf nodes.
Example: Route finding Problem
Partial search tree for route finding from Sidist Kilo to Stadium.
(a) The initial state SidistKilo goal test
SidistKilo
(b) After expanding Sidist Kilo generating a new state
AratKilo Giorgis ShiroMeda
choosing one SidistKilo
option
(c) After expanding Arat Kilo AratKilo Giorgis ShiroMeda
41
Infrastructure for search algorithms
• Search algorithms require a data structure to keep track of the
search tree that is being constructed.
For each node n of the tree, we have a structure that
contains four 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.
42
Conti..
• The frontier needs to be stored in such a way that the search algorithm can easily choose the next
node to expand according to its preferred strategy.
• The operations on a queue are as follows:
EMPTY?( queue) returns true only if there are no more elements in the queue.
Pop(queue) removes the first element of the queue and returns it.
INSERT(element, queue) inserts an element and returns the resulting queue.
A queue may be
• LIFO (stack): which pops the newest element of the queue
• FIFO: which pops the oldest element of the queue
• PRIORITY: which pops the element of the queue with the highest priority according to some
ordering function.
43
Algorithm Evaluation: Completeness and
Optimality
7 2 4 7 2 4
8 6 5 8 6
5 3 1 3 1
2 4 7 2 4 7 2 4 7 2 4 7 2 4 7 2 4
7 8 6 8 6 5 8 6 5 6 5 8 6 5 8 6
5 3 1 5 3 1 3 1 3 8 1 3 1 3 1
47
Searching Strategies: classification
• Search strategy gives the order in which the search space is examined
Uses a trial-and-error approach to
•Uninformed (= blind) search systematically guess the solution,
AKA weak search methods, most general methods are brute-force because they do
not need domain knowledge that guide them to the right direction towards the goal
Have no information about the number of steps or the path cost from the current
state to the goal
It is important for problems for which there is no additional information to consider
•Informed (= heuristic) search
Have problem-specific knowledge (knowledge that is true from experience)
Have knowledge about how far are the various state from the goal
Can find solutions more efficiently than uninformed search 48
Search Methods:
• Uninformed search
Breadth first
Depth first
Uniform cost, …
Depth limited search
Iterative deepening
etc.
• Informed search
Greedy search
A*-search
Iterative improvement,
Constraint satisfaction
etc. 49
Breadth first search
• The Breadth-First Search (BFS) is used to search a tree or graph data
structure for a node that meets a set of criteria.
• It starts at the tree’s root or graph and searches/visits all nodes at the current
depth level before moving on to the nodes at the next depth level.
• Expand shallowest unexpanded node,
i.e. expand all nodes on a given level of the search tree before moving to the next
level
• Implementation: use queue (enqueue and dequeue) data structure to store
the list:
Expansion: put successors at the end of queue
Pop nodes from the front of the queue
• Properties:
Takes space: keeps every node in memory 50
51
More
Breadth-First Search (BFS)
A
Successors: B,C,D
Initial state D
B C
E F G H I J
K N O P Q R
L M
Goal state
S T U
Fringe: A (FIFO) Visited:
Breadth-First Search (BFS)
A
Next node
B D
Successors: E,F C
E F G H I J
K N O P Q R
L M
S T U
Fringe: B,C,D (FIFO) Visited: A
Breadth-First Search (BFS)
A
Next node
Successors: G,H B C D
E F G H I J
K N O P Q R
L M
S T U
Fringe: C,D,E,F (FIFO) Visited: A, B
Breadth-First Search (BFS)
A
Next node
Successors: I,J
B C D
E F G H I J
K L M N O P Q R
S T U
Fringe: D,E,F,G,H (FIFO) Visited: A, B, C
Breadth-First Search (BFS)
A
Successors: K,L
B C D
Next node
E F G H I J
K N O P Q R
L M
S T U
Fringe: E,F,G,H,I,J (FIFO) Visited: A, B, C, D
Breadth-First Search (BFS)
A
Successors: M D
B C
Next node
E F G H I J
K N O P Q R
L M
S T U
Fringe: F,G,H,I,J,K,L (FIFO) Visited: A, B, C, D, E
Breadth-First Search (BFS)
A
Successors: N D
B C
Next node
E F G H I J
K L M N O P Q R
S T U
Fringe: G,H,I,J,K,L,M (FIFO) Visited: A, B, C, D, E, F
Breadth-First Search (BFS)
A
Successors: O D
B C
Next node
E F G H I J
K N O P Q R
L M
S T U
Fringe: H,I,J,K,L,M,N (FIFO) Visited: A, B, C, D, E, F, G
Breadth-First Search (BFS)
A
Successors: P,Q
B C D
Next node
E F G H I J
K N O P Q R
L M
S T U
Fringe: I,J,K,L,M,N,O (FIFO) Visited: A, B, C, D, E, F, G, H
Breadth-First Search (BFS)
A
Successors: R
B C D
Next node
E F G H I J
K N O P Q R
L M
S T U
Fringe: J,K,L,M,N,O,P,Q (FIFO) Visited: A, B, C, D, E, F, G, H, I
Breadth-First Search (BFS)
A
Successors: S
B C D
E F G H I J
Next node
K N O P Q R
L M
S T U
Fringe: K,L,M,N,O,P,Q,R (FIFO) Visited: A, B, C, D, E, F, G, H, I, J
Breadth-First Search (BFS)
A
Successors: T D
B C
E F G H I J
Next node
K L N O P Q R
M
S T U
Fringe: L,M,N,O,P,Q,R,S (FIFO) Visited: A, B, C, D, E, F, G, H, I, J, K
Breadth-First Search (BFS)
A
Successors: B C D
E F G H I J
K L M N O P Q R
Next node
S T U
Fringe: M,N,O,P,Q,R,S,T (FIFO) Visited: A, B, C, D, E, F, G, H, I, J, K, L
Breadth-First Search (BFS)
A
Goal state achieved
Successors: D
B C
E F G H I J
K L M N O P Q R
Next node
S T U
Fringe: N,O,P,Q,R,S,T (FIFO) Visited: A, B, C, D, E, F, G, H, I, J, K, L, M
66
Activity 1!
The queue evolves like this
A
B C D
C D E F
D E F G H I
E F G H I J
F G H I J K L
G H I J K L M
H I J K L M
I J K L M N O
J K L M N O
K L M N O P Q
L M N O P Q
M N O P Q
N O P Q
O P Q
P Q R S T
Q R S T
R S T U
S T U
T U V
U V
V
67
Conti..
Web Crawling: BFS indexing web pages. The algorithm starts
traversing from the source page and follows all the links
associated with the page.
GPS Navigation systems: BFS is one of the best algorithms used
to find neighboring locations by using the GPS system.
Find the Shortest Path & Minimum Spanning Tree for an
unweighted graph: BFS can allow this by traversing a minimum
number of nodes starting from the source node.
Broadcasting: Networking makes use of what we call as packets
for communication. These packets follow a traversal method to
reach various networking nodes. BFS is being used as an
algorithm that is used to communicate broadcasted packets
across all the nodes in a network.
Peer to Peer Networking: BFS can be used as a traversal method
to find all the neighboring nodes in a Peer to Peer Network. For
example, BitTorrent uses Breadth-First Search for peer to peer
communication.
68
Uniform Cost Search
• So far in BFS we’ve ignored the issue of costs.
• The goal of this technique is to find the shortest path to the goal in terms of cost.
It modifies the BFS by always expanding least-cost unexpanded node
• Implementation: nodes in list keep track of total path length from start to that node
List kept in priority queue ordered by path cost
A
1 10 S S S
S
5 B 5
S G 0 A B C A B C A B C
1 5 15 5 15 15
15 5 G G G
C 11 11 10
A route-finding problem, (a) The state space, showing the cost for each operator, (b) Progression of the search. Each
node is labeled with g(n). At the next step, the goal node with g = 10 will be selected.
• Properties:
This strategy finds the cheapest solution provided the cost of a path must never decrease as we
go along the path g(successor(n)) ≥ g(n), for every node n
69
Takes space since it keeps every node in memory
Conti..
Initialization: { [ S , 0 ] }
Iteration1: { [ S->A , 1 ] , [ S->G , 12 ] }
Iteration2: { [ S->A->C , 2 ] , [ S->A->B , 4 ] , [ S->G , 12] }
Iteration3: { [ S->A->C->D , 3 ] , [ S->A->B , 4 ] , [ S->A->C->G , 4 ] , [ S->G , 12 ] }
Iteration4: { [ S->A->B , 4 ] , [ S->A->C->G , 4 ] , [ S->A->C->D->G , 6 ] , [ S->G , 12 ] }
Iteration5: { [ S->A->C->G , 4 ] , [ S->A->C->D->G , 6 ] , [ S->A->B->D , 7 ] , [ S->G , 12 ] }
Iteration6 gives the final output as S->A->C->G.
More 70
Activity!
71
Depth-first search
• Expand one of the node at the deepest level of the tree.
Only when the search hits a non-goal dead end does the search
go back and expand nodes at shallower levels
• Implementation: treat the list as stack
Expansion: push successors at the top of stack
Pop nodes from the top of the stack
• Properties
Incomplete and not optimal: fails in infinite-depth spaces,
spaces with loops.
• Modify to avoid repeated states along the path
Takes less space (Linear): Only needs to remember up to the 72
Conti…
73
Depth-First Search (DFS)
A
Successors: B,C,D
C Initial state D
B
E F G H I J
K N O P Q R
L M
S T Goal state U
Fringe: A (LIFO) Visited:
Depth-First Search (DFS)
A
Successors: E,F
B C D
E F G H I J
K N O P Q R
L M
S T U
Fringe: B,C,D (LIFO) Visited: A
Depth-First Search (DFS)
A
Successors: K,L
B C D
E F G H I J
K N O P Q R
L M
S T U
Fringe: E,F,C,D (LIFO) Visited: A, B
Depth-First Search (DFS)
A
Successors: S D
B C
E F G H I J
K N O P Q R
L M
S T U
Fringe: K,L,F,C,D (LIFO) Visited: A, B, E
Depth-First Search (DFS)
A
Successors: C D
B
E F G H I J
K N O P Q R
L M
S T U
Fringe: S,L,F,C,D (LIFO) Visited: A, B, E, K
Depth-First Search (DFS)
A
Successors: T D
B C
E F G H I J
K N O P Q R
L M
S T U Visited: A, B, E, K, S
Fringe: L,F,C,D (LIFO) Backtracking
Depth-First Search (DFS)
A
Successors: D
B C
E F G H I J
K L N O P Q R
M
S T U
Fringe: T,F,C,D (LIFO) Visited: A, B, E, K, S, L
Depth-First Search (DFS)
A
Successors: M
B C D
E F G H I J
K L N O P Q R
M
S T U Visited: A, B, E, K, S, L, T
Fringe: F,C,D (LIFO) Backtracking
Depth-First Search (DFS)
A
Successors: B C D
E F G H I J
K L N O P Q R
M
S T U
Fringe: M,C,D (LIFO) Visited: A, B, E, K, S, L, T, F
Depth-First Search (DFS)
A
Successors: G,H
B C D
E F G H I J
K L M N O P Q R
S T U Visited: A, B, E, K, S, L, T, F,
Fringe: C,D (LIFO) M Backtracking
Depth-First Search (DFS)
A
Successors: N B C D
E F G H I J
K L M N O P Q R
S T U
Fringe: G,H,D (LIFO) Visited: A, B, E, K, S, L, T, F, M, C
Depth-First Search (DFS)
A
Goal state achieved
Successors: D
B C
Finished search
E F G H I J
K L M N O P Q R
S T U
Fringe: N,H,D (LIFO) Visited: A, B, E, K, S, L, T, F, M, C,
Activity!
A
B C D
E F C D
K L F C D
L F C D
F C D
M C D
C D
G H I D
H I D
N O I D
O I D
R S T I D
S T I D
V T I D
T I D
I D
D
J
P Q
Q
U
86
Depth-Limited Strategy
• Depth-first with depth cutoff k (maximal
depth below which nodes are not expanded)
C E E B B F
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
Example
90
Iterative Deepening Search(IDS)
• IDS solves the issue of choosing the best depth limit by trying all possible depth limit:
Perform depth-first search to a bounded depth d, starting at d = 1 and increasing it by 1 at each
iteration.
91
Example
More
92
Activity!
93
Bidirectional Search
• Simultaneously search both forward from the initial state to the goal and
backward from the goal to the initial state, and stop when the two searches
meet somewhere in the middle
Requires an explicit goal state and invertible operators (or backward chaining).
Decide what kind of search is going to take place in each half using BFS, DFS, uniform
cost search, etc.
Start Goal
94
Example
Intersection at: 9
*****Path*****
14 8 9 10 12 16
95
Activity!
Intersection at: 7
*****Path*****
04 6 7 8 10 14
96
…. More
• Advantage, Disadvantage and Real World Application of
BFS,
UCS,
DFS,
DLS,
IDS and
Bidirectional Search is Reading Assignment !!
• Questions from those topics may appear in the exam. 97
Comparing Uninformed Search
Strategies Complete Optimal Time complexity Space
complexity
99
Quiz
1. Breadth-First Search?
2. Depth-First Search?
3. Uniform-Cost Search?
4. Iterative-Deepening Search?
100
Solutions!
101
Find nodes expanded and Solution Path
1. Breadth-First Search?
2. Depth-First Search?
3. Uniform-Cost Search?
4. Iterative-Deepening Search?
102
Solution
• Depth-First Search: S A D E G
Solution found: S A G
• Breadth-First Search: S A B C D E G
Solution found: S A G
• Uniform-Cost Search: S A D B C E G
Solution found: S B G
• Iterative-Deepening Search: S A B C S A D E G
Solution found: S A G
103
Informed Search
Informed Search
• Search efficiency would be improved greatly if there is a way to order the choices
so that the most promising are explored first.
This requires domain knowledge of the problem.
• Add domain specific knowledge to select what is the best one to continue
searching along the path.
• Define a heuristic function, h(n) that estimates the goodness of a node n, based on
domain specific information that is computable from the current state description.
• Heuristics is knowledge about the domain that helps to undertake focused search.
It is rule of thumb, or guess that limits the search space towards the goal.
Example: 8- puzzle
105
Heuristic function, h(n)
• It is an estimate of how close we are to a goal state.
• Heuristic function h(n)
h(n) = estimated cost of path from state n to a goal state.
• Heuristic function is good for the following problems:
Route finding: straight line distance
8-puzzle: (i) Number of mismatched tiles and (ii) Manhattan or city block distance (sum of
distances each tile is from its goal position)
8-queens: Min-conflict or max conflict – (that is, the number of attacking queens)
• Note that:
h(n) ≥ 0 for all nodes n
h(n) = 0 if n is a goal node
h(n) = infinity if n is a dead-end from which a goal can’t be reached 106
Best first search
• It is a generic name to the class of informed methods
• Nodes are ordered in the open list so that the one with the best evaluation (minimum
cost) is expanded first
Evaluation function, f incorporates domain specific information and determines the desirability of
expanding the node
It aims to find low-cost solutions towards goal node
• There are two measures
The use of the path cost g to decide which path to extend further (e. g. uniform cost search).
The use of some estimate of the cost of the path from current state to the goal state (heuristics h)
The first measure may not direct search towards the goal. It needs to be supplemented by the
second measure
107
Best First Search Approaches
• Two approaches to find the shortest path:
Greedy search: minimizes estimated cost to reach a goal
A*-search: minimizes the total path cost
• When expanding a node n in the search tree, greedy search uses the
estimated cost to get from the current state to the goal state, define as
h(n).
In route finding problem h(n) is the straight-line distance
• We also possess the sum of the cost to reach that node from the start
state, define as g(n).
In route finding problem this is the sum of the step costs for the search
path.
• For each node in the search tree, an evaluation function f(n) can be
defined as the sum of these functions.
f(n) = g(n) + h(n) 108
Greedy Search
• A best first search that uses a heuristic function h(n) alone to guide the search
Selects node to expand that is closest (hence it’s greedy) to a goal node
The algorithm doesn’t take minimum path costs from initial to current nodes into
account, it just go ahead optimistically and never looks back.
• Implementation: expand 1st the node closest to the goal state, i.e. with evaluation function
f(n) = h(n)
h(n) = 0 if node n is the goal state
Otherwise h(n) ≥ 0; an estimated cost of the cheapest path from the state at node n to
a goal state
Example 1: Route finding problem
109
Example
110
Solution to Route Finding
f(n) = 366 Arad
114
Solution to Route Finding
f(n) = 0 + 366 Arad
393
Sibiu =140+253 Timisoara 447 Zerind 449
413 =
220+193
Arad 646 Fagaras 415 Oradea 671
Rimnicu Vilcea
415
=317+98 Pitesti Craiova 573
418 Bucharest
=418+0 Craiova 615 Rimnicu 607
115
Properties
• A* search is complete, optimal, and optimally efficient for any given
admissible heuristic function
• Complete
It guarantees to reach to goal state
• Optimality
If the given heuristic function is admissible, then A* search is optimal.
• Takes memory:
It keeps all generated nodes in memory
Both time and space complexity is exponential
• Time and space complexity of heuristic algorithms depends on the
quality of the heuristic function.
Worst case is exponential 116
Local Search
• In many problems, the path to the goal is irrelevant; the goal state itself is the
solution
Local Search: widely used for very big problems
Returns good but not optimal solutions
State space = set of complete configurations. Find configuration satisfying
constraints
Examples: n-Queens, airline flight schedules
In these cases, we can use local search algorithms that keep a single current state, try to
improve it.
• Being able to ignore the path offers two main advantages:
Less memory is used, generally a constant amount.
A reasonable solution can be found in infinite search spaces for which systematic search would
be unsuited. 117
Conti..
• They are also very useful for optimization problems where
the goal is to find the best state according to a function.
• Many of these problems are not suitable for standard search.
• As an example, nature provides a function of reproductive
Darwin defined evolution as
success that is used to guide Darwinian evolution. "descent with modification,"
Evolution tries to optimize this function, but there is no path the idea that species change
over time, give rise to new
cost or goal test. species, and share a common
• Example: n-queens. Put n queens on an n × n board with no ancestor.
two queens on the same row, column, or diagonal.
118
Conti..
Key idea (surprisingly simple):
1) Select (random) initial state (initial guess at solution)
e.g. guess random placement of N queens
2) Make local modification to improve current state
e.g. move queen under attack to “less attacked” square
3) Repeat Step 2 until goal state found (or out of time)
cycle can be done billions of times
Not necessarily!
Requirements: Method is incomplete.
Generate an initial (often random; probably-not-optimal or even valid) guess
Evaluate quality of guess
Move to other state (well-defined neighborhood function)
• The general idea is to start with a complete configuration and to make modifications to
improve its quality
• It keeps track of only the current state and try to improve it until goal state is reached,
It does not look ahead beyond the immediate neighbors of that state
• Implementation:
Consider all states laid out on the surface of a landscape
IIA moves around the landscape trying to find the highest peaks, which are the optimal solutions
• IIA search algorithms:
Hill climbing
Simulated annealing
Local beam search
Genetic algorithms (Genetic programming)
Tabu search 120
ADVERSARIAL SEARCH
• Games Playing as Search Problem
• Min-max Algorithm
• Alpha-Beta Pruning
Which one/s are Adversarial Search?
Adversarial search is search when there is an "enemy" or
"opponent" changing the state of the problem every step
in a direction you do not want.
Simply it is search in a competitive environments.
Because you change state, but then you don't control the
next state.
Examples: Chess, business, trading, war. 122
Tic-Tac-Toe Problem
• Given n x n matrix, the object of the game is to make three of your symbol
in a row, column or diagonal
This is a win state for the game.
• One player is designated as player X and makes the first play by marking an
X into any of the n x n open squares of the board.
Think a 3 x 3 square board where the player can put an X in any of the
9 open places
• The second player, "O", then follows suit by marking an O into any of the
other open squares that remain.
• This continues back-and-forth until one player wins the game or players fill
all squares on the board without establishing a winner. This is a draw. 123
Partial Game Tree for Tic-Tac-Toe Problem
8*9
7*(8*9)
124
How to play a game?
• The main objective in playing is to win the game; which requires to select the best move
carefully:
The system first generates all possible legal moves, and applies them to the current
board.
Evaluate each of the positions/states and
Determine the best one to move.
In a game like tic-tac-toe this process is repeated for each possible move until the
game is won, lost, or drawn.
Wait for your opponent to move and repeat
• For real problems, the search tree is too big to make it The
possible
game treetoforreach thehas
Tic-Tac-Toe terminal
255,168 leafstates
Example: nodes. This means that there are 255,168 possible
ways to play the game until it ends.
• Chess: 10120 nodes
• 8 Puzzle: 105 nodes
• How many nodes to consider in a Tic-Tac-Toe game with 3 x 3 board? 125
• In normal search problem, the optimal
solution is the sequence of actions
leading to the goal state.
• How can we come up with optimal
decision in game? Or
• Ex. How to win tic-tac-toe?
126
Adversarial Search
• In AI, it is used in game playing since one player's
attempts in maximizing winning (fitness) the game is
opposed by another player.
• The search tree in adversarial games like tic-tac-toe
consist of alternating levels where the moving (MAX)
player tries to maximize fitness and then the opposing
(MIN) player tries to minimize it.
• To simplify let us consider a typical case of:
2-person game
Zero-sum game 127
Perfect information
Typical case
2-person game: two players alternate moves
e.g. chess playing
Zero-sum game: one player’s loss is the other’s gain
The zero-sum assumption allows us to use a single evaluation function to describe the goodness of a board with respect to both
players.
f(n) > 0: position n good for A and bad for B
f(n) < 0: position n bad for A and good for B
f(n) near 0: position n is a neutral position
f(n) = +infinity: win for A
f(n) = -infinity: win for B
Perfect information: both players have access to complete information about the state of the game. No
information is hidden from either player.
Board configuration is known completely to both players at all times.
Any examples of perfect information game:
Tic-Tac-Toe & Chess. 128
Playing cards is not. Cards held by one are not known to others.
How a game can be formally defined?
• A game can be formally defined as a kind of search problem with the following elements:
1. S0:The initial state, which specifies how the game is set up at the start.
2. PLAYER(s): Defines which player has the move in a state.
3. ACTIONS(s): Returns the set of legal moves in a state.
4. RESULT(s, a): The transition model, which defines the result of a move.
5. 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.
6. 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. Ex. -1,0,1 in tic-
tac-toe
The initial state, ACTIONS function, and RESULT function define the game tree for the game
—a tree where the nodes are game states and the edges are moves.
129
Game Tree Search
Problem spaces for typical games are represented as trees
In game tree, the nodes are game states and the edges are moves.
Game tree represents possible moves by both players given an initial configuration.
Each node represents a (board) configuration. Ex. Each node marked by a letter (A, B, etc.)
Root node represents initial configuration of the board at which a decision must be made as to what is the best
single move to make next.
If it is my turn to move, then the root is labeled a "MAX" node indicating it is my turn; otherwise it is labeled a
"MIN" node to indicate it is my opponent's turn.
Children of a node n indicate possible configurations after the player makes a move from node n
Ex. B, C, D are children of A. They are possible configurations after player makes a move from configuration A.
In case of alternating moves between the players, alternating levels in a tree have alternating moves.
Each level of the tree has nodes that are all MAX or all MIN; nodes at level i are of the opposite kind from those at
130
level i+1.
Example… Game tree
• Game between players X and Y. Let us analyze from X's perspective by
looking ahead of two moves.
• H is a winning state for X (marked by plus infinity). If H reached, X wins. No
further moves.
• E, M are losing states for X (marked by minus infinity). If E or M reached, X
loses. No further moves.
Min
136
137
138
139
140
141
142
143
144
145
146
Exercise 1: Use Min-Max to find optimal play
147
Exercise 2: Identify the optimal play path?
MAX
MIN
MAX
MIN
MAX
MIN
148
Comments
• A complete depth-first search exploration of the game
tree is made by min-max algorithm.
In a tree of maximum depth M with b legal moves
at any point results in complexity on the order of
bM .
This exponential complexity means that min-max
by itself is not practical for real games.
• Basic idea: “If you have an idea that is surely bad, don't take the
time to see how truly awful or terrible it is.”
• Traverse the search tree in depth-first order and update alpha and beta values for
each search node as the search progresses
At each MAX node n, alpha(n) = maximum value found so far
At each MIN node n, beta(n) = minimum value found so far
The alpha values start at -infinity and only increase, while beta values start at +infinity and only
decrease.
Min
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Exercise 1
170
Exercise 2: Mark Pruned paths with ‘X’
MAX
MIN
MAX
MIN
MAX
MIN
171
Effectiveness of alpha-beta
• Alpha-beta is guaranteed to compute the same value for the root node as
computed by min-max, with less or equal computation
• Worst case: no pruning, examining bM leaf nodes, where each node has b
children and a M-depth search is performed
• Best case is when each player’s best move is the first alternative generated
Best case: examine only bM/2 leaf nodes.
The result shows that you can search twice as deep as min-max.
• In Deep Blue (chess program), they found empirically that alpha-beta
pruning meant that the average branching factor at each node was about
6 instead of about 35!
172