AI Module4 1
AI Module4 1
• Local search algorithms operate using a single current node (rather than
multiple paths) and generally move only to neighbors of that node. Typically, the
paths followed by the search are not retained. Although local search algorithms
are not systematic, they have two key advantages:
(i) Use very little memory- usually a constant amount.
(ii) Can often find reasonable solutions in large or infinite (continuous) state
spaces for which systematic algorithms are not suitable.
Local Search Algorithms and Optimization
Problems (contd…)
• 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.
• Many optimization problems do not fit the “standard” search model (both
uninformed and informed).
• If elevation corresponds to cost, then the aim is to find the lowest valley- a global
minimum.
• If elevation corresponds to an objective function, then the aim is to find the highest peak-
a global maximum.
• We can convert from one to the other just by inserting a minus sign.
• Local search algorithms explore this landscape. A complete local search algorithm always
finds a goal if one exists; an optimal algorithm always finds a global minimum/maximum.
Local Search Algorithms and Optimization
Problems (contd…)
Hill Climbing
• It is often used when a good heuristic function is available for evaluating
states but when no other useful knowledge is available.
• This algorithm is simply a loop that continuously moves in the direction of
increasing value i.e., uphill.
• It terminates when it reaches a “peak” where no neighbor has a higher
value.
• The algorithm doesn’t maintain a search tree, so the current node data
structure only records the state and its objective function value.
• Hill–climbing doesn’t look ahead beyond the immediate neighbors of the
current state.
Hill Climbing Algorithm
1. Evaluate the initial state (IS). If it is the goal state (GS), then return it
and quit. Else consider IS as the current state (CS) and proceed.
• Algorithm
1. Evaluate the initial state (IS). If it is the goal state (GS) , then return it and
quit. Else consider IS as the current state (CS) and proceed.
2. Loop until a solution is found or until a complete iteration produces no change
to the CS:
Steepest – Ascent Hill Climbing Algorithm
a) Let successor (SUCC) be a state such that any NS that can be generated from
CS is better than SUCC. [ i.e., setting SUCC to a minimum value at the
beginning of an iteration or set CS as SUCC]
c) If the SUCC is better than CS, then set CS to SUCC [i.e move to the next best
state]
Hill-Climbing Search
• "Like climbing Everest in thick fog with amnesia"
Limitations: Hill-Climbing Search
• Both basic and steepest-ascent hill climbing may fail to find a solution . Either
algorithm may terminate not by finding a goal state but by getting to a state from
which no better states can be generated. This will happen if the program has
reached either a local maximum, a plateau or a ridge.
• A local maximum is a state that is better than all its neighbors but is not better
than some other states further away.
• Solution of Plateau
– Expand few generation ahead to move to a different section of the search space.
• A Ridge is an area in the search space which is higher than its surroundings but
itself has slopes. It is not possible to traverse a ridge by a single move i.e., no
such operator is available.
• Solution of Ridges
– Apply several operators before doing the evaluation
Hill-Climbing Search (via ridge)
Hill-Climbing Search
• Problem: depending on initial state, can get stuck in local maxima
Hill-Climbing Search: 8 Queens Problem
• 3. Random-restart hill climbing adopts the well-known adage, “If at first you don’t succeed, try,
try again.” It conducts a series of hill-climbing searches from randomly generated initial states,
until a goal is found.
(The success of hill climbing depends very much on the shape of the state-space landscape: if
there are few local maxima and plateaus, random-restart hill climbing will find a good solution
very quickly.)
Simulated Annealing
• A hill-climbing algorithm that never makes “downhill” moves toward states with lower
value (or higher cost) is guaranteed to be incomplete, because it can get stuck on a local
maximum.
• Therefore, it seems reasonable to try to combine hill climbing with a random walk in
some way that yields both efficiency and completeness.
• Simulated annealing was first used extensively to solve VLSI layout problems in the early
1980s. It has been applied widely to factory scheduling and other large-scale
optimization tasks.
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.
• To explain simulated annealing, we switch our point of view from hill climbing to
gradient descent (i.e., minimizing cost) and imagine the task of getting a ping-pong ball
into the deepest crevice in a bumpy surface.
• If we just let the ball roll, it will come to rest at a local minimum. If we shake the
surface, we can bounce the ball out of the local minimum. The trick is to shake just hard
enough to bounce the ball out of local minima but not hard enough to dislodge it from
the global minimum.
2 4 7 4 8 5 5 2 3 2 7 5 2 4 1 1
(24 non-attacking pairs of queens) (23 non-attacking pairs of queens)
Genetic Algorithms
• (a) shows a population of four 8-digit strings representing 8-queens states.
• In (b), 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.
• So, for the 8-queens problem, we use the number of nonattacking pairs of queens, which has a
value of 28 for a solution.
• The values of the given four states are 24, 23, 20, and 11.
• In this particular variant of the genetic algorithm, the probability of being chosen for reproducing
is directly proportional to the fitness score, and the percentages are shown next to the raw
scores.
Genetic Algorithms
• In Figure (c), two pairs are selected at random for reproduction, in accordance with the
probabilities in (b). It is to be noticed that one individual is selected twice and one not
at all.
• For each pair to be mated, a crossover point is chosen randomly from the positions in
the string. In Figure, the crossover points are after the third digit in the first pair and
after the fifth digit in the second pair.
• In Figure (d), the offspring themselves are created by crossing over the parent strings at
the crossover point. For example, the first child of the first pair gets the first three
digits from the first parent and the remaining digits from the second parent, whereas
the second child gets the first three digits from the second parent and the rest from the
first parent. The 8-queens states involved in this reproduction step are shown in the
next figure.
Genetic Algorithms
The 8-queens states corresponding to the first two parents in Figure (c) and
the first offspring in Figure (d). The shaded columns are lost in the crossover
step and the unshaded columns are retained.
Genetic Algorithms
• The example shows that when two parent states are quite different, the crossover
operation can produce a state that is a long way from either parent state.
• It is often the case that the population is quite diverse early on in the process, so
crossover (like simulated annealing) frequently takes large steps in the state space early
in the search process and smaller steps later on when most individuals are quite similar.
• Finally, in Figure (e), each location is subject to random mutation with a small
independent probability.
• One digit was mutated in the first, third, and fourth offspring.
• In the 8-queens problem, this corresponds to choosing a queen at random and moving it
to a random square in its column.
Genetic Algorithms
• function GENETIC-ALGORITHM(population, FITNESS-FN) returns
an individual • function REPRODUCE(x , y) returns an individual
• inputs: population, a set of individuals • inputs: x , y, parent individuals
• n←LENGTH(x ); c←random number from 1 to n
• FITNESS-FN, a function that measures the fitness of an individual
• return APPEND(SUBSTRING(x, 1, c), SUBSTRING(y, c +
• repeat 1, n))
• new population ←empty set
• for i = 1 to SIZE(population) do
• x ←RANDOM-SELECTION(population, FITNESS-FN)
(A genetic algorithm is the same as the one
• y ←RANDOM-SELECTION(population, FITNESS-FN)
diagrammed in Figure, with one variation: in
• child ←REPRODUCE(x , y) this more popular version, each mating of
• if (small random probability) then child ←MUTATE(child ) two parents produces only one offspring, not
• add child to new population two.)
• population ←new population
• until some individual is fit enough, or enough time has elapsed
• return the best individual in population, according to FITNESS-FN
Constraint Satisfaction Problem (CSP)
Content
➢ Introduction
➢ Constraint Satisfaction Problems (CSP)
➢ Map Coloring
➢ N-Queen Problem
➢ Crossword Puzzle
➢ Sudoku
➢ Cryptarithmetic Problem
➢ Job-shop Scheduling
➢ Variations on CSP Formalism
➢ Solving CSP Problems
➢ Generate-and-Test
➢ Backtracking Search
➢ Consistency Driven
➢ Forward Checking
Introduction
• We have so far explored the idea that problems can be solved by searching in a space of states.
These states can be evaluated by domain-specific heuristics and tested to see whether they are goal
states.
• From the point of view of the search algorithm, each state is atomic, or indivisible—a black box with
no internal structure.
• However, there is a way to solve a wide variety of problems more efficiently. We use a factored
representation for each state: a set of variables, each of which has a value. A problem is solved
when each variable has a value that satisfies all the constraints on the variable.
• The main idea is to eliminate large portions of the search space all at once by identifying
variable/value combinations that violate the constraints.
• An assignment of values to some or all of the variables that does not violate any constraints is
called a consistent or legal assignment.
• A complete assignment is that in which every variable is mentioned and if it satisfies all the
constraints, then it is a solution.
Constraint Satisfaction Problem (CSP)
• A constraint satisfaction problem consists of three components, X, D, and C:
• X is a set of variables, {X1, . . . ,Xn}.
• D is a set of domains, {D1, . . . ,Dn}, one for each variable.
• C is a set of constraints that specify allowable combinations of values.
• Each domain Di consists of a set of allowable values, {v1, . . . , vk} for variable Xi.
For example, if X1 and X2 both have the domain {A,B}, then the constraint saying the two variables
must have different values can be written as <(X1,X2), [(A,B), (B,A)]> or as <(X1,X2), X1!= X2>.
Why formulate a problem as a CSP ?
• One reason is that the CSPs yield a natural representation for a wide variety of problems; if we
already have a CSP-solving system, it is often easier to solve a problem using it than to design a
custom solution using another search technique. In addition, 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} in the Australia problem, we can conclude that none
of the five neighboring variables can take on the value blue.
• Without taking advantage of constraint propagation, a search procedure would have to consider
3^5=243 assignments for the five neighboring variables;
• With constraint propagation we never have to consider blue as a value, so we have only 2^5=32
assignments to look at, a reduction of 87%.
Varieties of Constraints
• Unary constraints involve a single variable, e.g., SA != green
The constraints require neighboring regions to have distinct colors. Since there are nine places
where regions border, there are nine constraints:
C = {SA != WA, SA != NT, SA != Q, SA != NSW, SA != V, WA != NT, NT != Q, Q != NSW, NSW != V}
CSP Example1: Map Coloring (contd…)
• SA != WA is a shortcut for <(SA,WA), SA != WA>, where SA != WA can be fully enumerated in turn as
{(red , green), (red , blue), (green, red), (green, blue), (blue, red), (blue, green)}
• There are many possible solutions to this problem, such as
{WA = red, NT = green, Q = red, NSW = green, V = red,
SA = blue, T = red}
CSP Example1: Map Coloring (contd…)
• Constraint graph
• Binary CSP: Each constraint relates two variables
• Constraint graph: Nodes are variables, arcs are constraints
Constraint Graph
CSP Example 2: n-Queen problem
• Problem proposed: Chess player Max Bazzel (1848)
• Solution: S. Gunther(1874), J.W.L. Glaisher (refiner)
• The eight queens puzzle has 92 distinct solutions.
• If solutions that differ only by symmetry operations (rotations and reflections) of the board are
counted as one, the puzzle has 12 unique solutions.
• The following table gives the number of solutions for n queens, both unique and distinct.
• Note that the 6 queens puzzle has, interestingly, fewer solutions than the 5 queens puzzle!
CSP Example 3: Crossword Puzzle
• Complete the following puzzle with words:
AFT, ALE, EEL, HEEL, HIKE, HOSES, KEEL, KNOT, LASER, LEE, LINE, SAILS, SHEET, STEER, TIE
• Replace each letter by a distinct digit so that the resulting sum is correct.
1. D + E = 10 * C1 + Y
2. C1 + N + R = 10 * C2 + E
3. C2 + E + O = 10 * C3 + N
4. C3 + S + M = 10 * C4+ O
5. C4 = M = 1 (As M has to be non-zero.)
Where C1, C2, C3 and C4 can be either 0 or 1.
Other alphabets can take unique values from the set { 0,1, 2, 3, 4, 5, 6, 7, 9}
CSP Example 5: Cryptarithmetic Problem
• In the following pattern: Initial state
S E N D
M O R E M=1 (As C4 =1)
--------------- S=8 OR 9
O=0 OR 1 O=0
M O N E Y N=E OR E+1 N=E+1
C2=1
C1=1 C1=0 D=Y+5 N+R>8
R=8 E<>9 , E = 2, 3, 4 ends with conflict
SOLUTION:
{S=9, E=5, N=6, D=7, M=1, O=0, N=6 R=9
C3= 0 AND S=9 Contradiction
R=8, Y=2} R=9 - C1 as S=9
{ C1 =1, C2 = 1, C3 =0, C4 = 1} 5+D=Y OR 5+D=10 + Y
Y= 2
CSP Example 5: Cryptarithmetic Solution
• In the following pattern:
S E N D C4 C3 C2 C1 1 0 1 1
M O R E -------------------- --------------------
--------------- S E N D 9 5 6 7
M O N E Y + M O R E + 1 0 8 5
______________ ______________
C1=1 C1=0 M O N E Y 1 0 6 5 2
SOLUTION:
{S=9, E=5, N=6, D=7, M=1, O=0, R=8, Y=2} and
{ C1 =1, C2 = 1, C3 =0, C4 = 1}
CSP Example 5: Cryptarithmetic Problem
• These constraints can be represented in a constraint hypergraph as shown in the figure.
• A hypergraph consists of ordinary nodes (the circles in the figure) and hypernodes (the squares),
which represent n-ary constraints.
• Variables: F T U W R O
• Domains: {0,1,2,3,4,5,6,7,8,9}
• Consider the problem of scheduling the assembly of a car. The whole job is composed of tasks, and
we can model each task as a variable, where the value of each variable is the time that the task
starts, expressed as an integer number of minutes.
• Constraints can assert that one task must occur before so many tasks can go on at once.
• Constraints can also specify that a task takes a certain amount of time to complete.
• We now consider a small part of the car assembly, consisting of 15 tasks: install axles (front and
back), affix all four wheels (right and left, front and back), tighten nuts for each wheel, affix
hubcaps, and inspect the final assembly.
CSP Example 6: Job-shop scheduling
• We can represent the tasks with 15 variables:
X = {AxleF, AxleB, WheelRF, WheelLF, WheelRB, WheelLB, NutsRF, NutsLF , NutsRB,
NutsLB, CapRF , CapLF , CapRB, CapLB, Inspect} .
• The value of each variable is the time that the task starts.
• Next we say that, for each wheel, we must affix the wheel (which takes 1 minute), then tighten the
nuts (2 minutes), and finally attach the hubcap (1 minute, but not represented yet):
WheelRF + 1 ≤ NutsRF ; NutsRF + 2 ≤ CapRF ;
WheelLF + 1 ≤ NutsLF ; NutsLF +2 ≤ CapLF ;
WheelRB + 1 ≤ NutsRB; NutsRB + 2 ≤ CapRB;
WheelLB + 1 ≤ NutsLB; NutsLB + 2 ≤ CapLB .
CSP Example 6: Job-shop scheduling
Disjunctive constraint:
• Suppose we have four workers to install wheels, but they have to share one tool that helps put
the axle in place. We need a disjunctive constraint to say that AxleF and AxleB must not overlap
in time; either one comes first or the other does:
(AxleF + 10 ≤ AxleB) or (AxleB + 10 ≤ AxleF)
• This looks like a more complicated constraint, combining arithmetic and logic. But it still
reduces to a set of pairs of values that AxleF and AxleB can take on.
CSP Example 6: Job-shop scheduling
• We also need to assert that the inspection comes last and takes 3 minutes.
• Finally, suppose there is a requirement to get the whole assembly done in 30 minutes. We can
achieve that by limiting the domain of all variables:
Di = {1, 2, 3, . . . , 27}
• This particular problem is trivial (i.e. not important) to solve, but CSPs have been applied to
job-shop scheduling problems like this with thousands of variables. In some cases, there are
complicated constraints that are difficult to specify in the CSP formalism, and more advanced
planning techniques are used.
Variations on CSP Formalism
A) Discrete Variables
Continuous domains: Constraint satisfaction problems with continuous domains are common in the
real world and are widely studied in the field of operations research.
Examples:
Example: In the map-coloring problem it could be the case that South Australians won’t tolerate the color green;
we can express that with the unary constraint <(SA),SA != green>
Example: SA != NSW is a binary constraint. (A binary CSP is one with only binary constraints; it can be
represented as a constraint graph).
Example: Asserting that the value of Y is between X and Z, with the ternary constraint Between(X, Y, Z).
Variations on CSP Formalism (contd…)
• Global Constraints:
• A constraint involving an arbitrary number of variables is called a global constraint. (The name is
traditional but confusing because it need not involve all the variables in a problem).
• One of the most common global constraints is Alldiff , which says that all of the variables involved in
the constraint must have different values.
Examples:
✓ Sudoku problems: All variables in each row or column or 3 ˟ 3 matrix must satisfy an Alldiff
constraint.)
• The constraints we have described so far have all been absolute constraints, violation of which
rules out a potential solution.
• Many real-world CSPs include preference constraints indicating which solutions are preferred.
Real-World CSPs (contd…)
• Example: In a university class-scheduling problem, there are absolute constraints that no
professor can teach two classes at the same time. But we also may allow preference constraints:
Prof. R might prefer teaching in the morning, whereas Prof. N prefers teaching in the afternoon.
A schedule that has Prof. R teaching at 2 p.m. would still be an allowable solution but would not
be an optimal one.
• With this formulation, CSPs with preferences can be solved with optimization search methods,
either path-based or local. We call such a problem a constraint optimization problem, or COP.
Real-World CSPs (contd…)
• In CSPs there is a choice: an algorithm can search (choose a new variable assignment from
several possibilities) or do a specific type of inference called constraint propagation (use the
constraints to reduce the number of legal values for a variable, which in turn can reduce the
legal values for another variable, and so on.)
• Depth-first search for CSPs with single-variable assignments is called backtracking search
• The one that rules out the fewest values in the remaining variables
1) Node Consistency: A single variable (corresponding to a node in the CSP network) is node-
consistent if all the values in the variable’s domain satisfy the variable’s unary constraints. For
example, in the variant of the Australia map-coloring problem where South Australians dislike green, the
variable SA starts with domain {red, green, blue}, and we can make it node consistent by eliminating
green, leaving SA with the reduced domain {red , blue}.
•We say that a network is node-consistent if every variable in the network is node-consistent.
•It is always possible to eliminate all the unary constraints in a CSP by running node consistency. It is
also possible to transform all n-ary constraints into binary ones. (Ex.6.6)
Local Consistency
• 2) Arc Consistency: A variable in a CSP is arc-consistent if every value in its domain
satisfies the variable’s binary constraints. More formally, Xi is arc-consistent with respect to
another variable Xj if for every value in the current domain Di there is some value in the
domain Dj that satisfies the binary constraint on the arc (Xi,Xj). A network is arc-consistent
if every variable is arc consistent with every other variable.
• For example, consider the constraint Y = 𝑋^2 where the domain of both X and Y is the set of
digits. We can write this constraint explicitly as (X, Y ), {(0, 0), (1, 1), (2, 4), (3, 9))} .
• To make X arc-consistent with respect to Y , we reduce X’s domain to {0, 1, 2, 3}. If we also
make Y arc-consistent with respect to X, then Y ’s domain becomes {0, 1, 4, 9} and the
whole CSP is arc-consistent.
Local Consistency
• Arc Consistency:
Simplest form of propagation makes each arc consistent X →Y is consistent iff
for every value x of X there is some allowed value y of Y
Local Consistency
• Arc Consistency:
• Simplest form of propagation makes each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed value y of Y
If X loses a value, neighbors of X need to be rechecked If X loses a value, neighbors of X need to be rechecked
Arc consistency detects failure earlier than forward
checking
Can be run as a preprocessor or after each assignment
Local Consistency
• Arc Consistency Algorithm:
Actions:
move queen in column
Goal test:
no attacks
Evaluation:
h(n) = number of attacks