2) Random Resetting Mutation
Random resetting mutation is another mutation technique used in genetic algorithms. Unlike bit flip mutation, which operates on binary-encoded individuals, random resetting mutation can be applied to various encoding schemes, including real-valued & integer-valued representations. In this mutation, each gene has a probability of being reset to a randomly selected value within a specified range.
For example :
Original individual: [4.2, 2.7, 1.5, 3.8]
Mutated individual: [4.2, 1.3, 1.5, 3.8]
In this example, the second gene is reset to a new randomly selected value (1.3) within the specified range. Random resetting mutation introduces larger changes compared to bit flip mutation, as it allows the algorithm to explore entirely new regions of the search space.
For example :
import random
def random_resetting_mutation(individual, mutation_rate, lower_bound, upper_bound):
mutated_individual = individual.copy()
for i in range(len(mutated_individual)):
if random.random() < mutation_rate:
mutated_individual[i] = random.uniform(lower_bound, upper_bound)
return mutated_individual
In this code, the `random_resetting_mutation` function takes an individual (represented as a list of real values), a mutation rate, & the lower & upper bounds for the randomly selected values. It creates a copy of the individual & iterates over each gene. If the mutation rate condition is met, the gene is reset to a randomly selected value within the specified range using the `random.uniform` function. Finally, the mutated individual is returned.
Note: Random resetting mutation is useful when the algorithm needs to explore a wider range of values & escape local optima. If we introduce larger changes to the individuals, it allows the algorithm to diversify the population & potentially discover new promising regions in the search space.
3) Swap Mutation
Swap mutation is a mutation technique commonly used in genetic algorithms that operate on permutation-based encodings, such as in solving optimization problems like the Traveling Salesman Problem (TSP). In swap mutation, two randomly selected genes within an individual are swapped, creating a new permutation of the original sequence.
Let’s see an example of how swap mutation works:
Original individual: [1, 2, 3, 4, 5]
Mutated individual: [1, 4, 3, 2, 5]
In this example, the second and fourth genes (2 and 4) are swapped, resulting in a new permutation of the original sequence. Swap mutation maintains the validity of the permutation while introducing changes to the order of the genes.
For example :
import random
def swap_mutation(individual):
mutated_individual = individual.copy()
gene1, gene2 = random.sample(range(len(mutated_individual)), 2)
mutated_individual[gene1], mutated_individual[gene2] = mutated_individual[gene2], mutated_individual[gene1]
return mutated_individual
In this code, the `swap_mutation` function takes an individual (represented as a list of integers) as input. It creates a copy of the individual to avoid modifying the original. Then, it randomly selects two distinct gene indices using the `random.sample` function. The selected genes are swapped using tuple unpacking, creating a new permutation. Finally, the mutated individual is returned.
Swap mutation is effective in permutation-based problems because it preserves the validity of the permutation while introducing changes to the order of the genes. It allows the algorithm to explore different arrangements of the genes and potentially discover better solutions.
Note: Swap mutation can be very useful in problems where the order of the genes is crucial, such as in scheduling or routing optimization tasks. By swapping genes, the algorithm can generate new permutations and explore different configurations of the solution space.
4) Scramble Mutation
Scramble mutation is another mutation technique used in genetic algorithms, particularly in permutation-based encodings. In scramble mutation, a subset of genes within an individual is randomly selected, and their order is scrambled or shuffled. This mutation introduces larger changes to the individual compared to swap mutation, as it rearranges multiple genes at once.
Here's an example of how scramble mutation works:
Original individual: [1, 2, 3, 4, 5, 6, 7, 8]
Mutated individual: [1, 2, 5, 7, 3, 6, 4, 8]
In this example, a subset of genes (3, 4, 5, 6, 7) is randomly selected and their order is scrambled, resulting in a new arrangement of the genes within that subset. The remaining genes (1, 2, 8) remain unchanged.
For example
import random
def scramble_mutation(individual):
mutated_individual = individual.copy()
start, end = sorted(random.sample(range(len(mutated_individual)), 2))
subset = mutated_individual[start:end+1]
random.shuffle(subset)
mutated_individual[start:end+1] = subset
return mutated_individual
In this code, the `scramble_mutation` function takes an individual (represented as a list) as input. It creates a copy of the individual to avoid modifying the original. Then, it randomly selects two indices (start and end) to define the subset of genes to be scrambled. The selected subset is extracted using list slicing, and the `random.shuffle` function is used to randomly reorder the genes within that subset. Finally, the scrambled subset is reinserted into the mutated individual, and the mutated individual is returned.
Scramble mutation introduces a higher level of disruption to the individual compared to swap mutation. It allows the algorithm to explore different arrangements of genes within a selected subset, potentially leading to more diverse solutions.
Note: Scramble mutation can be useful in situations where the relative order of genes within a subset is important, but the overall structure of the individual needs to be preserved. It can help the algorithm escape local optima and explore new configurations within the selected subset of genes.
5) Inversion Mutation
Inversion mutation is a mutation technique used in genetic algorithms that operates on permutation-based encodings. In inversion mutation, a subset of genes within an individual is selected, and the order of the genes within that subset is reversed. This mutation preserves the positions of the genes outside the selected subset while introducing changes to the order of the genes within the subset.
Let’s see an example of how inversion mutation works:
Original individual: [1, 2, 3, 4, 5, 6, 7, 8]
Mutated individual: [1, 2, 6, 5, 4, 3, 7, 8]
In this example, a subset of genes (3, 4, 5, 6) is selected, and their order is reversed, resulting in a new arrangement of the genes within that subset. The genes outside the selected subset (1, 2, 7, 8) remain unchanged.
Let’s see the code in python :
import random
def inversion_mutation(individual):
mutated_individual = individual.copy()
start, end = sorted(random.sample(range(len(mutated_individual)), 2))
mutated_individual[start:end+1] = reversed(mutated_individual[start:end+1])
return mutated_individual
In this code, the `inversion_mutation` function takes an individual (represented as a list) as input. It creates a copy of the individual to avoid modifying the original. Then, it randomly selects two indices (start and end) to define the subset of genes to be inverted. The selected subset is extracted using list slicing, and the `reversed` function is used to reverse the order of the genes within that subset. Finally, the inverted subset is reinserted into the mutated individual, and the mutated individual is returned.
Inversion mutation is useful in scenarios where the relative order of genes within a subset is important, and reversing that order can potentially lead to better solutions. It allows the algorithm to explore different arrangements of genes while maintaining the overall structure of the individual.
Inversion mutation can be particularly effective in problems where the order of genes within a subset has a significant impact on the fitness of the individual. By reversing the order of genes within a subset, the algorithm can create new configurations and potentially discover improved solutions.
Note: Inversion mutation introduces a moderate level of disruption to the individual compared to other mutation techniques. It preserves the positions of genes outside the selected subset, which makes it a suitable choice when the overall structure of the individual needs to be maintained while exploring variations within specific subsets of genes.
Frequently Asked Questions
What is the purpose of mutation in genetic algorithms?
Mutation in genetic algorithms serves the purpose of maintaining genetic diversity and exploring new solutions. It introduces random changes to the genetic material of individuals, allowing the algorithm to escape local optima and discover potentially better solutions in the search space.
How does the mutation rate affect the performance of genetic algorithms?
The mutation rate is a parameter that determines the probability of applying mutation to an individual. A higher mutation rate introduces more diversity but can also disrupt good solutions. A lower mutation rate preserves good solutions but may limit exploration. Finding the right balance is crucial for the algorithm's performance.
Can multiple mutation techniques be used together in a genetic algorithm?
Yes, multiple mutation techniques can be used together in a genetic algorithm. The choice of mutation technique depends on the problem domain and the encoding scheme used. Different mutation techniques can be applied to different parts of the individual or combined to introduce various levels of changes in the genetic material.
Conclusion
In this article, we have learned about different mutation in genetic algorithms. We discussed bit flip mutation, random resetting mutation, swap mutation, scramble mutation, and inversion mutation. Each mutation technique bring changes to the genetic material of individuals in different ways, allows the algorithm to look for new solutions and maintain genetic diversity.
You can also check out our other blogs on Code360.