unit 2 AI part 2
unit 2 AI part 2
Intelligence
Unit 2 (Part 2)
Heuristic Functions
• Heuristic means finding solution to a problem quickly and
efficiently.
• The 8-puzzle was one of the earliest heuristic search
problems. As mentioned earlier, the object of the puzzle is to
slide the tiles horizontally or vertically into the empty space
until the configuration matches the goal configuration.
• The average solution cost for a randomly generated 8-puzzle
instance is about 22 steps. The branching factor is about 3.
(When the empty tile is in the middle, four moves are
possible; when it is in a corner, two; and when it is along an
edge, three.) This means that an exhaustive tree search to
depth 22 would look at about 3**22 ≈ 3.1 × 10**10 states.
• If we want to find the shortest solutions, we need a heuristic
function.
Heuristic Function:
h(n) = h1(n) + h2(n)
Where,
h1(n) = the number of misplaced tiles.
h2 (n) = the sum of the distances of the tiles from their
goal positions.
h1(n) = Here, all of the eight tiles are out of
position, so the start state would have h1(n) = 8.
h2(n) = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18.
h(n) = 8 +18 = 26.
• It is easy to see from the definitions of the two
heuristics that, for any node n, h2(n) ≥ h1(n).
Generating admissible
heuristics from relaxed
problems
• If the rules of the puzzle were changed so that a tile
could move anywhere instead of just to the adjacent
empty square, then h1(n) would give the exact
number of steps in the shortest solution.
• Similarly, if a tile could move one square in any
direction, even onto an occupied square, then h2
would give the exact number of steps in the shortest
solution.
• A problem with fewer restrictions on the actions is
called a relaxed problem.
• If a problem definition is written down in a formal
language, it is possible to construct relaxed problems
automatically.
• For example, if the 8-puzzle actions are described as:
• A tile can move from square A to square B if A is
horizontally or vertically adjacent to B and B is blank,
• we can generate three relaxed problems by
removing one or both of the conditions:
• (a) A tile can move from square A to square B if A is
adjacent to B.
• (b) A tile can move from square A to square B if B is
blank.
• (c) A tile can move from square A to square B.
Generating admissible
heuristics from subproblems:
Pattern databases
• Admissible heuristics can also be derived from the
solution cost of a subproblem of a given problem.
• The subproblem involves getting tiles 1, 2, 3, 4 into their
correct positions. Clearly, the cost of the optimal solution of
this subproblem is a lower bound on the cost of the
complete problem.
• One might wonder whether the heuristics obtained from the
1-2-3-4 database and the 5-6-7-8 could be added, since the
two subproblems seem not to overlap. Would this still give
an admissible heuristic?
• The answer is no, because the solutions of the 1-2-3-4
subproblem and the 5-6-7-8 subproblem for a given state
will almost certainly share some moves—it is unlikely that 1-
2-3-4 can be moved into place without touching 5-6-7-8, and
vice versa.
• we record not the total cost of solving the 1-2- 3-4
subproblem, but just the number of moves involving 1-2-3-4.
Learning heuristics from
experience
• “Experience” here means solving lots of 8-
puzzles, for instance. Each optimal solution to an
8-puzzle problem provides examples from which
h(n) can be learned.
• From these examples, a learning algorithm can
be used to construct a function h(n) that can
predict solution costs for other states that arise
during search.
h(n) = c1x1(n) + c2x2(n)
• The constants c1 and c2 are adjusted to give the
best fit to the actual data on solution costs.
LOCAL SEARCH ALGORITHMS
AND OPTIMIZATION PROBLEMS
• When a goal is found, the path to that goal also
constitutes a solution to the problem. In many
problems, however, the path to the goal is irrelevant.
• For example, in the 8-queens problem, what matters is
the final configuration of queens, not the order in which
they are added.
• The same general property holds for many important
applications such as integrated-circuit design, factory-
floor layout, job-shop scheduling, automatic
programming, telecommunications network
optimization, vehicle routing, and portfolio
management.
LOCAL SEARCH ALGORITHMS
• In addition to finding goals, local search algorithms are
useful for solving pure optimization problems, in which
the aim is to find the best state according to an
objective function.
• Hill-climbing search
• Simulated annealing
• Local beam search
• Genetic algorithms
Hill-climbing search
• It is simply a loop that continually moves in the
direction of increasing value—that is, uphill.
• It terminates when it reaches a “peak” where no
neighbor has a higher value.
• The algorithm does not maintain a search tree, so the
data structure for the current node need only record the
state and the value of the objective function.
• Hill climbing does not look ahead beyond the immediate
neighbors of the current state.
• Hill climbing is sometimes called greedy local search
because it grabs a good neighbor state without thinking
ahead about where to go next.
Hill climbing often gets stuck for the
following reasons:
1. Local maxima: a local maximum is a peak
that is higher than each of its neighboring
states but lower than the global maximum.
2. Ridges: Ridges result in a sequence of local
maxima that is very difficult for greedy
algorithms to navigate.
3. Plateaux: a plateau is a flat area of the state-
space landscape. It can be a flat local
maximum, from which no uphill exit exists.
Simulated annealing
• Simulated annealing is a probabilistic algorithm
which tests points across a solution space to find
the lowest minima.
• The algorithm is termed “simulated annealing”
because it mirrors physical annealing, a process
in which a material is repeatedly heated and
cooled to elicit desired structural properties.
• It is used to solve VLSI layout problems.
• It has been applied widely to factory scheduling
and other large-scale optimization tasks.
Local beam search
• Keeping just one node in memory might seem to be an
extreme reaction to the problem of memory limitations.
The local beam search algorithm keeps track of k states
rather than just one.
• It begins with k randomly generated states. At each
step, all the successors of all k states are generated. If
any one is a goal, the algorithm halts. Otherwise, it
selects the k best successors from the complete list and
repeats.
• A variant called stochastic beam search, instead of
choosing the best k from the the pool of candidate
successors, stochastic beam search chooses k
successors at random.
Genetic algorithms
• A genetic algorithm, in which successor states are
generated by combining two parent states rather than
by modifying a single state.
• GAs begin with a set of k randomly generated
states, called the population. Each state, or
individual, is represented as a string over a
finite alphabet—most commonly, a string of 0s
and 1s.
• The production of the next generation of states
is rated by the objective function, or the fitness
function.
• For each pair to be mated, a crossover point is
chosen randomly from the positions in the string.
• Finally, each location is subject to random
mutation with a small independent probability.
Constraint Satisfaction
Problems
• Solve the following map coloring problem using
Constraint Satisfaction problem with Domain = {red,
blue, green}
24
Games vs. search problems
"Unpredictable" opponent specifying a move for every
possible opponent reply
•
• Time limits unlikely to find goal, must approximate
•
25
Game search
27
Game tree (2-player, deterministic,
turns)
28
Minimax Algorithm
• How to deal with the contingency problem?
• Assuming the opponent is always rational and always
optimizes its behavior (opposite to us), we consider the best
opponent’s response
• Then the minimax algorithm determines the best move
29
Minimax
Perfect play for deterministic games
•
• Idea: choose move to position with highest minimax value
= best achievable payoff against best play
•
• E.g., 2-ply game: [will go through another eg in lecture]
•
30
Minimax. Example
31
32
Complexity of the minimax
algorithm
33
Solution to the complexity problem
34
Alpha Beta Pruning
• Some branches will never be played by rational players
since they include sub-optimal decisions for either
player
• First, we will see the idea of Alpha Beta Pruning
• Then, we’ll introduce the algorithm for minimax with
alpha beta pruning, and go through the example again,
showing the book-keeping it does as it goes along
35
Alpha beta pruning. Example
36
Minimax with alpha-beta pruning:
The algorithm
• Maxv: function called for max nodes
• Might update alpha, the best max can do far
• Minv: function called for min nodes
• Might update beta, the best min can do so far
37
Algorithm example: alphas/betas
shown
38
Properties of α-β
Pruning does not affect final result
•
41
Further extensions to real games
42