Python | Single Point Crossover in Genetic Algorithm Last Updated : 13 Oct, 2019 Improve Improve Like Article Like Save Share Report Single Point Crossover in Genetic Algorithm is a form of crossover in which two-parent chromosome are selected and a random/given point is selected and the genes/data are interchanged between them after the given/selected point for example Examples: P1: 000011110011 P2: 101010101010 Point: 4 After Crossover: C1: 000010101010 C2: 101011110011 The problem is to select a random point for the crossover of two given parents and generate at least five generations of children from the given pair of a chromosome. Code : Python program for single-point crossover in Genetic Algorithm # library to generate a random number import random # function for implementing the single-point crossover def crossover(l, q): # converting the string to list for performing the crossover l = list(l) q = list(q) # generating the random number to perform crossover k = random.randint(0, 15) print("Crossover point :", k) # interchanging the genes for i in range(k, len(s)): l[i], q[i] = q[i], l[i] l = ''.join(l) q = ''.join(q) print(l) print(q, "\n\n") return l, q # patent chromosomes: s = '1100110110110011' p = '1000110011011111' print("Parents") print("P1 :", s) print("P2 :", p, "\n") # function calling and storing the off springs for # next generation crossover for i in range(5): print("Generation ", i+1, "Childrens :") s, p = crossover(s, p) Output: Parents P1 : 1100110110110011 P2 : 1000110011011111 Generation 1 Childrens : Crossover point : 2 1100110011011111 1000110110110011 Generation 2 Childrens : Crossover point : 7 1100110110110011 1000110011011111 Generation 3 Childrens : Crossover point : 0 1000110011011111 1100110110110011 Generation 4 Childrens : Crossover point : 7 1000110110110011 1100110011011111 Generation 5 Childrens : Crossover point : 2 1000110011011111 1100110110110011 Like Article Suggest improvement Next Crossover in Genetic Algorithm Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads Crossover in Genetic Algorithm Genetic Algorithm for Reinforcement Learning : Python implementation Simple Genetic Algorithm (SGA) Encoding Methods in Genetic Algorithm Steady State Genetic Algorithm (SSGA) ML - Convergence of Genetic Algorithms Genetic Algorithms for Graph Colouring | Project Idea Connect new point to the previous point on a image with a straight line in Opencv-Python Transpose a matrix in Single line in Python Python code to move spaces to front of string in single traversal Like A AKSHAYRAJ4 Follow Article Tags : Artificial Intelligence Genetic Algorithms Machine Learning Python Practice Tags : Machine Learningpython