-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeModel.py
108 lines (82 loc) · 3.77 KB
/
LifeModel.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
class TemporalAbstraction:
def __init__(self, f, v, s, e, **kwargs):
self.f = f#kwargs["f"]
self.v = v#kwargs["v"]
self.s = s
self.e = e
self.diagnosis = 0
@property
def Length(self):
return self.e - self.s
def __repr__(self, **kwargs):
return "({0}, {1}, {2}, {3})".format(self.f,self.v,self.s,self.e)
class LifeModel:
def __init__(self, F, V, n=None, future=False):
self.n=n
self.F=F
self.V=V
self.history=not future
def GetMSS (self, Z):
self.Z=Z
if self.history:
sequenceLength=max(E.e for E in Z)#Ending point -> Should become zero
Z_norm=list(map(lambda x:TemporalAbstraction(x.f,x.v,x.s-sequenceLength,x.e-sequenceLength),Z))
DeltaT=-min(E.s for E in Z_norm)#If not sorted, the length of the MSS
else: #Future
sequenceLength=min(E.s for E in Z) # Starting point - > Should become zero
Z_norm=list(map(lambda x:TemporalAbstraction(x.f,x.v,x.s-sequenceLength,x.e-sequenceLength),Z))
DeltaT=max(E.e for E in Z_norm)#If not sorted, the length of the MSS
print ("Z:")
print (Z)
print ("Z norm:")
print (Z_norm)
#Finding k so
if(self.n is None):
n=0
k=1
delta=1#delta
while DeltaT>=2**(delta*k):
n+=1
k=2**n
else:
k=2**self.n
print ("n= {0}, k= {1}".format(n,k))
if self.history==True:
p=[(-2**(k-i)+1,-2**(k-(i+1))+1) for i in range(0,k)]
else:#future
p=[(2**i-1,2**(i+1)-1) for i in range(0,k)]
#print (p)
Matrix = [[[0 for v in range(self.V)] for u in range(self.F)] for m in range (0,k)]
#find list
for i in range(0,k):
In=list(filter(lambda x: ( p[i][0]<=x.s<p[i][1] or p[i][0]<=x.e<p[i][1]), Z_norm))#create in list for all temporal states that either start or end inside current period p_i using "interval comparisson"
Out=list(filter(lambda x: ( x.s<p[i][0] and x.e>=p[i][1]), Z_norm))#create in list for all temporal states that are outside of the current period p_i using "interval comparisson"
pLength=p[i][1]-p[i][0]
#print("In")
#print(p[i])
#print(In)
#print("Out")
#print(p[i])
#print(Out)
#print("##################################")
for Ej in In:
if p[i][0]<=Ej.s<p[i][1] and p[i][0]<=Ej.e<p[i][1]:
#print("Condition In 1: i={0}, Ej.f={1}, Ej.v={2}".format(i,Ej.f,Ej.v))
Matrix[i][Ej.f][Ej.v]+=Ej.Length
Z_norm.remove(Ej)
elif p[i][0]<=Ej.s<p[i][1]:#Ej only starts in p_i
#print("Condition In 2: i={0}, Ej.f={1}, Ej.v={2}", i,Ej.f,Ej.v)
Matrix[i][Ej.f][Ej.v]+=p[i][1]-Ej.s
elif p[i][0]<=Ej.e<p[i][1]:#Ej only ends in p_i
#print("Condition In 2: i={0}, Ej.f={1}, Ej.v={2}", i,Ej.f,Ej.v)
Matrix[i][Ej.f][Ej.v]+=Ej.e-p[i][0]
Z_norm.remove(Ej)#Will not be useful for further periods
for Ej in Out:
#print("Condition Out: i={0}, Ej.f={1}, Ej.v={2}", i,Ej.f,Ej.v)
Matrix[i][Ej.f][Ej.v]+=p[i][1]-p[i][0]#Add pi.length
#print("S{0}=".format(i))
#print(Matrix[i]/pLength)
#Matrix_norm=list(map(lambda x: [matr][] ,Matrix[i]))
#print(Matrix[i])
return Matrix
#implement algorithm