Evolutionary algorithm

Mehmet Akif Cifci
3 min readFeb 8, 2023

--

An evolutionary algorithm is a type of computer program that uses principles from evolution and natural selection to find solutions to problems. It works by mimicking the process of evolution and natural selection to evolve better and better solutions over time.

Example:

A company wants to optimize the design of a new product. They use an evolutionary algorithm to generate a number of different designs and test them. The designs that perform the best are selected to be used in the next generation of designs, and the process repeats until an optimal design is found.
A farmer wants to find the best crop rotation strategy for their land. They use an evolutionary algorithm to generate different crop rotation plans and evaluate their performance based on factors such as yield, soil health, and pest management. The best plans are selected to be used in the next generation of plans, and the process continues until the farmer finds the optimal crop rotation strategy.

Here is an example of a simple Python code implementing a genetic algorithm to solve the problem of finding the maximum value of the mathematical function f(x) = x²:

import random
# Define the population size
POPULATION_SIZE = 20
# Define the number of generations
NUM_GENERATIONS = 50
# Define the mutation rate
MUTATION_RATE = 0.1
# Define the function to optimize
def f(x):
return x**2
# Generate the initial population
def generate_population(population_size):
population = []
for i in range(population_size):
x = random.uniform(-10, 10)
population.append(x)
return population
# Evaluate the fitness of a member of the population
def evaluate_fitness(member):
return f(member)
# Select two members to breed based on their fitness
def selection(population, fitness_values):
sorted_population = [x for _,x in sorted(zip(fitness_values, population))]
parent1 = random.choice(sorted_population[-2:])
parent2 = random.choice(sorted_population[-2:])
return parent1, parent2
# Breed two members to produce a child
def crossover(parent1, parent2):
child = (parent1 + parent2) / 2
return child
# Apply mutation to a member of the population
def mutation(member):
if random.random() < MUTATION_RATE:
member += random.uniform(-1, 1)
return member
# The main loop of the genetic algorithm
def genetic_algorithm():
population = generate_population(POPULATION_SIZE)
for generation in range(NUM_GENERATIONS):
fitness_values = [evaluate_fitness(member) for member in population]
new_population = []
for i in range(POPULATION_SIZE):
parent1, parent2 = selection(population, fitness_values)
child = crossover(parent1, parent2)
child = mutation(child)
new_population.append(child)
population = new_population
return population
population = genetic_algorithm()
fitness_values = [evaluate_fitness(member) for member in population]
best_member = population[fitness_values.index(max(fitness_values))]
print("Best member:", best_member)
print("Fitness value:", f(best_member))

The code defines the problem to be optimized as a function f(x) = x². The initial population of 20 individuals is randomly generated and evaluated for their fitness based on the defined function.

Selection, crossover, and mutation are the three main operators in the genetic algorithm, and they are defined as separate functions in this code.

Selection chooses two members from the current population based on their fitness to be parents for the next generation. Crossover combines the two selected parents to create a child. Mutation introduces random changes to the child.

The genetic algorithm consists of the following steps:

Generate the initial population
Evaluate the fitness of each member of the population
Select two members to breed based on their fitness
Breed the two members to produce a child
Apply mutation to the child
Repeat the above steps for the specified number of generations (in this case, 50)

Finally, the best member of the final population is identified by finding the member with the highest fitness value, and it is returned as the result.

--

--

Mehmet Akif Cifci
Mehmet Akif Cifci

Written by Mehmet Akif Cifci

Mehmet Akif Cifci holds the position of associate professor in the field of computer science in Austria.

No responses yet