Ass 3
Ass 3
Submitted by
Hafiz Muavia
BCS21001
Submitted to
Sir Sohaib Bin Khalid Alvi
1. Initialization:
o Start with a randomly generated population of potential solutions, often
represented as chromosomes (strings of data).
2. Fitness Evaluation:
o Evaluate the fitness of each individual in the population. The fitness function
measures how well a solution solves the problem.
3. Selection:
o Select individuals for reproduction based on their fitness. Higher-fitness
individuals have a greater chance of being selected. Common methods include:
▪ Roulette Wheel Selection
▪ Tournament Selection
▪ Rank-Based Selection
4. Crossover (Recombination):
o Combine two parent solutions to produce offspring. This step simulates
reproduction and introduces diversity. Common crossover methods include:
▪ Single-point crossover
▪ Multi-point crossover
▪ Uniform crossover
5. Mutation:
o Apply small random changes to individuals to maintain diversity and explore new
areas of the search space. For example, flipping a bit in a binary chromosome.
6. Replacement:
o Form a new population by replacing some or all individuals from the previous
generation with offspring. Strategies include generational replacement or elitism
(keeping the best individuals).
7. Termination:
o Repeat the process until a stopping condition is met, such as a maximum number
of generations or a satisfactory fitness level.
1. Optimization:
o Traveling Salesman Problem (TSP)
o Scheduling and timetabling
o Parameter tuning in machine learning models
2. Artificial Intelligence:
o Evolving game strategies
o Designing neural networks
3. Engineering Design:
o Structural optimization
o Circuit design
4. Bioinformatics:
o DNA sequence alignment
o Protein structure prediction
5. Finance:
o Portfolio optimization
o Risk management
6. Creative Fields:
o Generative art
o Music composition
Advantages
Limitations
1. Initialization:
o Generate an initial population of size NNN where each individual is a potential
solution.
o Represent each individual as a chromosome (e.g., binary string, real number
vector).
2. Fitness Evaluation:
o For each individual in the population:
▪ Evaluate its fitness using the problem-specific fitness function.
3. Repeat until the stopping criterion is met (e.g., maximum generations or satisfactory
fitness):
1. Selection:
▪ Select PPP pairs of parent individuals based on their fitness (e.g., using
roulette wheel, tournament, or rank selection).
2. Crossover:
▪ For each selected pair of parents:
▪ Perform crossover with a probability pcp_cpc (crossover rate).
▪ Combine genetic information to produce one or more offspring
(e.g., single-point, two-point, or uniform crossover).
3. Mutation:
▪ For each offspring:
▪ Mutate genes with a probability pmp_mpm (mutation rate).
▪ Make small, random modifications to ensure genetic diversity.
4. Fitness Evaluation for Offspring:
▪ Evaluate the fitness of the newly created offspring.
5. Replacement:
▪ Form a new population by combining:
▪ Offspring from the current generation.
▪ Possibly the fittest individuals from the previous generation
(elitism).
4. Return the best individual:
o After the loop ends, return the individual with the highest fitness as the solution.
Pseudocode
plaintext
Copy code
1. Initialize population P with random individuals
2. Evaluate fitness of each individual in P
Parameter Considerations
Python Code
python
Copy code
import random
# Genetic Algorithm
def genetic_algorithm(pop_size, chromosome_length, generations, lower_bound,
upper_bound, crossover_rate, mutation_rate):
# Initialize population
population = initialize_population(pop_size, chromosome_length)
# Crossover
offspring1, offspring2 = crossover(parent1, parent2,
crossover_rate)
# Mutation
offspring1 = mutate(offspring1, mutation_rate)
offspring2 = mutate(offspring2, mutation_rate)
population = new_population
# Parameters
POP_SIZE = 10
CHROMOSOME_LENGTH = 8 # Binary representation length
GENERATIONS = 20
LOWER_BOUND = 0
UPPER_BOUND = 10
CROSSOVER_RATE = 0.8
MUTATION_RATE = 0.1
How It Works
1. Chromosomes:
oEach chromosome is a binary string, decoded to represent a number within the
range [0, 10].
2. Fitness Function:
o f(x)=x2f(x) = x^2f(x)=x2 is used to evaluate each solution.
3. Selection:
o Roulette wheel selection favors fitter individuals.
4. Crossover:
o Combines genetic information from two parents.
5. Mutation:
o Randomly flips bits to introduce diversity.
Output
The algorithm prints the best solution and its fitness for each generation, and finally returns the
optimal solution.
1. Initialization
2. Fitness Function
3. Decoding Chromosome
• Binary chromosomes are mapped to real-world values within a specified range (e.g., [0, 10]).
o Example:
▪ Chromosome: 10101011 (binary)
▪ Decimal Value: 171171171
▪ Decoded Value: Maps 171171171 to the range [0,10][0, 10][0,10].
4. Selection
5. Crossover
6. Mutation
• Goal: Find xxx and yyy in the range [0, 10] that maximize the function.
Changes in Code:
1. Fitness Function:
python
Copy code
def fitness_function(x, y):
return x**2 + y**2
2. Chromosome Decoding:
o Split a 16-bit chromosome into two 8-bit parts: xxx and yyy.
python
Copy code
def decode_chromosome(chromosome, lower_bound, upper_bound):
midpoint = len(chromosome) // 2
x_bits = chromosome[:midpoint]
y_bits = chromosome[midpoint:]
3. Fitness Evaluation:
python
Copy code
fitness_values = [
fitness_function(*decode_chromosome(ind, LOWER_BOUND, UPPER_BOUND))
for ind in population
]
python
Copy code
best_chromosome = population[fitness_values.index(max(fitness_values))]
best_x, best_y = decode_chromosome(best_chromosome, LOWER_BOUND,
UPPER_BOUND)
print(f"Generation {generation + 1}: Best Solution = (x={best_x},
y={best_y}), Fitness = {max(fitness_values)}")