In this chapter, we will learn to implement the Python code for Genetic Algorithms. Genetic Algorithms are fast, easy to implement, and highly customizable. Genetic Algorithms are widely used in various fields, such as engineering, finance, artificial intelligence, and optimization problems where traditional methods might be impractical or computationally expensive. Their ability to explore vast solution spaces and find near-optimal solutions makes them valuable tools for tackling complex optimization challenges.

The algorithm starts with an initial population, and through a series of genetic operations, such as selection, crossover (recombination), and mutation, new generations are created. The fitter individuals, those with better solutions, have a higher chance of being selected to produce offspring, which inherit characteristics from their parents. As generations progress, the population tends to evolve towards better solutions as the fitter individuals dominate. This process continues until a termination criterion, such as reaching a maximum number of generations or achieving a satisfactory solution, is met.

Genetic Algorithm (GA) was proposed by John Holland in 1975. Since its origin, it has found many interesting applications in various branches of science and engineering. Here is the simple ready-to-implement Python code for Genetic Algorithms.

import random

# Genetic Algorithm parameters
POPULATION_SIZE = 100
GENERATION_COUNT = 50
CROSSOVER_RATE = 0.8
MUTATION_RATE = 0.1
CHROMOSOME_LENGTH = 4
LOWER_BOUND = -5.12
UPPER_BOUND = 5.12

# Generate a random chromosome
def generate_chromosome():
    return [random.randint(0, 1) for _ in range(CHROMOSOME_LENGTH)]

# Evaluate the fitness of an individual
def evaluate_fitness(chromosome):
    x = decode_chromosome(chromosome)
    #Here you can change the objective function value 
    fitness_value = sum([gene**2 for gene in x])
    return fitness_value

# Decode binary chromosome to real-valued representation
def decode_chromosome(chromosome):
    x = []
    for gene in chromosome:
        value = LOWER_BOUND + (UPPER_BOUND - LOWER_BOUND) * int("".join(map(str, chromosome)), 2) / (2 ** CHROMOSOME_LENGTH - 1)
        x.append(value)
    return x

# Perform selection using tournament selection
def selection(population):
    tournament_size = 5
    selected_parents = []
    for _ in range(len(population)):
        tournament = random.sample(population, tournament_size)
        winner = min(tournament, key=lambda x: x[1])
        selected_parents.append(winner[0])
    return selected_parents

# Perform crossover between two parents
def crossover(parent1, parent2):
    if random.random() < CROSSOVER_RATE:
        crossover_point = random.randint(1, CHROMOSOME_LENGTH - 1)
        child1 = parent1[:crossover_point] + parent2[crossover_point:]
        child2 = parent2[:crossover_point] + parent1[crossover_point:]
        return child1, child2
    else:
        return parent1, parent2

# Perform mutation on an individual
def mutate(individual):
    mutated_individual = individual.copy()
    for i in range(CHROMOSOME_LENGTH):
        if random.random() < MUTATION_RATE:
            mutated_individual[i] = 1 - mutated_individual[i]
    return mutated_individual

# Generate an initial population
population = [(generate_chromosome(), 0) for _ in range(POPULATION_SIZE)]

# Genetic Algorithm main loop
for _ in range(GENERATION_COUNT):
    # Evaluate fitness of each individual in the population
    population = [(chromosome, evaluate_fitness(chromosome)) for chromosome, _ in population]

    # Select parents for reproduction
    parents = selection(population)

    # Create offspring through crossover and mutation
    offspring = []
    for i in range(0, POPULATION_SIZE, 2):
        parent1 = parents[i]
        parent2 = parents[i + 1]
        child1, child2 = crossover(parent1, parent2)
        child1 = mutate(child1)
        child2 = mutate(child2)
        offspring.extend([child1, child2])

    # Replace the old population with the offspring
    population = [(chromosome, 0) for chromosome in offspring]

# Get the best individual as the solution
best_individual = min(population, key=lambda x: x[1])[0]
decoded_solution = decode_chromosome(best_individual)
fitness_value = evaluate_fitness(best_individual)
# Print the solution
print("Best Solution:", decoded_solution)
print("Best Fitness:", fitness_value)

In this Python code for Genetic Algorithms, you can implement the Genetic Algorithm for your specific requirements with minor modifications. If you’re interested in MATLAB implementation of Genetic Algorithm, please click here. Read all the comments carefully and let us know if you are stuck in any problems in the comment box below. Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *