AI Quiz 1 Notes
AI Quiz 1 Notes
21F3001823
AI: Search Methods for Problem-Solving October 14, 2024
• Intelligent Agents: An entity that is persistent(it’s there all the time), autonomous, proactive(decide
what goals to achieve next) and goal directed(once it has goals, it will follow them). Human beings are also
agents.
• An intelligent agent in a world carries a model of the world in its ”head”. the model may be an abstraction.
A self-aware agent would model itself in the world model.
• Signal→Symbol→Signal
• Sense(Signal Processing) → Deliberate(Neuro fuzzy reasoning + Symbolic Reasoning) → Act(Output
Signal)
• Intelligence: Remember the past and learn from it. Memory and experience(case based reasoning),
Learn a model(Machine learning), Recognize objects, faces, patterns(deep neural networks).
Understand the present. Be aware of the world around you. Create a model of the world(knowledge
representation), Make inferences from what you know(logic and reasoning).
Imagine the future. Work towards your goals. Trial and error(heuristic search), Goals, plans, and
actions(automated planning).
• Biosemiotics: How complex behaviour emerges when simple systems interact with each other through
signs.
• Reasoning: The manipulation of symbols in a meaningful manner.
1
1.3 Problem-Solving
• An autonomous agent in some world has a goal to achieve and a set of actions to choose from to strive for
the goal.
• We deal with simple problems first, i.e., the world is static, the world is completely known, only one agent
changes the world, action never fail, representation of the world is taken care of.
2 Search Methods
2.1 State Space Search
• We start with the map coloring problem, where we want to color each region in the map with an allowed
color such that no two adjacent regions have the same color.
2
Algorithm 1 GoalTest for the Water Jug Problem
Require: [a, b, c], the state which needs to be checked
1: if a = 4 or b = 4 or c = 4 then
2: return True
3: end if
4: return False
• A few other examples are The Eight Puzzle, The Man, Goat, Lion, Cabbage and The N-Queens
Problem.
Before studying different algorithms, we need to familiarize ourselves with some pseudocode syntax.
3
AI SMPS 2022: Lists and Tuples
Version 0.3 (prepared by S. Baskaran)
This is a quick reference for List and Tuple operations used in algorithms
discussed in this course. In the assignments and final exam, answers to
short-answer-type questions depend on the sequence in which values are
added, read and removed from lists and tuples. Therefore, it is important
to understand the representation and operations on lists and tuples.
LIST OPERATIONS
[] an empty list
3 2 1 [] a three element list
[3, 2, 1] shorthand notation
[] is empty = true
[1] is empty = false
[1] = 1 []
1 = head [1] = head 1 []
[] = tail [1] = tail 1 []
a ← head [3, 2, 1] a ← 3;
b ← tail [3, 2, 1] b ← [2, 1];
a b c ← [3, 2, 1] a ← 3; b ← 2; c ← [1];
a b c ← 3 2 1 [] a ← 3; b ← 2; c ← 1 [];
a c ← [3, 2, 1] a ← 3; c ← [1];
a c ← 3 2 1 [] a ← 3; c ← 1 [];
TUPLE OPERATIONS
This algorithm may run into an infinite loop, to address it we have Simple Search 2.
We can modify this a bit further by doing, OPEN ← OPEN ∪ {MoveGen(N) - CLOSED - OPEN}, this
lowers the state space even more.
• NodePairs: Keep track of parent nodes. Each node is a pair (currentN ode, parentN ode).
• Depth First Search: OPEN is a stack data structure
8
Algorithm 4 Depth First Search
1: OPEN ← (S, null) : [ ]
2: CLOSED ← [ ]
3: while OPEN is not empty do
4: nodePair ← head OPEN
5: (N, ) ← nodePair
6: if GoalTest(N) = True then
7: return ReconstructPath(nodePair, CLOSED)
8: end if
9: CLOSED ← nodePair : CLOSED
10: children ← MoveGen(N)
11: newNodes ← RemoveSeen(children, OPEN, CLOSED)
12: newPairs ← MakePairs(newNodes, N)
13: OPEN ← newPairs ++ tail OPEN
14: end while
15: return [ ]
9
Algorithm 5 Depth First Search continued
• BFS always generates the shortest path, whereas DFS may not generate the shortest path.
• Time Complexity: Assume constant branching factor b and assume the goal occurs somewhere at depth
d. In the best case the goal would be the first node scanned at depth d and in the worst case the goal would
b3 the last node scanned.
d
−1
Best Case: NDF S = d + 1 and NBF S = bb−1 +1
bd+1 −1
Worst Case: NDF S = NBF S = b−1
d
bd
Average: NDF S ≈ 2 and NBF S ≈ b2(b−1)
(b+1)
, b+1
NBF S ≈ NDF S ( b−1 )
• Depth Bounded DFS: Do DFS with a depth bound d. It is not complete and does not guarantee the
shortest path. Given below is a modified version that returns the count of nodes visited, to get the original
version just remove count.
10
Algorithm 7 Depth Bounded DFS
1: count ← 0
2: OPEN ← (S, null, 0) : [ ]
3: CLOSED ← [ ]
4: while OPEN is not empty do
5: nodePair ← head OPEN
6: (N, , depth) ← nodePair
7: if GoalTest(N) = True then
8: return count, ReconstructPath(nodePair, CLOSED)
9: end if
10: CLOSED ← nodePair : CLOSED
11: if depth < depthBound then
12: children ← MoveGen(N)
13: newNodes ← RemoveSeen(children, OPEN, CLOSED)
14: newPairs ← MakePairs(newNodes, N, depth + 1)
15: OPEN ← newPairs ++ tail OPEN
16: count ← count + length newPairs
17: else
18: OPEN ← tail OPEN
19: end if
20: end while
21: return count, [ ]
• Depth First Iterative Deepening: Iteratively increase depth bound. Combines best of BFS and DFS.
This will give the shortest path, but we have to be careful that we take correct nodes from CLOSED.
b
NDF ID ≈ NBF D b−1 for large b.
• DFID-C as compared to regular DFID-N adds new neighbors to OPEN list and also adds neighbors that
are in CLOSED but not in OPEN list. This allows DFID-C to get the shortest path whereas DFID-N may
not get the shortest path.
• The monster that AI fights is Combinatorial Explosion.
• All these algorithms we studied are blind algorithms or uniformed search, they don’t know where the goal is.
11
Algorithm 9 Best First Search
1: OPEN ← (S, null, h(S)) : [ ]
2: CLOSED ← [ ]
3: while OPEN is not empty do
4: nodePair ← head OPEN
5: (N, , ) ← nodePair
6: if GoalTest(N) = True then
7: return ReconstructPath(nodePair, CLOSED)
8: end if
9: CLOSED ← nodePair : CLOSED
10: children ← MoveGen(N)
11: newNodes ← RemoveSeen(children, OPEN, CLOSED)
12: newPairs ← MakePairs(newNodes, N)
13: ▷ Again, this is pretty much the most important line
14: OPEN ← sorth (newPairs ++ tail OPEN)
15: end while
16: return [ ]
• For The eight puzzle we can define a few heuristic functions as follows:
h1 (n) = number
P of tiles out of place, Hamming distance.
h2 (n) = for each tile Manhattan distance to its destination.
• Hill Climbing: A local search algorithm, i.e., move to the best neighbor if it is better, else terminate. In
practice sorting is not needed, only the best node. This has burnt its bridges by not storing OPEN. We are
interested in this because it is a constant space algorithm, which is a vast improvement on the exponential
space for BFS and DFS. It’s time complexity is linear. It treats the problem as an optimization problem.
• Synthesis Methods: Constructive methods. Starting with the initial state and build the solution state
piece by piece.
• Perturbation Methods: Permutation of all possible candidate solutions.
• SAT problem: Given a boolean formula made up of a set of propositional variables V = {a, b, c, d, e, ...}
each of which can be true or f alse, or 1 or 0, to find an assignment of variables such that the given formula
evaluates to true or 1.
A SAT problem with N variables has 2N candidates.
• Travelling Salesman Problem: Given a set of cities and given a distance measure between every pair of
cities, the task is to find a Hamiltonian cycle, visiting each city exactly once, having the least cost.
1. Nearest Neighbor Heuristic: Start at some city, move to nearest neighbor as long as it does not
close the loop prematurely.
2. Greedy Heuristic: Sort the edges, then add the shortest available edge to the tour as long as it does
not close the loop prematurely.
3. Savings Heuristic: Start with n − 1 tours of length 2 anchored on a base vertex and performs n − 2
merge operations to construct the tour.
4. Perturbation operators: Start with some tour, then choose two cities and interchange, this gives
the solution space. Heuristic function is saving = cost(base, a) + cost(base, b) - cost(a, b)
12
5. Edge Exchange: Similar to above but now instead of choosing cities we are choosing edges. Can be
2-edge exchange, 3-edge, 4-edge, etc. City exchange is a special case of 4-edge exchange.
• Solution space of TSP grows in order factorial, much faster than SAT problem.
• A collection of problems with some solutions is available here.
• To escape local minima we need something called exploration.
• Beam Search: Look at more than one option at each level. For a beam width b, look at best b options.
Heavily memory dependent.
• Variable Neighborhood Descent: Essentially we are doing hill climbing with different neighborhood
functions. The idea is to use sparse functions before using denser functions, so the storage is not high. The
algorithm assumes that there are N moveGen functions sorted according to the density of the neighborhoods
produced.
• Best Neighbor: Another variation of Hill Climbing, simply move to the best neighbor regardless of
whether it is better than current node or not. This will require an external criterion to terminate. It will
not escape local maxima, as once it escapes it will go right back to it in the next iteration.
• Tabu Search: Similar to Best Neighbor but not allowed back immediately. An aspiration criterion can be
added that says that if a tabu move result in a node that is better than bestSeen then it is allowed. To
drive this search into newer areas, keep a frequency array and give preference to lower frequency bits or
bits that have been changed less.
13
Algorithm 14 Tabu Search
TabuSearch( )
1: N ← start
2: bestSeen ← N
3: while some termination criterion do
4: N ← best(allowed(moveGen(N )))
5: if N better than bestSeen then
6: bestSeen ← N
7: end if
8: end while
9: return bestSeen
3 Stochastic Search
Very often to escape local minima we use Stochastic/Randomized methods instead of Deterministic methods.
• Stochastic Hill Climbing: Let the algorithm be at the current bode vc , then we select a random neighbor
vN and calculate ∆E = (eval(vN ) − eval(vc )).
If ∆E is positive, it means that vN is better than vc , then the algorithm should move to it with a higher
probability.
If ∆E is negative, it means that vN is better than vc , then the algorithm should move to it with a lower
14
probability.
The probability is computed using the sigmoid function, which is given as
1
Probability P = ∆E
1 + e− T
• Annealing: In metallurgy and materials science, annealing is a heat treatment that alters the physical
and sometimes chemical properties of a material to increase its ductility and reduce its hardness, making
it more workable. It involves heating a material above its recrystallization temperature, maintaining a
suitable temperature for an appropriate amount of time and then cooling.
In annealing, atoms migrate in the crystal lattice and the number of dislocations decreases, leading to a
change in ductility and hardness. As the material cools it recrystallizes.
• Simulated Annealing: Essentially tries to mimic the annealing process, begins with exploration and
ends with exploitation. Annealing is also where ∆E and T come from, essentially meaning Energy and
Temperature.
• Artificial Selection: Given a population of candidate solutions we do a three step iterative process
1. Reproduction: Clone each candidate in proportion to its fitness. Some may get more than one copy,
some none.
2. Crossover: Randomly mate the resulting population and mix up the genes.
3. Mutation: Once in a while, in search of a missing gene.
• Selection: Imagine a roulette wheel on which each candidate in the parent population {P1 , P2 , ..., PN } is
represented as a sector. The angle subtended by each candidate is proportional to its fitness. The roulette
wheel is spun N times. Then the selected parents are added one by one to create a new population.
• Crossover Operators: A single point crossover simply cuts the two parents at a randomly chosen point
and recombines them to form two new solution strings. It can be at multiple points as well, idea is to mix
up the genes.
15
Algorithm 18 Genetic Algorithm
Genetic-Algorithm( )
1: P ← create N candidate solutions ▷ initial population
2: repeat
3: compute fitness value for each member of P
4: S ← with probability proportional to fitness value, randomly select N member from P .
5: of f spring ← partition S into two halves, and randomly mate and crossover members to generate N
offsprings.
6: With a low probability mutate some offsprings
7: Replace k the weakest members of P with k strongest offsprings
8: until some termination criteria
9: return the best member of P
• A large diverse population size is necessary for the performance of genetic algorithm to work.
• One issue is how to represent candidate solutions?, as they can be of any type. One simple solution is to
convert the solution into a string this will require an additional function.
• In TSP, one problem is also that during crossover some cities can get repeated.
• Cycle Crossover: Identify cycles, look at below image, easier to understand.
Once cycles are identified, then C1 gets odd numbered cycles from P1 and even numbered cycles from P2
the other go to C2 .
• Partially Mapped Crossover(PMX): Identify some cities that form a subtour and establish a mapping.
• Order Crossover: Copy a subtour from P1 into C1 and the remaining from P2 in the order they occur in
P2 .
• For all the above we were using path representation of TSP, there is another representation of TSP.
• Adjacency Representation: The cities are arranged based on where they come from in the tour with
respect to the index. For example if A → H, then at index 0 we would have H.
• Alternating Edges Crossover: From a given city X choose the next city Y from P1 and from the city Y
choose the next city from P2 and so on. It is possible to run into cycles, need to be careful.
• Heuristic Crossover: For each city choose the next from that parent P1 or P2 whichever is closer.
16
• Ordinal Representation: Replace the name of the city is Path Representation one by one to its current
numeric index. At the start all cities A, B, C, D, E will have numeric index 1, 2, 3, 4, 5, now consider if
we add C to the representation the new indexing would be for cities A, B, D, E, we have 1, 2, 3, 4. The
advantage is that single point crossover produces valid offspring.
• Let 5 ants A, B, C, D, and E go out in search for food. Each ant lays a trail of pheromone where it goes.
Each ant lays a trail of pheromone where it goes. More ants that emerge will tend to follow some pheromone
trail.
• Let’s say A finds some food, then it will follow its pheromone trail back and the other ants will continue
their search.
• Eventually, as more ants travel on the trail they deposit more pheromone and the trail gets stronger and
stronger, eventually becoming the caravan we might have seen raiding our food.
• They tend to find the shortest path.
• Ant Colony Optimization: We try to capitalize on this method. Each ant constructs a solution using a
stochastic greedy method using a combination of a heuristic function and pheromone trail following.
• This is related to the class of algorithms known as Swarm Optimization.
17
• From a city i the k th ant moves to city j with a probability given by
[τij (t)]α ×[ηij ]β
k
P
([τih (t)]α [ηih (t)]β )
, if j ∈ allowedk (t) the cities ant k is allowed to move to
Pij (t) = h∈allowed k (t)
0, otherwise
where τij (t) is pheromone on edgeij and ηij is called visibility which is inversely proportional to the distance
between cities i and j.
• After constructing a tour in n time steps, each ant k deposits an amount of pheromone Q
Lk on the edges it
has traversed, which is inversely proportional to the cost of the tour Lk it found.
• Total pheromone deposited on edgeij is ∆τij (t, t + n)
• The total pheromone on edgeij is updated as
18
• Dijkstra’s Algorithm
1. Begins by assigning infinite cost estimates to all nodes except the start node.
2. It assigns color white to all the nodes initially.
3. It picks the cheapest white node and colors it black.
4. Relaxation: Inspect all neighbors of the new black node and check if a cheaper path has been found
to them.
5. If yes, then update cost of that node, and mark the parent node.
4.2 Algorithm A*
• A* achieves better performance by using heuristics to guide its search.
• For all nodes we will compute f (n) which is made up of two components g(n) and h(n).
• g(n) = actual cost of solution found from the start node to node n.
• h(n) = estimated cost of the path from node n to goal node.
• Maintain a priority queue of nodes sorted on the f values
• Pick the lowest f value node, and check whether it is the goal node.
• Else generate its neighbors, and compute their f values
• Insert new nodes into the priority queue
Algorithm 20 A* Algorithm
A*(S)
1: default value of g for every node is +∞
2: parent(S) ← null
3: g(S) ← 0
4: f (S) ← g(S) + h(S)
5: OP EN ← S :[ ]
6: CLOSED ← empty list
7: while OP EN is not empty do
8: N ← remove node with the lowest f value from OP EN
9: add N to CLOSED
10: if GoalTest(N ) then
11: return ReconstructPath(N )
12: end if
13: for each neighbor M ∈ MoveGen(N ) do
14: if g(N ) + k(N, M ) < g(M ) then
15: parent(M ) ← N
16: g(M ) ← g(N ) + k(N, M )
17: f (M ) ← g(M ) + h(M )
18: if M ∈ OP EN then
19: continue
20: end if
21: if M ∈ CLOSED then
22: Propagate-Improvement(M )
23: else
24: add M to OP EN
25: end if
26: end if
27: end for
28: end whilereturn empty list
19
1. The branching factor is finite, otherwise you cannot even generate the neighbors.
2. Every edge has a cost greater than a small constant ϵ, however it is possible to get stuck in an infinite
path with a finite cost.
3. For all nodes h(n) ≤ h ∗ (n)
• If a path exists to the goal node, then the OPEN list always contains a node n′ from an optimal path.
Moreover, the f value of that node is not greater than the optimal value.
20