In genetic algorithms, the crossover is a genetic operator that involves recombining genetic material between two parent individuals to generate new offspring.
During crossover, a portion of the genetic material of each parent is exchanged or combined to create a new individual with a combination of traits from both parents. The specific mechanism of crossover varies depending on the encoding scheme used to represent the individuals. Still, the genetic material is generally divided into segments that are exchanged between parents to create offspring.
Crossover is a key component of genetic algorithms, because it allows for creating new solutions that combine beneficial features from multiple parent solutions. In addition, the crossover can help maintain diversity in the population of solutions over the optimization process. Overall, the crossover is a powerful tool for enhancing genetic algorithms' exploration and exploitation capabilities. It's also essential for achieving high-quality solutions to many optimization problems.
There are two common variations of the crossover operator in genetic algorithms.
One-point crossover
Two-point crossover
One-point crossover involves selecting a random point along the length of the parents, and exchanging the values on the tails of the parents results in new offspring.
Let's have a look at the one-point crossover code example below:
# Define the two parents as arraysparent1 = [1, 2, 3, 4, 5, 6, 7, 8]parent2 = [1, 3, 5, 7, 9, 11, 13, 15]def one_point_crossover(parent1, parent2, crossover_point):"Perform one-point crossover between two parents at a specified point"# Create offspring by combining parent genes up to the specified crossover pointoffspring1 = parent1[:crossover_point] + parent2[crossover_point:]offspring2 = parent2[:crossover_point] + parent1[crossover_point:]return offspring1, offspring2# Specify the crossover point (e.g., point 3)crossover_point = 4# Perform one-point crossover on the two parents at the specified pointoffspring1, offspring2 = one_point_crossover(parent1, parent2, crossover_point)# Print the resultsprint("Parent 1: ", parent1)print("Parent 2: ", parent2)print("Offspring 1: ", offspring1)print("Offspring 2: ", offspring2)
Lines 2–3: We define the two parents as arrays parent1
and parent2
.
Lines 5–12: The one_point_crossover
function is defined to perform a one-point crossover operation between two parents at a specified crossover point.
Line 18: We call the one_point_crossover()
with three parameters parent1
, parent2
and crossover_point
.
Lines 21–24: We print both parents and offspring.
Two-point crossover is similar to one-point crossover but, instead of selecting a single point, two points are chosen for the parents. The values between the two points are exchanged between the parents to get new offspring.
Let's have a look at the two-point crossover code example below:
# Define the two parents as arraysparent1 = [1, 2, 3, 4, 5, 6, 7, 8]parent2 = [1, 3, 5, 7, 9, 11, 13, 15]def two_point_crossover(parent1, parent2, point1, point2):"Perform a two-point crossover between two parents at specified points"# Create offspring by exchanging genetic material between parents at the specified pointsoffspring1 = (parent1[:point1] + parent2[point1:point2] + parent1[point2:])offspring2 = (parent2[:point1] + parent1[point1:point2] + parent2[point2:])return offspring1, offspring2# Specify the crossover pointscrossover_point1 = 2crossover_point2 = 6# Perform two-point crossover on the two parents at the specified pointsoffspring1, offspring2 = two_point_crossover(parent1, parent2, crossover_point1, crossover_point2)# Print the resultsprint("Parent 1: ", parent1)print("Parent 2: ", parent2)print("Offspring 1: ", offspring1)print("Offspring 2: ", offspring2)
Lines 2–3: We define the two parents as arrays parent1
and parent2
.
Lines 5–15: The two_point_crossover
function is defined to perform a two-point crossover operation between two parents at specified crossover points.
Line 22: The two_point_crossover
function is called with parent1
, parent2
, crossover_point1
, and crossover_point2
.
Lines 25–28: We print both parents and offspring.
Free Resources