-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerateFT.py
171 lines (158 loc) · 7.12 KB
/
generateFT.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
# -*- coding: utf-8 -*-
"""
@author: M Nauta
"""
import random
import json
import numpy as np
import pandas as pd
import math
import itertools
def createdataset(tree):
#set BEs to True or False
values = dict()
for key in tree:
node = tree[key]
probability = random.uniform(0.2, 0.5)
if node[1]=='BE':
values[key] = random.random() < probability
# read logic to set gates to True or False
while len(values.keys())<len(tree.keys()):
for key in tree:
if key not in values.keys():
node=tree[key]
gatetype = node[1]
if gatetype!='BE':
children=node[0]
allchildrenset = True
for c in children:
if c not in values.keys():
allchildrenset = False
#If all children of the node have a value, then set value of that parent node as well
if allchildrenset == True:
if gatetype=='AND':
alltrue = True
for c in children:
if values[c]==False:
alltrue=False
if alltrue:
values[key]=True
else:
values[key]=False
elif gatetype=='OR':
notrue = True
for c in children:
if values[c]==True:
notrue=False
if notrue==False:
values[key]=True
else:
values[key]=False
return values
def createallcombinations(tree, dataset):
for gatekey in tree:
outputnode = tree[gatekey]
if outputnode[1]!='BE':
inputchildren = outputnode[0]
combinations = list(itertools.product([0, 1], repeat=len(inputchildren)))
for combi in combinations:
values=dict()
for i in range(len(combi)):
values[inputchildren[i]]=bool(combi[i])
while len(values.keys())<len(tree.keys()):
for key in tree:
if key not in values.keys():
node=tree[key]
gatetype = node[1]
if gatetype=='BE':
#check value of parent
for parent in tree:
parentchildren = tree[parent][0]
if key in parentchildren:
#parent found
if parent in values.keys():
v = values[parent]
if v:
values[key]=True
else:
values[key]=False
else: #parent not yet valued, set BE to false
values[key]=False
else:
children=node[0]
allchildrenset = True
for c in children:
if c not in values.keys():
allchildrenset = False
#If all children of the node have a value, then set value of that parent node as well
if allchildrenset == True:
if gatetype=='AND':
alltrue = True
for c in children:
if values[c]==False:
alltrue=False
if alltrue:
values[key]=True
else:
values[key]=False
elif gatetype=='OR':
notrue = True
for c in children:
if values[c]==True:
notrue=False
if notrue==False:
values[key]=True
else:
values[key]=False
dataset.append(list(values.values()))
return dataset
def createnoisedrow(tree):
#set BEs to True or False
values = dict()
events = tree.keys()
for key in tree:
node = tree[key]
probability = random.uniform(0.2, 0.5)
if node[1]=='BE':
values[key] = random.random() < probability
# read logic to set gates to True or False
while len(values.keys())<len(tree.keys()):
for key in tree:
if key not in values.keys():
node=tree[key]
gatetype = node[1]
if gatetype!='BE':
children=node[0]
allchildrenset = True
for c in children:
if c not in values.keys():
allchildrenset = False
#If all children of the node have a value, then set value of that parent node as well
if allchildrenset == True:
if gatetype=='AND':
alltrue = True
for c in children:
if values[c]==False:
alltrue=False
if alltrue:
values[key]=True
else:
values[key]=False
elif gatetype=='OR':
notrue = True
for c in children:
if values[c]==True:
notrue=False
if notrue==False:
values[key]=True
else:
values[key]=False
eventstochange = random.sample(events, 2)
for e in eventstochange:
values[e]=not values[e]
return values
def createrandomvariable(nrrows):
columnvector = []
for i in range(nrrows):
columnvector.append(bool(random.getrandbits(1))) #choose random boolean
return columnvector