-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic_engine.py
324 lines (246 loc) · 10.7 KB
/
genetic_engine.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
from pyevolve import GenomeBase
from flow import Flow, random_flow
from fx import FFlow
from nets_manager import directions, masks
from tester import network_packets_count_tester
class NetworkGenome(GenomeBase.GenomeBase):
"""
Класс, представляющий собой модель реальной сети, для эволюционирования в pyevolve
"""
def __init__(self, nets, nodes, flows, fflow, texp):
GenomeBase.GenomeBase.__init__(self)
cls_names = masks # cls_ranges.keys()
for n in nets:
if (n[0] not in cls_names) or (n[1] not in directions):
raise ValueError(n)
net_range = xrange(len(nets))
for n in nodes:
if n not in net_range:
raise ValueError(n)
node_range = len(nodes)
for f in flows:
if not isinstance(f, Flow):
raise ValueError(str(f))
if f.node1 >= node_range or f.node2 >= node_range:
raise ValueError
if (type(fflow) != FFlow) or (type(texp) != float):
raise ValueError
self.nets = nets
self.nodes = nodes
self.flows = flows
self.fflow = fflow
self.texp = texp
# ссылки на ФРВ всех вложенных потоков для ускорения доступа
self.fxs = []
for f in flows:
self.fxs += f.fxs
# полный геном организма
self.genome = self.fxs + [self.fflow]
self.initializator.set(network_initializer)
# В сети может мутировать:
# 1) какой либо поток (в т.ч добавиться новый или пропасть старый)
# 2) время жизни
# 3) какой либо узел (сменить сеть, добавиться, удалиться)
# 4) какая-либо сеть (в т.ч. добавиться или удалиться)
self.mutator.setRandomApply(True) # произвольным образом выбирается только один из мутаторов
self.mutator.set(network_mutator)
self.mutator.add(node_mutator)
self.mutator.add(texp_mutator)
self.mutator.add(flow_mutator)
self.mutator.add(fflow_mutator)
self.crossover.set(network_crossover)
self.evaluator.set(network_packets_count_tester)
def __repr__(self):
return str(self.texp) + '||' + str(self.fflow) + '||' + str(self.nets) + '||' + str(self.nodes) + '||' + str(
self.flows)
# Реализация контракта pyevolve
def copy(self, g):
g.nets = self.nets[:]
g.nodes = self.nodes[:]
g.flows = map(lambda fl: fl.clone(), self.flows)
g.fflow = self.fflow.clone()
g.texp = self.texp
# ссылки на ФРВ всех вложенных потоков для ускорения доступа
g.fxs = []
for f in g.flows:
g.fxs += f.fxs
# полный геном организма
g.genome = g.fxs + [g.fflow]
# Реализация контракта pyevolve
def clone(self):
if not all(n < len(self.nets) for n in self.nodes):
raise ValueError
clone = NetworkGenome(self.nets, self.nodes, self.flows, self.fflow, self.texp)
self.copy(clone)
return clone
def delete_node(genome, index):
check_genome(genome)
flows = []
nodes = genome.nodes
nets = []
for f in genome.flows:
if f.node1 != index and f.node2 != index:
flows.append(f)
# при удалении узла надо поменять индексы в потоках
for f in flows:
if f.node1 > index:
f.node1 -= 1
if f.node2 > index:
f.node2 -= 1
del nodes[index]
# # удаляем неиспользуемые сети
# for net_index in xrange(len(genome.nets)):
# if any(node == net_index for node in nodes):
# nets.append(genome.nets[net_index])
# genome.nets = nets
genome.nodes = nodes
genome.flows = flows
check_genome(genome)
return
def network_mutator(genome, **args):
choice = random.randint(0, len(genome.nets) + 1) if len(genome.nets) != 1 else random.choice(
xrange(len(genome.nets) + 1))
if choice < len(genome.nets):
if random.randint(0, 1):
genome.nets[choice] = (random.choice(masks), genome.nets[choice][1])
else:
genome.nets[choice] = (genome.nets[choice][0], random.choice(directions))
elif choice == len(genome.nets):
genome.nets.append((random.choice(masks), random.choice(directions)))
else:
net_to_del = random.choice(xrange(len(genome.nets)))
# вместе с сетью нужно удалить узлы и потоки
while any(net_index == net_to_del for net_index in genome.nodes):
next_index = next(
node_index for node_index in xrange(len(genome.nodes)) if genome.nodes[node_index] == net_to_del)
delete_node(genome, next_index)
for n in xrange(len(genome.nodes)):
if genome.nodes[n] > net_to_del:
genome.nodes[n] -= 1
del genome.nets[net_to_del]
check_genome(genome)
return 1
def node_mutator(genome, **args):
choice = random.randint(0, len(genome.nodes) + 1) if len(genome.nodes) != 1 else random.choice(
xrange(len(genome.nodes) + 1))
if choice < len(genome.nodes):
old_net = genome.nodes[choice]
while genome.nodes[choice] == old_net:
genome.nodes[choice] = random.choice(xrange(len(genome.nets)))
elif choice == len(genome.nodes):
genome.nodes.append(random.choice(xrange(len(genome.nets))))
else:
delete_node(genome, random.choice(xrange(len(genome.nodes))))
check_genome(genome)
return 1
def get_random_texp():
return random.random() * 100
def texp_mutator(genome, **args):
old = genome.texp
while old == genome.texp:
genome.texp = get_random_texp()
check_genome(genome)
return 1
def fflow_mutator(genome, **kwargs):
genome.fflow.mutation()
check_genome(genome)
return 1
def flow_mutator(genome, **args):
choice = random.randint(0, len(genome.flows) + 1)
if choice < len(genome.flows):
genome.flows[choice].mutation()
elif choice == len(genome.flows):
genome.flows.append(
random_flow(random.randint(0, len(genome.nodes) - 1), random.randint(0, len(genome.nodes) - 1)))
else:
del genome.flows[random.randint(0, len(genome.flows) - 1)]
check_genome(genome)
return 1
def network_crossover(genome, **args):
g_mom = args["mom"]
g_dad = args["dad"]
sister = g_mom.clone()
brother = g_dad.clone()
sister.resetStats()
brother.resetStats()
if random.randint(0, 1):
sister.fflow, brother.fflow = brother.fflow, sister.fflow
if random.randint(0, 1):
sister.texp, brother.texp = brother.texp, sister.texp
# одноточечный кроссовер функций распределения
cross = random.randint(0, min(len(sister.flows), len(brother.flows)) - 2) if min(len(sister.flows),
len(brother.flows)) > 2 else 0
# формируем геном сестры
s_flows = sister.flows[:cross] + brother.flows[cross:]
sister.nets, sister.nodes = translate_nodes_and_nets(s_flows, sister.nodes, brother.nodes, sister.nets,
brother.nets, lambda x: 's' if x < cross else 'b')
sister.flows = s_flows
# формируем геном брата
b_flows = brother.flows[:cross] + sister.flows[cross:]
brother.nets, brother.nodes = translate_nodes_and_nets(b_flows, sister.nodes, brother.nodes, sister.nets,
brother.nets, lambda x: 'b' if x < cross else 's')
brother.flows = b_flows
check_genome(sister)
check_genome(brother)
return sister, brother
def translate_nodes_and_nets(flows, sister_nodes, brother_nodes, sister_nets, brother_nets, lambda_flag):
b_nodes = []
b_nets = []
node_dictionary = []
# транслируем узлы в новые
for i in xrange(len(flows)):
flag = lambda_flag(i)
if [flows[i].node1, flag] not in node_dictionary:
node_dictionary.append([flows[i].node1, flag])
flows[i].node1 = node_dictionary.index([flows[i].node1, flag])
if [flows[i].node2, flag] not in node_dictionary:
node_dictionary.append([flows[i].node2, flag])
flows[i].node2 = node_dictionary.index([flows[i].node2, flag])
net_dictionary = []
# копируем сетки для узлов
for i in xrange(len(node_dictionary)):
flag = node_dictionary[i][1]
nodes = sister_nodes if flag == 's' else brother_nodes
nets = sister_nets if flag == 's' else brother_nets
old_index = nodes[node_dictionary[i][0]]
net = nets[old_index]
if [old_index, flag] not in net_dictionary:
net_dictionary.append([old_index, flag])
b_nets.append(net)
b_nodes.append(net_dictionary.index([old_index, flag]))
if any(item >= len(b_nets) for item in b_nodes) or len(b_nodes) == 0 or len(b_nets) == 0:
raise ValueError
if any(f.node1 >= len(b_nodes) or f.node2 >= len(b_nodes) for f in flows):
raise ValueError
return b_nets, b_nodes
def network_initializer(genome, **args):
"""
Функция создания новой произвольнй сети
"""
nets = []
for net in xrange(random.randint(1, 10)):
nets.append((random.choice(masks), random.choice(directions)))
nodes = []
for node in xrange(random.randint(1, 100)):
nodes.append(random.choice(xrange(len(nets))))
flows = []
for f in xrange(random.randint(1, 10)):
flows.append(random_flow(random.choice(xrange(len(nodes))), random.choice(xrange(len(nodes)))))
fflow = FFlow().random_initialize()
texp = get_random_texp()
genome = NetworkGenome(nets, nodes, flows, fflow, texp)
check_genome(genome)
return genome
def network_random_tester(genome):
score = 0.0
score = float(random.random(100))
return score
def check_genome(genome):
if any(node >= len(genome.nets) for node in genome.nodes):
raise ValueError
if any(f.node1 >= len(genome.nodes) or f.node2 >= len(genome.nodes) for f in genome.flows):
raise ValueError
return