Repository navigation
Expand file tree
/
Copy pathGeneticAlgorithm.py
More file actions
74 lines (61 loc) · 2.8 KB
/
Copy pathGeneticAlgorithm.py
File metadata and controls
74 lines (61 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import random
import matplotlib.pyplot as plt
# Define the number of chromosomes in the population
POPULATION_SIZE = 100
# Define the length of each chromosome (number of genes)
CHROMOSOME_LENGTH = 10
# Define the maximum number of generations to run the algorithm
MAX_GENERATIONS = 100
# Define the probability of a mutation occurring
MUTATION_PROBABILITY = 0.01
# Define the fitness function to be optimized (in this example, we will maximize a function that takes a list of floats as input)
def fitness_function(genes):
return sum(genes)
# Generate a random float between min and max
def random_float(min, max):
return random.uniform(min, max)
# Generate a random chromosome with genes between min and max
def generate_chromosome(min, max):
return [random_float(min, max) for i in range(CHROMOSOME_LENGTH)]
# Generate a population of random chromosomes
def generate_population(min, max):
return [generate_chromosome(min, max) for i in range(POPULATION_SIZE)]
# Select two parents from the population using tournament selection
def tournament_selection(population):
parent1 = random.choice(population)
parent2 = random.choice(population)
while parent1 == parent2:
parent2 = random.choice(population)
return parent1, parent2
# Perform crossover on two parent chromosomes to create two child chromosomes
def crossover(parent1, parent2):
crossover_point = random.randint(0, CHROMOSOME_LENGTH - 1)
child1 = parent1[:crossover_point] + parent2[crossover_point:]
child2 = parent2[:crossover_point] + parent1[crossover_point:]
return child1, child2
# Perform mutation on a chromosome by randomly changing one of its genes
def mutation(chromosome, min, max):
if random.random() < MUTATION_PROBABILITY:
index = random.randint(0, CHROMOSOME_LENGTH - 1)
chromosome[index] = random_float(min, max)
# Run the genetic algorithm and return the best chromosome
def run_genetic_algorithm(min, max):
population = generate_population(min, max)
best_fitnesses = []
for i in range(MAX_GENERATIONS):
parents = [tournament_selection(population) for i in range(POPULATION_SIZE // 2)]
children = [crossover(parent1, parent2) for parent1, parent2 in parents]
population = [child1 + child2 for child1, child2 in children]
for chromosome in population:
mutation(chromosome, min, max)
fitnesses = [fitness_function(chromosome) for chromosome in population]
best_fitness = max(fitnesses)
best_fitnesses.append(best_fitness)
best_chromosome = population[fitnesses.index(best_fitness)]
return best_chromosome, best_fitnesses
# Run the genetic algorithm and plot the results
best_chromosome, best_fitnesses = run_genetic_algorithm(0, 1)
plt.plot(best_fitnesses)
plt.xlabel('Generation')
plt.ylabel('Fitness')
plt.show()