Artificial Intelligence 3451: UNIT: 02 Problem-Solving Through Search

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

Artificial Intelligence 3451

UNIT: 02
Problem-Solving Through Search
Lecture: 05

Farhad Muhammad Riaz


Lecture Outline
 Constraint Satisfaction Problem (CSP)
 Dynamic Programming
 Minimax Algorithm
 α-β Pruning
Constraint satisfaction problems (CSPs)
 CSP:
 state is defined by variables Xi with values from domain Di
 goal test is a set of constraints specifying allowable combinations of
values for subsets of variables

 Allows useful general-purpose algorithms with more power


than standard search algorithms

3
Example: Map-Coloring

 Variables WA, NT, Q, NSW, V, SA, T

 Domains Di = {red,green,blue}

 Constraints: adjacent regions must have different colors


 e.g., WA ≠ NT

4
Example: Map-Coloring

 Solutions are complete and consistent assignments, e.g., WA


= red, NT = green,Q = red,NSW = green,V = red,SA = blue,T
= green

5
Constraint graph
 Binary CSP: each constraint relates two variables
 Constraint graph: nodes are variables, arcs are constraints

6
Dynamic Programming
Strategy
Dynamic Programming is a technique for solving
optimization problems. Generally, such problems
have many feasible solutions, each having some
associated cost. The dynamic programming method
determines the best solution, that is, the one which
has the maximum or minimum cost.
The algorithm works by splitting a problem into a sequence of optimization subproblems.
Thesubproblems
The dynamic programming
are solved,appears to combined
and then have sametoapproach as divide-and-conquer
obtain solution algorithm.
to the main problem.
There are, however, some subtle differences. Unlike the divide-and-conquer method, the
dynamic programming splits the problem into overlapping subproblems , which are solved in
bottom up fashion. The optimal solutions to subproblems are stored into a table, which is
used repeatedly to build the final or global optimal solution.
Dynamic Programming
Solution
In general, solution of problem is obtained by
using the dynamic programming algorithm in the
following manner:
1) A recursion is set up to express the running time of the main
problem in terms running times of overlapping subproblems

2) The recursion is solved in bottom-up manner, by using the


initial conditions and previously computed values. The results
are saved in a table

3) The tabular solutions to sub-problems are used to construct


the solution to the main solution
Dynamic Programming
Building Solution
The steps involved in building a DP solution are depicted in the diagram. First, solution to
the smallest subproblem is obtained using the initial conditions. The result is saved in a table.
Using this table, the optimum solution is obtained for the next level of subproblem. This
procedure is continued until the solution to the main problem is found.

Initial conditions

Dynamic Programming procedure


Optimality Principle
Dynamic Programming
Optimality Principle
The dynamic programming approach is based on the
principle
states: “Optimal of optimality.
solution The can
to a problem principle
be found, if there exists an optimal solution to a
sub-problem”

Dynamic Programming solution is feasible when principle of optimality holds true for a
given problem.

The principle may not be applicable to all kinds of optimization problems. This condition
places a restriction on the wider applicability of dynamic programming algorithm.
Optimality Principle
Shortest Path
¾ The principle of optimality holds true for shortest paths in a
graph
Example: Consider the weighted graph shown in the diagram
It can be seen that the shortest distance the between the vertices A and D is 35
(i) The shortest path from A to D is A→B→C→D = 20+5+10=35
(ii) The shortest path from A to C is A→B→C=20+5=25
(iii) The shortest path from A to B is A→B=20.
It follows that each of the sub-paths from vertex A to vertex D is also the shortest
path between intermediate vertices. Thus, the principle of optimality holds true for
shortest distances in the sample graph.
50
A D

20 10

B C
5

Sample weighted graph. Shortest


paths are shown with bold lines
Optimality Principle
Longest Path
¾ The principle of optimality does not hold true for longest paths in a
graph
Example: Consider the sample weighted graph shown in the diagram
The longest path from C to A, is
C→D→A= 10+50 = 60
The sub-path C→D=10 is not the longest path. In fact, the longest path simple path
from C to D consists of sub-paths C→B→A→D = 75

50
A D

20 10

5
B C

Sample weighted graph. The longest


paths are shown with bold lines

Dynamic Programming /IIU 2008 / Dr. A. Sattar/9


Dynamic Programming
Applications

The DP has several applications. Some common useful applications are:

• Shortest distances in a network

• Longest common subsequences in strings

• Optimal matrix chain multiplication

• Optimal triangulation of a polygon

• Optimized binary search trees

• Optimal Packaging of Bins

• Optimal Assembly line scheduling


