-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclonalSelectionFactory.py
More file actions
117 lines (92 loc) · 4.52 KB
/
clonalSelectionFactory.py
File metadata and controls
117 lines (92 loc) · 4.52 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import math
from random import random
from copy import deepcopy
class ClonALG:
def __init__(self):
self.memoryset = []
self._iteration = 0
self._antibodies = []
def random_antibody_fcn(self):
raise NotImplemented()
def calculate_affinity_fcn(self, antibodies):
raise NotImplemented()
def clone_antibodies_fcn(self, antibodies, clone_rate):
clones = []
for a in antibodies:
clones += [deepcopy(a) for _ in range(int(math.ceil(len(antibodies) * a.affinity * clone_rate)))]
for c in clones:
c.affinity = 0
return clones
def mutation_fcn(self, clones, mutation_exp):
for i in range(0, len(clones)):
mutation_rate = math.exp(-clones[i].affinity * mutation_exp)
clones[i] = self.point_mutation(clones[i], mutation_rate)
return clones
def selection_fcn(self, antibodies):
return sorted(antibodies, key=lambda x: x.affinity, reverse=True)
def stop_criterion(self):
raise NotImplemented()
def remove_antibodies(self, antibodies, max_antibodies):
antibodies = self.selection_fcn(antibodies)[:max_antibodies]
return antibodies
def print_info(self, iteration):
raise NotImplemented()
def rand_paratope(self):
raise NotImplemented()
def point_mutation(self, clone, mutation_rate):
"""
Iterates over each clone's paratope and mutates it accordingly to the mutation rate
:param clone: The clone on which perform the mutation
:param mutation_rate: The mutation's occurrence rate
:return: The mutated clone
"""
for i in range(0, len(clone.paratopes)):
if random() < mutation_rate:
clone.paratopes[i] = self.rand_paratope()
return clone
def run(self, number_of_antibodies, clone_rate, mutation_exp, to_memory,
num_remove=2, max_antibodies=100, verbose=False):
"""
Runs the Clonal Selection Algorithm and returns the computed memory set containing the best antibodies.
:param number_of_antibodies: Initial number of antibodies.
:param clone_rate: The clonation's occurrence rate.
:param mutation_exp: Mutation exponent, mutation probability is proportional to exp(-affinity*mutation_exp).
:param to_memory: The number of antibodies being returned.
:param num_remove: The number of the worst antibodies that are removed on each iteration.
:param max_antibodies: The maximum number of antibodies that are kept between subsequent iterations.
:param verbose: If set, prints some execution details and executes "print_info" function on each iteration
:return: The memory set containing the best antibodies.
"""
# Initialization of the variable contatining the index of the iteration and of the antibodies set
self._iteration = 0
self._antibodies = []
if verbose:
print("Generating random antibodies")
# Antibodies creation
for i in range(0, number_of_antibodies):
self._antibodies.append(self.random_antibody_fcn())
if verbose:
print("Antibodies have been generated")
while not self.stop_criterion():
# Increment the iteration number
self._iteration += 1
# Calculate affinity for each antibody
self.calculate_affinity_fcn(self._antibodies)
# Clonation
clones = self.clone_antibodies_fcn(self._antibodies, clone_rate)
# Hypermutation
clones = self.mutation_fcn(clones, mutation_exp)
# Computes the clones' affinity
self.calculate_affinity_fcn(clones)
# Add the clones to the antibodies list
self._antibodies += clones
# This is needed in order to remove identical/unnecessary antibodies
self._antibodies = self.remove_antibodies(self._antibodies, max_antibodies)
# Assignment of the best antibodies to the memory set
self.memoryset = self._antibodies[:to_memory]
if verbose:
self.print_info(iteration=self._iteration)
if len(self._antibodies) - num_remove > 0:
for i in range(len(self._antibodies) - num_remove, len(self._antibodies)):
self._antibodies[i] = self.random_antibody_fcn()
return self.memoryset