Open In App

Single Point Crossover in Genetic Algorithm - Python

Last Updated : 06 May, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Single Point Crossover is a method used in a technique called genetic algorithms which are inspired by how living things pass on their traits to their children. Just like in nature where a child gets some traits from the mother and some from the father this method mixes two “parent solutions” to create new ones. This helps us to create better and stronger solutions by combining good parts from different options. There are four key steps in single point crossover:

  • Select Parents : Choose two parent chromosomes from population.
  • Choose a Crossover Point : Randomly select a position in chromosome.
  • Perform Crossover : Swaps the segments of parents after the crossover point to create two offspring.
  • Generate Offspring : Add new offspring to next generation.

This method ensures diversity in the population while preserving some genetic information from parents.

Why Use Single Point Crossover?

Single-point crossover is one of the simple crossover techniques and has several advantages:

  1. Simplicity : Easy to implement and computationally efficient.
  2. Diversity : Introduces variability in population that helps algorithm to explore search space.
  3. Preservation of Traits : It retains some genetic material from both parents and maintain good traits.

While more crossover techniques like multi-point or uniform crossover exist single point crossover is useful for many problems.

Implementation in Python

Let’s implement Single Point Crossover in Python step-by-step.

Step 1: Defining Parent Chromosomes

Each chromosome can be represented as a list of binary values like [1, 0, 1, 1, 0]. These values could represent features, decisions or parameters in an optimization problem.

Python
import random

parent1 = [1, 0, 1, 1, 0]
parent2 = [0, 1, 0, 1, 1]

print("Parent 1:", parent1)
print("Parent 2:", parent2)

Output:

Parent 1: [1, 0, 1, 1, 0]
Parent 2: [0, 1, 0, 1, 1]

Step 2: Select Random Crossover Point

Choose a random index in the chromosome as the crossover point. This point determines where the segments will be swapped.

Python
crossover_point = random.randint(1, len(parent1) - 1)

print("Crossover Point:", crossover_point)

Output:

Crossover Point: 3

Step 3: Perform Crossover

Swap the segments of the parents after the crossover point to create two offspring.

Python
offspring1 = parent1[:crossover_point] + parent2[crossover_point:]
offspring2 = parent2[:crossover_point] + parent1[crossover_point:]

print("Offspring 1:", offspring1)
print("Offspring 2:", offspring2)

Output:

Offspring 1: [1, 0, 1, 1, 1]

Offspring 2: [0, 1, 0, 1, 0]

Step 4: Combine Everything into a Function

To make the process reusable we can encapsulate it in a function.

Python
def single_point_crossover(parent1, parent2):

    if len(parent1) != len(parent2):
        raise ValueError("Parents must have the same length.")

    crossover_point = random.randint(1, len(parent1) - 1)

    offspring1 = parent1[:crossover_point] + parent2[crossover_point:]
    offspring2 = parent2[:crossover_point] + parent1[crossover_point:]
    
    return offspring1, offspring2

parent1 = [1, 0, 1, 1, 0]
parent2 = [0, 1, 0, 1, 1]

offspring1, offspring2 = single_point_crossover(parent1, parent2)
print("Offspring 1:", offspring1)
print("Offspring 2:", offspring2)

Output:

Offspring 1: [1, 0, 1, 1, 1]
Offspring 2: [0, 1, 0, 1, 0]

Applications of Single Point Crossover

Single Point Crossover is widely used in various domains like:

  • Solving Puzzles and Finding Best Routes: It helps in problems like finding the shortest path to visit cities like Traveling Salesman Problem or to adjust different settings to get the best results.
  • Choosing Important Information: It is used to find the most useful piece of data from a large set like picking the best features from a dataset in a smart way.
  • Creating Game Strategies: It helps in making better moves or decisions for games by mixing two good plans together.
  • Improving Product Designs: It is useful in designing things like bridges, vehicles or machines by combining good design parts to get better results in terms of cost, strength or weight.

Similar Reads