0% found this document useful (0 votes)
15 views9 pages

Ass 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Ass 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Information Technology

University of the Punjab


Gujranwala Campus
--------------------------------------------

Parallel & Distributed Computing


Genetic Algorithm
------------------------------------------------

Submitted by
Hafiz Muavia
BCS21001

Submitted to
Sir Sohaib Bin Khalid Alvi

Dated: Nov 20, 2024.


Genetic Algorithm
A Genetic Algorithm (GA) is a type of optimization algorithm inspired by the principles of
natural selection and genetics. It is commonly used for solving complex optimization problems
where traditional methods are less effective or infeasible. GAs belong to the family of
evolutionary algorithms and are particularly suited for problems where the search space is large,
nonlinear, or poorly understood.

How Genetic Algorithms Work

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.

Applications of Genetic Algorithms

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

• Effective for complex and multidimensional search spaces.


• Works well without requiring gradient information.
• Can find globally optimal solutions in some cases.

Limitations

• Computationally expensive for large populations or high-dimensional problems.


• May converge prematurely to suboptimal solutions (local optima).
• Requires careful tuning of parameters like population size, mutation rate, and crossover
rate.

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

3. Repeat until termination criterion is met:


a. Select parent pairs from P based on fitness
b. Perform crossover to create offspring
c. Mutate offspring
d. Evaluate fitness of offspring
e. Replace individuals in P with offspring (and elites, if using
elitism)

4. Output the best individual as the optimal solution

Parameter Considerations

1. Population Size (NNN):


o Larger populations improve diversity but require more computation.
2. Crossover Rate (pcp_cpc):
o Typically between 0.6 and 0.9.
3. Mutation Rate (pmp_mpm):
o Usually small, around 0.01 to 0.1.
4. Stopping Criteria:
o Maximum number of generations.
o Minimum fitness threshold.

Problem: Maximize f(x)=x2f(x) = x^2f(x)=x2


• The goal is to find the value of xxx (within a range) that maximizes f(x)f(x)f(x).
• Chromosomes are represented as binary strings.

Python Code
python
Copy code
import random

# Fitness function: f(x) = x^2


def fitness_function(x):
return x ** 2

# Decode a binary chromosome to an integer


def decode_chromosome(chromosome, lower_bound, upper_bound):
decimal_value = int("".join(map(str, chromosome)), 2)
max_value = 2 ** len(chromosome) - 1
return lower_bound + (decimal_value / max_value) * (upper_bound -
lower_bound)

# Generate a random population


def initialize_population(pop_size, chromosome_length):
return [[random.randint(0, 1) for _ in range(chromosome_length)] for _ in
range(pop_size)]

# Evaluate fitness for the entire population


def evaluate_population(population, lower_bound, upper_bound):
return [fitness_function(decode_chromosome(ind, lower_bound,
upper_bound)) for ind in population]

# Selection: Roulette Wheel


def select_parents(population, fitness_values):
total_fitness = sum(fitness_values)
probabilities = [f / total_fitness for f in fitness_values]
return random.choices(population, weights=probabilities, k=2)

# Crossover: Single-point crossover


def crossover(parent1, parent2, crossover_rate):
if random.random() < crossover_rate:
point = random.randint(1, len(parent1) - 1)
return parent1[:point] + parent2[point:], parent2[:point] +
parent1[point:]
return parent1, parent2

# Mutation: Flip a random bit


def mutate(chromosome, mutation_rate):
return [1 - bit if random.random() < mutation_rate else bit for bit in
chromosome]

# 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)

for generation in range(generations):


# Evaluate fitness
fitness_values = evaluate_population(population, lower_bound,
upper_bound)

# Create next generation


new_population = []
for _ in range(pop_size // 2):
# Select parents
parent1, parent2 = select_parents(population, fitness_values)

# Crossover
offspring1, offspring2 = crossover(parent1, parent2,
crossover_rate)

# Mutation
offspring1 = mutate(offspring1, mutation_rate)
offspring2 = mutate(offspring2, mutation_rate)

# Add offspring to the new population


new_population.extend([offspring1, offspring2])

population = new_population

# Track the best solution


best_chromosome =
population[fitness_values.index(max(fitness_values))]
best_solution = decode_chromosome(best_chromosome, lower_bound,
upper_bound)
print(f"Generation {generation + 1}: Best Solution = {best_solution},
Fitness = {max(fitness_values)}")

# Return the best solution


return best_solution

# 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

# Run Genetic Algorithm


best_solution = genetic_algorithm(POP_SIZE, CHROMOSOME_LENGTH, GENERATIONS,
LOWER_BOUND, UPPER_BOUND, CROSSOVER_RATE, MUTATION_RATE)
print(f"\nOptimal Solution: {best_solution}")

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.

Explanation of the Code

1. Initialization

• Population: A group of potential solutions (chromosomes) is initialized randomly. Each


chromosome is a binary string.
• Why binary strings?
o Binary chromosomes are easy to work with and can represent solutions efficiently.

2. Fitness Function

• Purpose: The fitness function evaluates how good a solution is.


o For this example: f(x)=x2f(x) = x^2f(x)=x2. A higher value of f(x)f(x)f(x) means a better
solution.
• Adaptation: Change the fitness function to match your problem. For instance, maximize profit,
minimize cost, etc.

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

• Uses Roulette Wheel Selection:


o Individuals with higher fitness have a greater chance of being selected as parents.

5. Crossover

• Combines genetic material from two parents to produce offspring.


• In single-point crossover, a random point is chosen in the chromosome, and the genes are
swapped beyond that point.

6. Mutation

• Introduces randomness by flipping bits in a chromosome. This helps avoid premature


convergence to suboptimal solutions.

Adapting to Your Problem

Step 1: Define Your Fitness Function

• Replace the fitness_function definition with your specific goal.


o Example: For optimizing resources, fitness might be the profit gained from a solution.

Step 2: Adjust Parameters

• Population Size: Increase if the search space is large.


• Chromosome Length: Use longer binary strings for higher precision.
• Generations: More generations allow better solutions to emerge.
• Mutation Rate: Keep it low (e.g., 0.01) to avoid too much randomness.

Step 3: Modify Decoding Logic

• If you need multiple parameters (e.g., optimizing xxx and yyy):


o Split the chromosome into parts, each representing a parameter.

Example Problem: Optimize f(x,y)=x2+y2f(x, y) = x^2 + y^2f(x,y)=x2+y2

• 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:]

x = decode_chromosome_part(x_bits, lower_bound, upper_bound)


y = decode_chromosome_part(y_bits, lower_bound, upper_bound)
return x, y

def decode_chromosome_part(bits, lower_bound, upper_bound):


decimal_value = int("".join(map(str, bits)), 2)
max_value = 2 ** len(bits) - 1
return lower_bound + (decimal_value / max_value) * (upper_bound -
lower_bound)

3. Fitness Evaluation:

python
Copy code
fitness_values = [
fitness_function(*decode_chromosome(ind, LOWER_BOUND, UPPER_BOUND))
for ind in population
]

4. Print Best Solution:

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)}")

You might also like