AI Lec 10
AI Lec 10
Lecture – 10
Prepared By: Prof (Dr.) Anirban Bose
Dept.: Computer Science & Engineering
Dr. B. C. Roy Engineering College, Durgapur
2 Simulated Annealing
In metallurgy, annealing is the process used to temper or harden metals and glass by
heating them to a high temperature and then gradually cooling them, thus allowing the
material to reach a low energy crystalline state.
The loop of the simulated-annealing algorithm is quite similar to hill climbing. Instead of
picking the best move, however, it picks a random move.
If the move improves the situation, it is always accepted. Otherwise, the algorithm accepts
the move with some probability less than 1.
3 An Example
Initial State – S
1 2 3
Goal State – G
4 5 Cost/Heuristic Function, E(n) – No. of Misplaced Tiles
6 7 8 Temperature, T – 100
Cooling Schedule – 0.95
Start (S) Transition Function – Function to generate a neighbouring
state from the current state.
Acceptance Probability, P -
DeltaE,E – E(n’) – E(n)
1 2 3 Algo:
while(iter <= MaxIter)
4 5 6
while(T > Tmin )
7 8 n’ = generateNeighbour(n)
deltaE(n, n’, T)
Goal (G)
if(deltaE <= 0 or r <= P)
n n’
T T*0.95
iter iter + 1
4 Simulated Annealing…
The probability decreases exponentially with the “badness” of the move—the amount ΔE
by which the evaluation is worsened.
The probability also decreases as the “temperature” T goes down: “bad” moves are more
likely to be allowed at the start when T is high, and they become more unlikely as T
decreases.
If the schedule (mapping of time to temperature and how fast the temperature decreases is
called the Annealing Schedule) lowers T slowly enough, the algorithm will find a global
optimum with probability approaching 1.
5 Genetic Algorithms
A genetic algorithm (or GA) is a variant of stochastic beam search in which successor
states are generated by combining two parent states rather than by modifying a single state.
Like beam searches, 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. For example, an 8-queens state must specify the positions of 8
queens, each in a column of 8 squares, and so requires 8 × log 2 8 = 24 bits.
Alternatively, the state could be represented as 8 digits, each in the range from 1 to 8.
Each state is rated by the objective function, or (in GA terminology) the fitness function.
A fitness function should return higher values for better states.
6 How GA Works?
7 Operators in GA
Selection: It is one of the most popular ways of parent selection. In this every individual
can become a parent with a probability which is proportional to its fitness.
Mutation: It may be defined as a small random tweak in the chromosome, to get a new
solution. It is used to maintain and introduce diversity in the genetic population and is
usually applied with a low probability.
8 Operators in Action
Fitness Function: X2 (Minimization)
1 0 1 0 0 1 1 1 1 0 1 0 0 1 0 0
pa1 off1
0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1
pa2 off2
Crossover point
pa1 fitness value: 1672=27889 off1 fitness value: 26896
pa2 fitness value = 784 off2 fitness value = 961
1 0 1 0 0 1 0 0 1 0 1 0 0 0 0 0
off1 off1 off1 fitness value: 25600
off2 fitness value = 729
0 0 0 1 1 1 1 1 0 0 0 1 1 0 1 1
off2 Mutation point off2
9 8 - Queens
Problem: placing eight queens on an 8×8
chessboard such that none of them attack one
another (no two are in the same row, column,
or diagonal).
States: All configuration of the chessboard
with queens.
State Transition Model: Moving a queen in
distinct places within a column.
Fitness Function: Number of pairs of non-
attacking queens in a particular state.
10 8 - Queens
To formulate this as a CSP, we define the variables to be the regions X = {WA,NT, Q,NSW , V,SA, T }.
The domain of each variable is the set Di = {red,green,blue}. The constraints require neighboring
regions to have distinct colors. Thus there are nine constraints: C = {SA WA,SA NT,SA Q,SA
NSW ,SA V, WA NT,NT Q, Q NSW ,NSW V }
13 Example…
Variables, .
Domains, .
Constraints,
OR
• The nodes of the graph correspond to variables of the problem, and a link
connects any two variables that participate in a constraint.
• CSP solvers can be faster than state-space searchers because the CSP solver
can quickly eliminate large swatches of the search space.
• For example, once we have chosen {SA = blue}, a search procedure would
have to consider 35 = 243 assignments for the five neighboring variables
instead of only 25 = 32 assignments to look at, a reduction of 87%.
14 Types of CSP Variables
■ Discrete variables
■ finite domains:
■ n variables, domain size d O (d n) complete assignments
■ e.g., map coloring and 8-queens problems are both of this kind.
infinite domains:
■ integers, strings, etc.
■ e.g., job scheduling, variables are start/end days for each job
WA NT Q NSW V SA T
Initial Domain RGB RGB RGB RGB RGB RGB RGB
After WA = Red R GB RGB RGB RGB GB RGB
After Q = Green R B G R B RGB B RGB
After V = Blue R B G R B ??? RGB
18 Map Coloring Problem
WA NT Q NSW V SA T
Initial Domain RGB RGB RGB RGB RGB RGB RGB
After NT = Green R G R G R B G
19 Map Coloring Problem: Backtrack
20 LOCAL SEARCH FOR CSPS
Local search algorithms (Hill Climbing, Simulated Annealing etc.) turn out to be effective in solving many
CSPs.
They use a complete-state formulation.
The initial state assigns a value to every variable, and the search changes the value of one variable at a time.
Typically, the initial guess violates several constraints. The point of local search is to eliminate the violated
constraints.
In choosing a new value for a variable, the most obvious heuristic is to select the value that results in the
minimum number of conflicts with other variables
21 Example
22 Excercise