Minimax Algorithm
 Minimax algorithm
 Perfect play for deterministic, 2-player game
 Max tries to maximize its score
 Min tries to minimize Max’s score (Min)
 Goal: move to position of highest minimax value
 Identify best achievable payoff against best play
Minimax Algorithm
Minimax Algorithm

Payoff for Max


Minimax Algorithm (cont’d)

3 9 0 7 2 6

Payoff for Max


Minimax Algorithm (cont’d)

3 0 2

3 9 0 7 2 6

Payoff for Max


Minimax Algorithm (cont’d)

3 0 2

3 9 0 7 2 6

Payoff for Max


Minimax Algorithm (cont’d)
 Properties of minimax algorithm:
 Complete? Yes (if tree is finite)

 Optimal? Yes (against an optimal opponent)

 Time complexity? O(bm)
 m – maximum depth of tree; b branching factor
 Space complexity? O(bm) (depth-first exploration, if it
generates all successors at once)
 For chess, b ≈ 35, m ≈100 for "reasonable" games
 exact solution completely infeasible
m – maximum depth of the tree; b – legal moves;
Minimax Algorithm
 Limitations
 Not always feasible to traverse entire tree
 Time limitations
 Key Improvement
 Use evaluation function instead of utility
 Evaluation function provides estimate of utility at given position

 More soon…
α-β Pruning

 Can we improve search by reducing the size of the


game tree to be examined?

 Yes!!! Using alpha-beta pruning

Principle
– If a move is determined worse than another move already
examined, then there is no need for further examination of the
node.
Alpha-Beta Pruning (αβ prune)
 Rules of Thumb

 α is the best ( highest) found so far along the path for Max
 β is the best (lowest) found so far along the path for Min
 Search below a MIN node may be alpha-pruned if the its β  
of some MAX ancestor
 Search below a MAX node may be beta-pruned if the its  β
of some MIN ancestor.
Alpha-Beta Pruning Example

1. Searchbelow a MIN
node may be alpha-
pruned if the beta
value is <= to the
alpha value of some
MAX ancestor.

2. Search below a
MAX node may be
beta-pruned if the
alpha value is >= to
the beta value of
some MIN ancestor.
Alpha-Beta Pruning Example

1. Searchbelow a MIN 3
node may be alpha-
pruned if the beta
value is <= to the
alpha value of some
MAX ancestor.
 3
2. Search below a
MAX node may be
beta-pruned if the
alpha value is >= to
the beta value of
some MIN ancestor.
3
Alpha-Beta Pruning Example

1. Searchbelow a MIN 3
node may be alpha-
pruned if the beta
value is <= to the
alpha value of some
MAX ancestor.
 3
2. Search below a
MAX node may be
beta-pruned if the
alpha value is >= to
the beta value of
some MIN ancestor.
3 5

β
Alpha-Beta Pruning Example

1. Searchbelow a MIN 3
node may be alpha-
pruned if the beta
value is <= to the
alpha value of some
MAX ancestor.
 3 0
2. Search below a
MAX node may be
beta-pruned if the α
alpha value is >= to
the beta value of
some MIN ancestor.
3 5 0
β
Alpha-Beta Pruning Example

1. Searchbelow a MIN 3
node may be alpha-
pruned if the beta
value is <= to the
alpha value of some
MAX ancestor.
 3 0 2
2. Search below a
MAX node may be
α α
beta-pruned if the
alpha value is >= to
the beta value of
some MIN ancestor.
3 5 0 2

β
-β Search Algorithm

1. If terminal state, compute e(n) and return the result.


2. Otherwise, if the level is a minimizing level, pruning
• Until no more children or
- search on a child
- If
• Return min
3. Otherwise, the level is a maximizing level: pruning
• Until no more children or
– search on a child.
– If See page 170 R&N
• Return
Another Example
1.Search below a MIN
node may be alpha-
pruned if the beta
value is <= to the
alpha value of some
MAX ancestor.

2. Search below a
MAX node may be
beta-pruned if the
alpha value is >= to
the beta value of
some MIN ancestor.
Example
1.Search below
a MIN node
may be
alpha-pruned
5 if the beta
5 3 value is <= to
the alpha
value of
some MAX
ancestor.
5 3
2. Search
5 6 3 7 below a
α MAX node
may be beta-
pruned if the
alpha value is
>= to the beta
5 6 3 value of
some MIN
5 0 6 1 3 2 4 7 ancestor.
β
Properties of α-β Prune
 Pruning does not affect final result

 Good move ordering improves effectiveness of pruning


b(e.g., chess, try captures first, then threats, froward
moves, then backward moves…)

 With "perfect ordering," time complexity = O(bm/2)


 doubles depth of search that alpha-beta pruning can explore
Example of the value of reasoning about which
computations are relevant (a form of metareasoning)

You might also like