-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.py
195 lines (135 loc) · 4.88 KB
/
ga.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from abc import ABC, abstractmethod
from numpy.random import permutation, rand, choice
from hashlib import md5
#python makes a mess if a class has the ABCmeta to determine whether it's a (sub)instance of another class
#so I made my own version
def ga_isinstance(obj,cls):
return cls in obj.__class__.__mro__
class Individual(ABC):
def __init__(this):
this.parents= []
this.initialise()
this.id = rand()
@property
def id(this):
return this._id
@id.setter
def id(this,id):
this._id = md5(str(id).encode()).hexdigest()
def add_parent(this,iter,parent):
this.parents.append((iter,parent))
@property
def fitness(this):
return this.calculateFitness()
@abstractmethod
def __getitem__(this,key):
...
@abstractmethod
def __setitem__(this, key,value):
...
@abstractmethod
def __len__(this):
...
@abstractmethod
def getSequence(this):
...
@abstractmethod
def initialise(this):
...
@abstractmethod
def calculateFitness(this):
...
@abstractmethod
def is_good(this):
...
class GAOperator(ABC):
def __init__(this):
...
class UnaryOperator (GAOperator):
@abstractmethod
def apply(this,individual):
...
class BinaryOperator (GAOperator):
@abstractmethod
def apply(this, a,b):
...
class Selection(ABC):
def __init__(this,size=10):
this.__size = size
def __len__(this):
return this.__size
@property
def size(this):
return len(this)
@size.setter
def size(this,new_size):
this.__size = new_size
@abstractmethod
def selectPool(this, population):
...
class RandomSelection(Selection):
def selectPool(this, population):
return list(permutation(len(population))[:len(this)])
class GeneticAlgorithm:
def __init__(this,population_size=100,selection=None,individual=None,params=None,operators=None,max_iter=1000,track_parents=False):
this.population_size = population_size
this.selection = selection
this.max_iter = max_iter
this.track_parents = track_parents
n = sum(operators.values())
this.operators = { k:(v/n) for k,v in sorted(operators.items(),key=lambda x:x[1],reverse=True)}
if (params == None):
params = []
this.population = [individual(*params) if type(params)==list else individual(**params) for i in range(population_size)]
def __str__(this):
value = ""
for i in range(len(this.population)):
value+=(f"Individual {i+1}: {this.population[i].calculateFitness()}\n")
return value.strip()
def sort_population(this):
this.population = sorted(this.population, key=lambda x: x.fitness)
def evolve(this):
i=0
stop = False
while (i<this.max_iter) and (not stop):
this.sort_population()
stop = this.population[0].fitness == 0 #reached global maximum
if (stop):
continue
if (len(this.population)>this.population_size):
#let's kill individuals with higher fitness
this.population = this.population[:this.population_size]
#print iteration:
print(f"\nIteration ({i+1}/{this.max_iter})")
print(this)
pool_idx = this.selection.selectPool(this.population)
for idx in pool_idx:
ind = this.population[idx]
if (not ind.is_good()):
continue
operator_applied = False
for operator,prob in this.operators.items():
x = rand()
if (x<=prob):
if (ga_isinstance(operator,UnaryOperator)):
parent=ind
ind = operator.apply(parent)
operator_applied = True
if (this.track_parents):
ind.add_parent(i,parent)
elif (ga_isinstance(operator, BinaryOperator)):
parent = ind
#select a random sample from the selected individuals for mating
pool_idx2 = pool_idx.copy()
pool_idx2.remove(idx)
parent2 = this.population[choice(pool_idx2)]
ind = operator.apply(parent,parent2)
operator_applied = True
if (this.track_parents):
ind.add_parent(i,(parent,parent2))
if (operator_applied):
ind.id = rand()
ind.calculateFitness()
this.population.append(ind)
i+=1
this.sort_population()