-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenetic_algorithm.py
More file actions
58 lines (45 loc) · 2.49 KB
/
genetic_algorithm.py
File metadata and controls
58 lines (45 loc) · 2.49 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
## genetic_algorithm.py
from deap import base, creator, tools
import random
class GeneticAlgorithm:
def __init__(self, population_size: int = 100, individual_size: int = 10, fitness_goal: float = 1.0):
self.population_size = population_size
self.individual_size = individual_size
self.fitness_goal = fitness_goal
self.toolbox = base.Toolbox()
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
self.toolbox.register("attr_bool", random.randint, 0, 1)
self.toolbox.register("individual", tools.initRepeat, creator.Individual, self.toolbox.attr_bool, self.individual_size)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
self.toolbox.register("evaluate", self.evaluate)
self.toolbox.register("mate", tools.cxTwoPoint)
self.toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
self.toolbox.register("select", tools.selTournament, tournsize=3)
self.population = self.toolbox.population(n=self.population_size)
def evaluate(self, individual):
# This is a placeholder for the evaluation function
# It should be replaced with a function that evaluates the fitness of an individual
return sum(individual),
def evolve(self):
CXPB, MUTPB = 0.5, 0.2
fits = self.toolbox.map(self.toolbox.evaluate, self.population)
for fit, ind in zip(fits, self.population):
ind.fitness.values = fit
while max(ind.fitness.values for ind in self.population) < self.fitness_goal:
offspring = self.toolbox.select(self.population, len(self.population))
offspring = list(map(self.toolbox.clone, offspring))
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < CXPB:
self.toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
if random.random() < MUTPB:
self.toolbox.mutate(mutant)
del mutant.fitness.values
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = self.toolbox.map(self.toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
self.population[:] = offspring