|
5 | 5 |
|
6 | 6 | import logging
|
7 | 7 | import random
|
| 8 | +import re |
8 | 9 |
|
9 | 10 | LOG = logging.getLogger()
|
10 | 11 |
|
11 | 12 |
|
| 13 | +def _eq_attr(node, attr, gens, container): |
| 14 | + """ |
| 15 | + Calcs fitness based on the fact that node need target node to have an attr |
| 16 | + with a certain value. |
| 17 | + """ |
| 18 | + trg_nd = container.node[gens[node]] |
| 19 | + if attr[0] not in trg_nd: |
| 20 | + return 10.1 |
| 21 | + elif attr[1] != trg_nd[attr[0]]: |
| 22 | + return 10.2 |
| 23 | + return 0.0 |
| 24 | + |
| 25 | + |
| 26 | +def _neq_attr(node, attr, gens, container): |
| 27 | + """ |
| 28 | + Calcs fitness based on the fact that node's target shall not have an attr |
| 29 | + with a certain value. |
| 30 | + """ |
| 31 | + trg_nd = container.node[gens[node]] |
| 32 | + if attr[0] in trg_nd and attr[1] == trg_nd[attr[0]]: |
| 33 | + return 10.1 |
| 34 | + return 0.0 |
| 35 | + |
| 36 | + |
| 37 | +def _lg_attr(node, attr, gens, container): |
| 38 | + """ |
| 39 | + Calcs fitness based on the fact that node's target node shall have an attr |
| 40 | + with a value larger than the given one. |
| 41 | + """ |
| 42 | + trg_nd = container.node[gens[node]] |
| 43 | + if attr[0] not in trg_nd: |
| 44 | + return 10.1 |
| 45 | + elif attr[1] > trg_nd[attr[0]]: |
| 46 | + return 10.2 |
| 47 | + return 0.0 |
| 48 | + |
| 49 | + |
| 50 | +def _lt_attr(node, attr, gens, container): |
| 51 | + """ |
| 52 | + Calcs fitness based on the fact that node's target node shall have an attr |
| 53 | + with a value smaller than the given one. |
| 54 | + """ |
| 55 | + trg_nd = container.node[gens[node]] |
| 56 | + if attr[0] not in trg_nd: |
| 57 | + return 10.1 |
| 58 | + elif attr[1] < trg_nd[attr[0]]: |
| 59 | + return 10.2 |
| 60 | + return 0.0 |
| 61 | + |
| 62 | + |
| 63 | +def _regex_attr(node, attr, gens, container): |
| 64 | + """ |
| 65 | + Calcs fitness based on the fact that node's target node shall have an attr |
| 66 | + with a value smaller than the given one. |
| 67 | + """ |
| 68 | + trg_nd = container.node[gens[node]] |
| 69 | + if attr[0] not in trg_nd: |
| 70 | + return 10.1 |
| 71 | + elif not re.search(attr[1], trg_nd[attr[0]]): |
| 72 | + return 10.2 |
| 73 | + return 0.0 |
| 74 | + |
| 75 | + |
| 76 | +def _same_target(node1, node2, gens): |
| 77 | + """ |
| 78 | + Calcs fitness based on the fact that two nodes should share same target. |
| 79 | + """ |
| 80 | + shared_tg = None |
| 81 | + for src in gens: |
| 82 | + if shared_tg is None and src in [node1, node2]: |
| 83 | + shared_tg = gens[src] |
| 84 | + elif shared_tg is not None and gens[src] != shared_tg: |
| 85 | + return 10.0 |
| 86 | + return 0.0 |
| 87 | + |
| 88 | + |
| 89 | +def _diff_target(node1, node2, gens): |
| 90 | + """ |
| 91 | + Calc fitness based on the fact that two nodes should not share same target. |
| 92 | + """ |
| 93 | + shared_tg = None |
| 94 | + for src in gens: |
| 95 | + if shared_tg is None and src in [node1, node2]: |
| 96 | + shared_tg = gens[src] |
| 97 | + elif shared_tg is not None and gens[src] == shared_tg: |
| 98 | + return 10.0 |
| 99 | + return 0.0 |
| 100 | + |
| 101 | + |
| 102 | +def _share_attr(attrn, node_list, gens, container): |
| 103 | + """ |
| 104 | + Calcs fitness based on the fact that two nodes from the request should be |
| 105 | + stitched to two nodes in the container two share the same attribute |
| 106 | + """ |
| 107 | + attrv = None |
| 108 | + for node in node_list: |
| 109 | + trg = gens[node] |
| 110 | + if attrn not in container.node[trg]: |
| 111 | + return 10.1 |
| 112 | + elif attrv is None: |
| 113 | + attrv = container.node[trg][attrn] |
| 114 | + elif attrv != container.node[trg][attrn]: |
| 115 | + return 10.2 |
| 116 | + return 0.0 |
| 117 | + |
| 118 | + |
| 119 | +def _my_filter(conditions, gens, container): |
| 120 | + """ |
| 121 | + Apply filters. |
| 122 | + """ |
| 123 | + res = 0.0 |
| 124 | + if 'attributes' in conditions: |
| 125 | + for condition in conditions['attributes']: |
| 126 | + para1 = condition[1][0] |
| 127 | + para2 = condition[1][1] |
| 128 | + if condition[0] == 'eq': |
| 129 | + res += _eq_attr(para1, para2, gens, container) |
| 130 | + if condition[0] == 'neq': |
| 131 | + res += _neq_attr(para1, para2, gens, container) |
| 132 | + if condition[0] == 'lg': |
| 133 | + res += _lg_attr(para1, para2, gens, container) |
| 134 | + if condition[0] == 'lt': |
| 135 | + res += _lt_attr(para1, para2, gens, container) |
| 136 | + if condition[0] == 'regex': |
| 137 | + res += _regex_attr(para1, para2, gens, container) |
| 138 | + if 'compositions' in conditions: |
| 139 | + for condition in conditions['compositions']: |
| 140 | + para1 = condition[1][0] |
| 141 | + para2 = condition[1][1] |
| 142 | + if condition[0] == 'same': |
| 143 | + res += _same_target(para1, para2, gens) |
| 144 | + if condition[0] == 'diff': |
| 145 | + res += _diff_target(para1, para2, gens) |
| 146 | + if condition[0] == 'share': |
| 147 | + res += _share_attr(para1, para2, gens, container) |
| 148 | + return res |
| 149 | + |
| 150 | + |
12 | 151 | class Candidate(object):
|
13 | 152 | """
|
14 | 153 | A candidate of a population for an evolutionary algorithm
|
@@ -41,6 +180,83 @@ def crossover(self, partner):
|
41 | 180 | """
|
42 | 181 | raise NotImplementedError('Not done yet.')
|
43 | 182 |
|
| 183 | + def __eq__(self, other): |
| 184 | + return self.gen == other.gen |
| 185 | + |
| 186 | + |
| 187 | +class GraphCandidate(Candidate): |
| 188 | + """ |
| 189 | + Candidate within a population. The DNA of this candidate is defined by a |
| 190 | + dictionary of source to target stitches. |
| 191 | + """ |
| 192 | + |
| 193 | + def __init__(self, gen, stitch, conditions, mutation_list, request, |
| 194 | + container): |
| 195 | + super(GraphCandidate, self).__init__(gen) |
| 196 | + self.stitch = stitch |
| 197 | + self.conditions = conditions |
| 198 | + self.mutation_list = mutation_list |
| 199 | + self.request = request |
| 200 | + self.container = container |
| 201 | + |
| 202 | + def fitness(self): |
| 203 | + fit = 0.0 |
| 204 | + |
| 205 | + # 1. stitch |
| 206 | + for src in self.gen: |
| 207 | + trg = self.gen[src] |
| 208 | + if self.container.node[trg]['type'] != \ |
| 209 | + self.stitch[self.request.node[src]['type']]: |
| 210 | + fit += 100 |
| 211 | + |
| 212 | + # 2. conditions |
| 213 | + fit += _my_filter(self.conditions, self.gen, self.container) |
| 214 | + |
| 215 | + return fit |
| 216 | + |
| 217 | + def mutate(self): |
| 218 | + # let's mutate to an option outside of the shortlisted candidate list. |
| 219 | + src = random.choice(list(self.gen.keys())) |
| 220 | + |
| 221 | + done = False |
| 222 | + cutoff = len(self.gen) |
| 223 | + i = 0 |
| 224 | + while not done and i <= cutoff: |
| 225 | + # break off as there might be no other match available. |
| 226 | + nd_trg = self.mutation_list[random.randint( |
| 227 | + 0, len(self.mutation_list) - 1)][0] |
| 228 | + if self.container.node[nd_trg]['type'] == \ |
| 229 | + self.stitch[self.request.node[src]['type']]: |
| 230 | + done = True |
| 231 | + self.gen[src] = nd_trg |
| 232 | + i += 1 |
| 233 | + |
| 234 | + def crossover(self, partner): |
| 235 | + tmp = {} |
| 236 | + |
| 237 | + for src in self.gen: |
| 238 | + # pick one from partner |
| 239 | + done = False |
| 240 | + nd_trg = '' |
| 241 | + cutoff = len(self.gen) |
| 242 | + i = 0 |
| 243 | + while not done and i <= cutoff: |
| 244 | + nd_trg = random.choice(list(partner.gen.values())) |
| 245 | + if self.container.node[nd_trg]['type'] \ |
| 246 | + == self.stitch[self.request.node[src]['type']]: |
| 247 | + done = True |
| 248 | + i += 1 |
| 249 | + if done: |
| 250 | + tmp[src] = nd_trg |
| 251 | + else: |
| 252 | + tmp[src] = self.gen[src] |
| 253 | + |
| 254 | + return self.__class__(tmp, self.stitch, self.conditions, |
| 255 | + self.mutation_list, self.request, self.container) |
| 256 | + |
| 257 | + def __repr__(self): |
| 258 | + return 'f: ' + str(self.fitness()) + ' - ' + repr(self.gen) |
| 259 | + |
44 | 260 |
|
45 | 261 | class BasicEvolution(object):
|
46 | 262 | """
|
|
0 commit comments