ML U4 Notes
ML U4 Notes
MDS (Multi-Dimensional Scaling) is a dimensionality reduction algorithm. Its primary goal is to transform
high-dimensional data into a lower-dimensional space (like 2D or 3D) while preserving the distances (or
similarities) between the points. It's often used for visualization or data exploration.
Explanation of Elitism, Tournaments, and Niching in Genetic Algorithms
Genetic Algorithms (GAs) are optimization techniques inspired by natural selection. These three
concepts play critical roles in maintaining diversity, selecting the best solutions, and avoiding premature
convergence.
1. Elitism
• Definition: Elitism is a strategy in Genetic Algorithms where the best individuals (solutions) from
the current generation are preserved and directly passed to the next generation without
modification.
• Purpose:
o Ensures that the best solutions are not lost during the evolution process.
o Helps maintain the quality of the population.
• Definition: A selection method where a subset of individuals is randomly chosen from the
population, and the best individual among this subset (based on fitness) is selected as a parent
for the next generation.
• Purpose:
o Balances between exploitation (selecting the best) and exploration (allowing weaker
individuals to occasionally reproduce).
3. Niching
• Purpose:
A Genetic Algorithm (GA) can be applied to solve this problem by treating it as an optimization problem.
• Example:
o For a map with 4 regions (A, B, C, D) and 3 possible colors, a chromosome could look
like: [1,2,1,3][1, 2, 1, 3]
2. Fitness Function
o Count the number of violations (i.e., pairs of adjacent regions with the same color).
• Example:
3. Initialization
4. Selection
• Use a selection method (e.g., tournament selection) to choose parents based on fitness.
5. Crossover
• Example:
o Parent 1: [1,2,1,3][1, 2, 1, 3]
o Parent 2: [2,1,3,1][2, 1, 3, 1]
o Crossover Point: 2
o Offspring: [1,2,3,1][1, 2, 3, 1]
6. Mutation
• Example:
o Original: [1,2,3,1][1, 2, 3, 1]
o Mutated: [1,2,3,2][1, 2, 3, 2]
7. Termination
1. Input:
3. Evaluate Fitness:
4. Selection:
5. Crossover:
6. Mutation:
7. Repeat:
o Continue the process for a fixed number of generations or until an optimal solution is
found.
Example
Map:
Regions: A,B,C,DA, B, C, D
Adjacency:
Steps:
1. Initial Population:
o Individual 1: [1,2,1,3][1, 2, 1, 3]
o Individual 2: [2,1,3,2][2, 1, 3, 2]
2. Fitness Evaluation:
3. Selection:
4. Crossover:
5. Mutation:
Limitations
Real-World Applications
The Knapsack Problem is a classic optimization problem where you need to maximize the total value of
items that can be placed in a knapsack, without exceeding its weight capacity.
Problem Description
1. A set of nn items.
The goal is to find the subset of items that maximizes the total value while keeping the total weight
within WW.
The Genetic Algorithm (GA) can solve this problem by evolving a population of possible solutions over
generations. Below are the steps:
Steps of GA for the Knapsack Problem
• The chromosome is a binary string of length nn, where each gene corresponds to an item:
2. Initial Population
• Example:
o Chromosome 1: [1,0,1,1][1, 0, 1, 1]
o Chromosome 2: [0,1,0,1][0, 1, 0, 1]
3. Fitness Function
Fitness=Total Value of Selected Items (if total weight ≤W).\text{Fitness} = \text{Total Value of Selected
Items (if total weight \( \leq W\))}.
• If the total weight exceeds WW, penalize the chromosome by assigning a low fitness value.
• Example:
o Items:
▪ w=[2,3,4,5]w = [2, 3, 4, 5]
o Chromosome: [1,0,1,1][1, 0, 1, 1]
▪ Selected Items: 1, 3, 4
4. Selection
• Common methods:
5. Crossover
• Example:
o Parent 1: [1,0,1,0][1, 0, 1, 0]
o Parent 2: [0,1,0,1][0, 1, 0, 1]
o Crossover Point: 2
o Offspring:
▪ Child 1: [1,0,0,1][1, 0, 0, 1]
▪ Child 2: [0,1,1,0][0, 1, 1, 0]
6. Mutation
• Example:
o Original: [1,0,1,0][1, 0, 1, 0]
o Mutated: [1,1,1,0][1, 1, 1, 0]
7. Termination
Problem:
• Items:
• Population:
o Chromosome 1: [1,0,1,0][1, 0, 1, 0]
o Chromosome 2: [0,1,1,0][0, 1, 1, 0]
o Value: 10+30=4010 + 30 = 40
o Fitness = 40.
o Value: 20+30=5020 + 30 = 50
o Fitness = 50.
Step 3: Selection
Step 4: Crossover
• Parent 1: [1,0,1,0][1, 0, 1, 0]
• Parent 2: [0,1,1,0][0, 1, 1, 0]
• Offspring:
o Child 1: [1,0,1,0][1, 0, 1, 0]
o Child 2: [0,1,1,0][0, 1, 1, 0]
Step 5: Mutation
• Mutate Child 1:
o Original: [1,0,1,0][1, 0, 1, 0]
o Mutated: [1,1,1,0][1, 1, 1, 0]
o Value: 10+20+30=6010 + 20 + 30 = 60
o Fitness = Penalized.
o Value: 20+30=5020 + 30 = 50
o Fitness = 50.
Step 7: Repeat
Limitations
Real-World Applications
The 4-Peaks Problem is an optimization problem often used to demonstrate how Genetic Algorithms
(GAs) can solve complex problems involving a balance between exploration (searching new areas) and
exploitation (refining known good solutions).
Problem Description
The 4-Peaks Problem is based on finding the optimal arrangement of binary strings that maximize a
specific fitness function. A binary string is considered "fit" if it has a certain structure of consecutive 1's
and 0's in either the first half or the second half of the string.
Key Parameters:
Fitness Function:
f(x)={max(num_leading_1’s,num_trailing_0’s)+R(x)if min(num_leading_1’s,num_trailing_0’s)>T,0oth
erwise.f(x) = \begin{cases} \max(\text{num\_leading\_1's}, \text{num\_trailing\_0's}) + R(x) & \text{if }
\min(\text{num\_leading\_1's}, \text{num\_trailing\_0's}) > T, \\ 0 & \text{otherwise}. \end{cases}
Where:
Goal:
Maximize f(x)f(x).
• It has a deceptive fitness landscape: high local optima may mislead traditional optimization
algorithms.
• It requires balancing exploration (diverse solutions) and exploitation (refining good solutions).
GAs are well-suited because they use:
1. Chromosome Representation
2. Initial Population
3. Fitness Function
o If both counts exceed the threshold TT, assign a fitness with a bonus reward (R(x)R(x)).
4. Selection
o Tournament Selection: Select the fittest chromosomes from small random subsets.
5. Crossover
• Example:
o Parent 1: [1,1,1,0,0,0,1,1][1, 1, 1, 0, 0, 0, 1, 1]
o Parent 2: [0,0,1,1,1,0,0,0][0, 0, 1, 1, 1, 0, 0, 0]
o Crossover Point: 4
o Child 1: [1,1,1,0,1,0,0,0][1, 1, 1, 0, 1, 0, 0, 0]
o Child 2: [0,0,1,1,0,0,1,1][0, 0, 1, 1, 0, 0, 1, 1]
6. Mutation
• Example:
o Original: [1,1,1,0,0,0,1,1][1, 1, 1, 0, 0, 0, 1, 1]
o Mutated: [1,1,1,0,1,0,1,1][1, 1, 1, 0, 1, 0, 1, 1]
7. Termination
Example Walkthrough
Input:
• Threshold (TT) = 4
• Initial Population:
o Chromosome 1: [1,1,1,1,0,0,0,0,0,0][1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
o Chromosome 2: [1,1,1,0,0,0,0,0,1,1][1, 1, 1, 0, 0, 0, 0, 0, 1, 1]
o Chromosome 3: [0,0,0,0,1,1,1,1,1,1][0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
Fitness Evaluation:
Crossover:
• Child: [1,1,1,1,1,1,1,1,0,0][1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
Mutation:
• Result: [1,1,1,1,1,1,0,1,0,0][1, 1, 1, 1, 1, 1, 0, 1, 0, 0]
Applications of 4-Peaks Problem
2. Optimization Problems: The principles used to solve 4-Peaks can be applied to other challenging
optimization problems.
By using GAs, the 4-Peaks Problem demonstrates how a combination of selection, crossover, and
mutation can navigate complex fitness landscapes efficiently.
MLP
• Previously, we chose the structure in a completely ad hoc way by trying out different structures
and choosing the one that worked best
• We can use a GA for this problem, although the crossover operator doesn’t make a great deal of
sense, so we just consider mutation
• However, we allow for four different types of mutation:
o delete a neuron
o delete a weight connection
o add a neuron
o add a connection.