Professional Documents
Culture Documents
INSTITUTE OF ENGINEERING
PULCHOWK CAMPUS
Lab - 5
Artificial Intelligence
Genetic Algorithm
Implementation
Selection: The selection process is used to determine which solutions are the
most fit and should be used to create the next generation of candidate solutions.
This process is based on the principle of natural selection, where individuals
with the best fitness have a higher chance of being selected for reproduction.
There are several methods for selection, such as tournament selection, roulette
wheel selection, and rank-based selection.
Reproduction: Once the selection process is complete, the next step is to create
new candidate solutions through reproduction. This is done by applying genetic
operators such as mutation and crossover to the selected individuals. Mutation
involves randomly changing one or more digits in the binary string, while
crossover involves combining two selected individuals to create a new
individual that inherits traits from both parents.
def fitnessValue(chromosome):
a, b, c, d, e, f, g, h = [int(digit) for digit in chromosome]
return (a + b) - (c + d) + (e + f) - (g + h)
def mutation(chromosome):
"""perform two point mutation"""
index = random.randint(0, 6)
replaced_number1 = random.randint(0, 9)
replaced_number2 = random.randint(0, 9)
return chromosome[0:index] + str(replaced_number1) +
str(replaced_number2) + chromosome[index + 2:]
One advantage of genetic algorithms is that they are able to explore a wide
range of potential solutions to a problem, which can lead to finding better
solutions than other optimization techniques. Additionally, genetic algorithms
are relatively easy to implement and can be adapted to a wide range of
problems.