Skip to content

Create genetic_algorithm.py #9548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions genetic_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import random

# Define the target string (the goal of the genetic algorithm)
target_string = "Hello, World!"

# Function to generate a random string of the same length as the target
def generate_random_string(length):
return ''.join(random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,.!?") for _ in range(length))

# Function to calculate the fitness of an individual (string)
def calculate_fitness(individual):
return sum(1 for i, j in zip(individual, target_string) if i == j)

# Function to perform selection (tournament selection)
def selection(population):
tournament_size = 3
selected = [random.choice(population) for _ in range(tournament_size)]
return max(selected, key=calculate_fitness)

# Function to perform crossover (single-point crossover)
def crossover(parent1, parent2):
point = random.randint(1, len(parent1) - 1)
child = parent1[:point] + parent2[point:]
return child

# Function to perform mutation (random character mutation)
def mutation(individual, mutation_rate):
return ''.join(
random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,.!?")
if random.random() < mutation_rate else char
for char in individual
)

# Main genetic algorithm loop
population_size = 100
mutation_rate = 0.01

population = [generate_random_string(len(target_string)) for _ in range(population_size)]

generation = 1
while True:
population = sorted(population, key=calculate_fitness, reverse=True)
best_individual = population[0]

if best_individual == target_string:
break

new_population = [best_individual]

while len(new_population) < population_size:
parent1 = selection(population)
parent2 = selection(population)
child = crossover(parent1, parent2)
child = mutation(child, mutation_rate)
new_population.append(child)

population = new_population
generation += 1

if generation % 10 == 0:
print(f"Generation {generation}: {best_individual} (Fitness: {calculate_fitness(best_individual)})")

print(f"Generation {generation}: {best_individual} (Fitness: {calculate_fitness(best_individual)})")
print("Genetic algorithm completed!")