-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromosome.py
79 lines (55 loc) · 2.52 KB
/
chromosome.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
import random
import math
import numpy.random
import scipy.stats
class Chromosome:
def __init__(self):
self.stage_list = []
self.accuracy = None
self.conclusiveness = 0.0
self.time = 0.0
self.inv_time = 0.0
self.rank = 0.0
self.fitness = 0.0
self.hash = None
def __eq__(self, other):
if isinstance(other, Chromosome):
return self.stage_list == other.stage_list and self.rank == other.rank
return False
def __repr__(self):
return ('(acc (g2): %5.3f, coverage (g1): %5.3f, cost (g3*):%5.2f), [Q]: %s'
%(self.accuracy, self.conclusiveness, self.time,
self.stage_list))
def set_fitness(self,alpha,scale=100):
alpha = alpha
weight = 1.0
scale=1
self.fitness = math.sqrt(pow(self.accuracy*scale*weight, 2) + \
pow(self.conclusiveness*scale, 2) + \
pow(self.inv_time*scale, 2))*pow(alpha, self.rank)
def set_hash(self):
assert self.stage_list
stages_str = ''.join(str(stage) for stage in self.stage_list)
self.hash = stages_str
def mutate(self, mutation_prob, max_stages, a=1, b=2):
for index,_ in enumerate(self.stage_list):
stage_index_limit = max(self.stage_list)
if random.uniform(0,1) < mutation_prob:
stage_index_limit = min(stage_index_limit + 1, (max_stages - 1))
# beta binomial distribution with a=1, b>1 has monotonically
# decreasing probability and p(x > stage_index_limit) = 0
distr = scipy.stats.betabinom(stage_index_limit, a, b)
draw = int(numpy.random.choice(range(stage_index_limit+1), 1,
[distr.pmf(x) for x in range(stage_index_limit)]))
self.stage_list[index] = draw
self.realign_stage_list()
def consecutive_stages(self):
return list(set(self.stage_list)) == list(range(0, max(self.stage_list) + 1))
def realign_stage_list(self):
if not self.consecutive_stages():
current_stage = 0
for stage in set(self.stage_list):
for index, value in enumerate(self.stage_list):
if value == stage:
self.stage_list[index] = current_stage
current_stage += 1