-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindividual.py
63 lines (52 loc) · 1.71 KB
/
individual.py
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
import numpy as np
class Individual:
def __init__(self, genes):
self.genes = genes
self._fitness = None
def evaluate_fitness(self):
"""
This method should be implemented in subclasses to evaluate fitness.
"""
raise NotImplementedError
def fitness(self):
"""
Returns the fitness of the individual. Computes fitness if it has not
been computed yet.
"""
if self._fitness is None:
self.evaluate_fitness()
return self._fitness
def crossover(self, partner):
"""
Partially mapped crossover (PMX)
Returns
-------
child
"""
size = len(self.genes)
child = [-1] * size
a, b = sorted(np.random.choice(range(size), size=2, replace=False))
# Copy segment from self to child
for i in range(a, b):
child[i] = self.genes[i]
# Create mapping for the other parent
mapping = {value: index for index, value in enumerate(child) if value != -1}
# Fill in the rest from other
for i in range(size):
if i < a or i >= b:
value = partner.genes[i]
while value in mapping:
value = partner.genes[mapping[value]]
child[i] = value
mapping[value] = i
return child
def mutate(self, mutation_probability):
"""
Mutation: Swap mutation
"""
if np.random.rand() < mutation_probability:
idx1, idx2 = np.random.randint(0, len(self.genes), 2)
self.genes[idx1], self.genes[idx2] = (
self.genes[idx2],
self.genes[idx1],
)