-
Notifications
You must be signed in to change notification settings - Fork 1
/
Boolean_Ananlysis.py
334 lines (315 loc) · 12.1 KB
/
Boolean_Ananlysis.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
325
326
327
328
329
330
from sys import argv
import re
import copy
import datetime
import profile
import random
import operator
# read and tokenize the Boolean model
Boolean_model = open(argv[2], 'r')
all_nodes = [] # list of all nodes in system
initial_states = {} # initial states for each node
function_nodes = {} # list of nodes incident on each node (Boolean function variables)
functions = {} # tokenized list of all nodes, operations, and parentheses in each function
in_rules = False
for line in Boolean_model: # checks for 'rules' text block between triple quotes
if re.match('.*"""', line,) and in_rules == False:
in_rules = True
if re.match('"""', line,) and in_rules == True:
in_rules = False
if in_rules == True: # extract tokens for each Boolean function
if re.match('.*\*.*', line):
line = line.replace('(', '( ')
line = line.replace(')', ' )')
for dNode in all_nodes:
if re.match('.*' + str(dNode) + '\*.*', line):
node_list = []
token_list = []
fuction_list = []
for iNode in all_nodes:
if re.match('.*\s*' + str(iNode) + '(\s.*|$)', line):
node_list.append(iNode)
token_list = re.split(' ', line.rstrip('\n'))
function_nodes[dNode] = node_list
index_of_eq = 0
for index, token in enumerate(token_list):
if token == '=':
index_of_eq = index
function_list = token_list[index_of_eq+1:]
functions[dNode] = function_list
else: # extract initial states
if line.rstrip('\n')[-4:] == 'True' or line.rstrip('\n')[-5:] == 'False' or line.rstrip('\n')[-6:] == 'Random':
line_list = re.split('\s*=\s*', line.rstrip('\n'))
initial = line_list[-1]
line_list = line_list[:-1]
for node in line_list:
initial_states[node] = initial
all_nodes.append(node)
def BoolFuncCalc(node, function, currentState): ###### Boolean in 1's and 0's
func = function[:]
for i,each in enumerate(func):
for item in currentState:
if each == item[0]:
func[i] = item[1]
expression = " ".join(map(str, func))
value = str(int(eval(expression)))
for item in currentState:
if node == item[0]:
item[1] = value
def GeneralAsynchronous(state, rounds): # 1 round is one node evaluation
nodes = all_nodes[:]
trace = []
lenN = len(all_nodes)
currentState = [None]*len(initial_states)
for i,each in enumerate(all_nodes):
currentState[i] = [all_nodes[i], str(int(eval(initial_states[each])))]
trace.append(copy.deepcopy(currentState))
round = 0
while round < rounds:
node = random.choice(nodes)
BoolFuncCalc(node, functions[node], currentState)
trace.append(copy.deepcopy(currentState))
round += 1
return trace
output = GeneralAsynchronous(initial_states, 10)
for each in output:
print each
# def FindAttractors(): # synchronous; no memory reduction
#
# lenN = len(all_nodes)
# attractors = []
# statelist2 = []
# for i in range(2**lenN):
# statelist2.append(None)
# attractor = 0
# k = 0
# while k < len(statelist2):
# if statelist2[k] == None:
# statelist2[k] = 0
# state = bin(k)[2:].rjust(lenN, '0')
# currentState = [None]*len(initial_states)
# updateState = [None]*len(initial_states)
# for i,each in enumerate(all_nodes):
# currentState[i] = [all_nodes[i], state[i]]
# updateState[i] = [all_nodes[i], state[i]]
# # updateState = copy.deepcopy(currentState)
# currentRun = []
# currentRun.append(int(state, 2))
# end = False
# while end == False:
# state = ""
# for node,function in functions.items():
# BoolFuncCalc(node, function, currentState, updateState)
# # currentState = copy.deepcopy(updateState)
# currentState[0][:],currentState[1][:],currentState[2][:] = updateState[0][:],updateState[1][:],updateState[2][:]
# for each in currentState:
# state = state+each[1]
# currentRun.append(int(state, 2))
# if statelist2[int(state, 2)] == 0:
# attractor += 1
# for every in currentRun:
# statelist2[every] = attractor
# end = True
# temp = []
# for each in currentRun:
# if each in temp:
# attractors.append([attractor, 0, currentRun[temp.index(each)+1:]])
# else:
# temp.append(each)
#
#
# if statelist2[int(state, 2)] == None:
# statelist2[int(state, 2)] = 0
# else:
# for every in currentRun:
# statelist2[every] = statelist2[int(state, 2)]
# end = True
# attractors[statelist2[k]-1][1] += 1
# k += 1
#
# print 'Statelist'
# for i,each in enumerate(statelist2):
# print i, each
# print
#
# print 'Attractors'
# for each in attractors:
# print each
#
# # FindAttractors()
#
# print
# print '-----------------------------'
# print
#
# def FindAttractorsCompressed(): # synchronous; with memory reduction
#
# lenN = len(all_nodes)
# stateN = 2**lenN
# statelist = []
# compressedlist = []
# templist = {}
# attractors = []
# attractor = 0
# k = 0
# s = 0
# while k < stateN:
# statelist.append([k, None, templist.get(k)])
# if statelist[k+s][2] == None:
# statelist[k+s][2] = 0
# state = bin(k)[2:].rjust(lenN, '0')
# currentState = [None]*len(initial_states)
# updateState = [None]*len(initial_states)
# for i,each in enumerate(all_nodes):
# currentState[i] = [all_nodes[i], state[i]]
# updateState[i] = [all_nodes[i], state[i]]
# # updateState = copy.deepcopy(currentState)
# currentRun = []
# currentRun.append(int(state, 2))
# end = False
# while end == False:
# state = ""
# for node,function in functions.items():
# BoolFuncCalc(node, function, currentState, updateState)
# # currentState = copy.deepcopy(updateState)
# currentState[0][:],currentState[1][:],currentState[2][:] = updateState[0][:],updateState[1][:],updateState[2][:]
# for each in currentState:
# state = state+each[1]
# currentRun.append(int(state, 2))
# if int(state, 2) <= k:
# if statelist[int(state, 2)+s][2] == 0:
# attractor += 1
# for every in currentRun:
# if every <= k:
# statelist[every+s][2] = attractor
# else:
# templist[every] = attractor
# end = True
# temp = []
# for each in currentRun:
# if each in temp:
# attractors.append([attractor, 0, currentRun[temp.index(each)+1:]])
# else:
# temp.append(each)
# if statelist[int(state, 2)+s][2] == None:
# statelist[int(state, 2)][2] = 0
# else:
# for every in currentRun:
# if every <= k:
# statelist[every+s][2] = statelist[int(state, 2)+s][2]
# else:
# templist[every] = statelist[int(state, 2)][2]
# end = True
# else:
# if templist.get(int(state, 2)) == 0:
# attractor += 1
# for every in currentRun:
# if every <= k:
# statelist[every+s][2] = attractor
# else:
# templist[every] = attractor
# end = True
# temp = []
# for each in currentRun:
# if each in temp:
# attractors.append([attractor, 0, currentRun[temp.index(each)+1:]])
# else:
# temp.append(each)
# if templist.get(int(state, 2)) == None:
# templist[int(state, 2)] = 0
# else:
# for every in currentRun:
# if every <= k:
# statelist[every+s][2] = templist[int(state, 2)]
# else:
# templist[every] = templist[int(state, 2)]
# end = True
#
# if k > 0 and statelist[k+s][2] == statelist[k-1+s][2]:
# statelist[k-1+s][1] = statelist[k+s][0]
# del statelist[k+s]
# s -= 1
# else:
# del templist[k]
# if k > 0 and statelist[k+s][2] == statelist[k-1+s][2]:
# statelist[k-1+s][1] = statelist[k+s][0]
# del statelist[k+s]
# s -= 1
#
# attractors[statelist[k+s][2]-1][1] += 1
# k += 1
# for i in range(len(statelist)):
# if statelist[i][1] == None:
# statelist[i][1] = statelist[i][0]
#
# print 'Statelist'
# for each in statelist:
# print each
#
# print
# # for each in templist:
# # print each, templist[each]
#
# print 'Attractors'
# for each in attractors:
# print each
#
# # FindAttractorsCompressed()
#
# def Synchronous(state):
#
# lenN = len(all_nodes)
# attractor = []
# currentState = [None]*len(initial_states)
# for i,each in enumerate(all_nodes):
# currentState[i] = [all_nodes[i], state[i]]
# updateState = copy.deepcopy(currentState)
# currentRun = []
# currentRun.append(int(state, 2))
# end = False
# while end == False:
# state = ""
# for node,function in functions.items():
# BoolFuncCalc(node, function, currentState)
# currentState = copy.deepcopy(updateState)
# for each in currentState:
# state = state+each[1]
# currentRun.append(int(state, 2))
#
# temp = []
# for each in currentRun:
# if each in temp:
# attractor.append(currentRun[temp.index(each)+1:])
# end = True
# print currentRun
# else:
# temp.append(each)
#
# print 'Attractor'
# for each in attractor:
# print each
#
# # Synchronous('010')
#
# def RandomAsynchronous(state, rounds): # 1 round is each node once
#
# nodes = all_nodes[:]
# print all_nodes
#
# lenN = len(all_nodes)
# currentState = [None]*len(initial_states)
# for i,each in enumerate(all_nodes):
# currentState[i] = [all_nodes[i], state[i]]
# updateState = copy.deepcopy(currentState)
#
# round = 0
# while round < rounds:
# random.shuffle(nodes)
# print nodes
# for node in nodes:
# BoolFuncCalc(node, functions[node], currentState)
# print currentState
#
# round += 1
#
# # RandomAsynchronous('001', 10)