Soft Computing — Lab 5
Objective 1 — Genetic Algorithm Basics
Objective
Implement a simple Genetic Algorithm (GA) covering selection, crossover, mutation, and
fitness evaluation to optimize a given objective. Students will compare at least two selection
methods and study the effect of mutation rates.
What to implement
1. Encoding: Binary chromosomes of length L.
2. Fitness & objective: Support maximization and minimization (you may convert
minimization to maximization, e.g., by using objective).
3. Selection (any two): Roulette wheel, Tournament (k=3), Rank (if chosen, include it
in arguments).
4. Crossover: Single-point and multi-point crossover (apply with probability
crossover_rate).
5. Mutation: Bit-flip (and optionally swap/inversion if you experiment with non-binary
encodings); apply with probability mutation_rate.
6. GA loop: Evaluate → select → crossover → mutate → form next generation (elitism:
keep best 1).
7. Termination: Stop after max_generations (and you may add “no improvement for K
generations”).
8. Reproducibility: Set and print a random seed.
Inputs — Input Format:
• Population Size: An integer representing the number of chromosomes in the population.
(population_size (int > 0))
• Chromosome Length: An integer representing the length of each chromosome.
(chromosome_length (int > 0))
• Selection Method: A string indicating the selection method to use ('roulette' or
'tournament'), Selection_method in {roulette, tournament, rank}
• Crossover Rate: A float representing the probability of crossover, (crossover_rate ∈ [0,1])
• Mutation Rate: A float representing the probability of mutation, mutation_rate ∈ [0,1]
• Objective Function: A function that takes a chromosome as input and returns its fitness
score. max_generations (int > 0), objective function (callable) and mode
{maximize|minimize}
Output Format:
• Print the initial population and their fitness scores.
• Display the selected parents, offspring after crossover, and the mutated offspring.
• Print the best chromosome found and its fitness score.
Examples for Test Cases:
1. Test Case 1:
• Population Size: 10
• Chromosome Length: 8
• Selection Method: 'roulette'
• Crossover Rate: 0.7
• Mutation Rate: 0.01
• Objective Function: Sum of bits (maximize the number of '1's in the chromosome)
Expected Output:
• Initial Population:
[01101001, 10110101, 11001011, 00011100, 11100010, 01010111, 10101001,
10011010, 01110001, 00100111]
• Fitness Scores:
[4, 5, 6, 3, 4, 5, 4, 4, 4, 5] (Number of '1's in each chromosome)
• Selected Parents:
[11001011, 10110101, 00100111, 01010111, 01101001, 10110101, 11100010,
11001011]
• Offspring After Crossover:
[11001001, 10100111, 00110111, 01010111, 11101011, 01110101, 11110010,
10101011]
• Offspring After Mutation:
[11001001, 10100111, 00110111, 01010111, 11101010, 01110101, 11110010,
10101011]
• Best Chromosome Found:
11110010 with fitness score 6 (Best chromosome after all iterations)
2. Test Case 2:
• Population Size: 6
• Chromosome Length: 5
• Selection Method: 'tournament'
• Crossover Rate: 0.8
• Mutation Rate: 0.05
• Objective Function: Minimize the binary number represented by the chromosome.
Expected Output:
• Initial Population:
[11101, 01010, 10101, 01110, 00001, 11011]
• Fitness Scores:
[29, 10, 21, 14, 1, 27] (Decimal values of binary chromosomes)
• Selected Parents:
[00001, 01010, 01110, 00001, 10101, 01010]
• Offspring After Crossover:
[01001, 01010, 01110, 00001, 10101, 00010]
• Offspring After Mutation:
[01001, 01010, 01110, 00001, 10101, 00011]
• Best Chromosome Found:
00001 with fitness score 1 (Best chromosome after all iterations)
Examples for Error Test Cases:
1. Error Case 1: Population Size: -5 (Invalid) → Error: Population size must be a
positive integer.
2. Error Case 2: Chromosome Length: 0 (Invalid) → Error: Chromosome length must
be greater than zero.
3. Error Case 3: Selection Method: 'random' (Invalid method) → Error: Selection
method must be either 'roulette' or 'tournament'.
4. Error Case 4: Mutation Rate: 1.5 (Invalid range) → Error: Mutation rate must be
between 0 and 1.
Objective 2 — Genetic Algorithm for Function
Optimization (Real-Valued)
Objective
Students will learn to apply Genetic Algorithms (GA) to solve optimization problems. The
exercise will involve implementing key components of GA, such as selection, crossover,
mutation, and fitness evaluation, to find the maximum value of a given continuous function
within a specified range.
Instructions
• Write a program in Python to solve the given problem using a Genetic Algorithm.
• Ensure your code includes error handling for edge cases, such as invalid parameter values,
out-of-bound solutions, or improper ranges.
• Test your program with the provided test cases, including error cases.
• Submit the code with comments explaining each step.
Question: Genetic Algorithm for Function Optimization — Problem Statement
Implement a Genetic Algorithm to find the maximum value of a given continuous function
within a specified range. The Genetic Algorithm should work with a population of real-valued
chromosomes (floating-point numbers), and it should use genetic operations such as selection,
crossover, and mutation to evolve the population over generations.
Steps
1. Chromosome Representation: Each chromosome will represent a potential solution
in the form of a floating-point number within the specified range.
2. Selection: Implement a selection method such as roulette wheel or tournament
selection to choose parent chromosomes based on their fitness.
3. Crossover: Perform arithmetic crossover (blend crossover) between pairs of parent
chromosomes.
4. Mutation: Apply a small random perturbation to the chromosome values with a
certain mutation rate.
5. Fitness Evaluation: The fitness of each chromosome will be the value of the function
at that chromosome's point.
6. Termination: The algorithm should terminate after a fixed number of generations or
when the population's fitness has converged.
Input Format
• Population Size: An integer representing the number of chromosomes in the population.
• Number of Generations: An integer representing the number of generations to run the
algorithm.
• Crossover Rate: A float representing the probability of crossover.
• Mutation Rate: A float representing the probability of mutation.
• Range: A tuple of two floats representing the lower and upper bounds of the search space.
• Objective Function: A continuous function to optimize (maximize).
Output Format
• Print the initial population and their fitness scores.
• Display the best chromosome and its fitness score for each generation.
• Print the best chromosome found and its corresponding function value at the end of the
algorithm.
Examples for Test Cases
1. Test Case 1:
• Population Size: 20
• Number of Generations: 50
• Crossover Rate: 0.8
• Mutation Rate: 0.05
• Range: (-10.0, 10.0)
• Objective Function: f(x)=x⋅sin(10⋅π⋅x) +1.0
Expected Output:
• Initial Population: A list of 20 floating-point numbers within the range (-10.0, 10.0).
• Fitness Scores: A list of fitness values corresponding to the initial population.
• Best Chromosome and Fitness for each Generation: E.g., Generation 1:
Chromosome = 2.34, Fitness = 1.75
• Best Chromosome Found: E.g., 3.14 with function value 4.0 at the end of the
algorithm.
2. Test Case 2:
• Population Size: 15
• Number of Generations: 100
• Crossover Rate: 0.9
• Mutation Rate: 0.01
• Range: (0.0, 2.0)
• Objective Function: f(x)=e-x⋅cos(2⋅π⋅x)
Expected Output:
• Initial Population: A list of 15 floating-point numbers within the range (0.0, 2.0).
• Fitness Scores: A list of fitness values corresponding to the initial population.
• Best Chromosome and Fitness for each Generation: E.g., Generation 1:
Chromosome = 0.87, Fitness = 0.67
• Best Chromosome Found: E.g., 0.79 with function value 0.8 at the end of the
algorithm.
Examples for Error Test Cases
1. Error Case 1: Range: (10.0, -10.0) (Invalid range where lower bound is greater than
upper bound) → Error: The range must have the lower bound less than the upper bound.
2. Error Case 2: Population Size: 0 (Invalid) → Error: Population size must be a positive
integer.
3. Error Case 3: Crossover Rate: 1.5 (Invalid range) → Error: Crossover rate must be
between 0 and 1.