Genetic Algorithm in
Python Programming
Genetic Algorithm
This code implements a simple genetic algorithm to evolve
a population of strings towards a target string. The target string is
specified as 'TARGET', and the genetic algorithm aims to
generate a population of strings that closely match the target
string through iterations of selection, crossover, mutation, and
replacement.
Initialization:
•POP_SIZE: Number of individuals in the population.
•MUT_RATE: Mutation rate, indicating the probability of a mutation occurring.
•TARGET: The target string that the algorithm aims to evolve towards.
•GENES: The set of characters that can be used to form individuals in the
population.
Initialization of Population:
initialize_pop(TARGET): Generates an initial population of
strings, each having the same length as the target string. The
characters of the strings are randomly chosen from the set of
genes.
Crossover
crossover(selected_chromo, CHROMO_LEN, population):
Performs crossover/mating between selected individuals from the
current population to create a new population
Mutation
mutate(offspring, MUT_RATE): Introduces random mutations in
the offspring with a probability specified by the mutation rate.
Selection
Selection(population, TARGET): Selects the best
individuals from the current population based on
their fitness.
Replacement
replace(new_gen, population): Replaces individuals
in the current population with new individuals from the
new generation if they are fitter.
Main Genetic Algorithm Loop:
main(POP_SIZE, MUT_RATE, TARGET, GENES): The main loop that
iterates through generations until the target string is found. It involves the
following steps:
• Initialize the population.
• Calculate the fitness of the initial population.
• Iterate until the target is found:
• Select the best individuals.
• Mate parents to create a new generation.
• Mutate the new generation.
• Replace the least fit individuals with the new generation.
• Check if the target is found and print information.
 The algorithm prints information about each generation, including the
string, generation number, and fitness of the fittest individual. The
process continues until the target string is found.
¼ sir?
YES ¼ POOOOOO 

Genetic Algorithm in Python Programming.pptx

  • 1.
  • 2.
    Genetic Algorithm This codeimplements a simple genetic algorithm to evolve a population of strings towards a target string. The target string is specified as 'TARGET', and the genetic algorithm aims to generate a population of strings that closely match the target string through iterations of selection, crossover, mutation, and replacement.
  • 3.
    Initialization: •POP_SIZE: Number ofindividuals in the population. •MUT_RATE: Mutation rate, indicating the probability of a mutation occurring. •TARGET: The target string that the algorithm aims to evolve towards. •GENES: The set of characters that can be used to form individuals in the population.
  • 4.
    Initialization of Population: initialize_pop(TARGET):Generates an initial population of strings, each having the same length as the target string. The characters of the strings are randomly chosen from the set of genes.
  • 5.
    Crossover crossover(selected_chromo, CHROMO_LEN, population): Performscrossover/mating between selected individuals from the current population to create a new population
  • 6.
    Mutation mutate(offspring, MUT_RATE): Introducesrandom mutations in the offspring with a probability specified by the mutation rate.
  • 7.
    Selection Selection(population, TARGET): Selectsthe best individuals from the current population based on their fitness.
  • 8.
    Replacement replace(new_gen, population): Replacesindividuals in the current population with new individuals from the new generation if they are fitter.
  • 9.
    Main Genetic AlgorithmLoop: main(POP_SIZE, MUT_RATE, TARGET, GENES): The main loop that iterates through generations until the target string is found. It involves the following steps:
  • 10.
    • Initialize thepopulation. • Calculate the fitness of the initial population. • Iterate until the target is found: • Select the best individuals. • Mate parents to create a new generation. • Mutate the new generation. • Replace the least fit individuals with the new generation. • Check if the target is found and print information.
  • 11.
     The algorithmprints information about each generation, including the string, generation number, and fitness of the fittest individual. The process continues until the target string is found.
  • 12.
    ¼ sir? YES ¼POOOOOO 