-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecisionNets.py
661 lines (538 loc) · 17.4 KB
/
DecisionNets.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import getopt
import sys
import copy
import formatter
import itertools
import operator
def get_input_file():
opts, args = getopt.getopt(sys.argv[1:], "i:")
if len(opts) != 0:
return opts[0][1]
else:
return "./input.txt"
rfilepath = get_input_file()
rfile = open(rfilepath)
wfile = open("output.txt","w")
query_var =[]
cpt = []
dict_to_sort = {}
maininputlist = []
query_var1 = []
utility_list = []
cpt_dict ={}
Main_dict ={}
dict_to_sort = {}
dec_node = []
ord_list = []
dict_sort =[]
query_var1 = []
abc = ''
query_dict = {}
dec_node_list_wo_util = []
pval = 0
event_dict = {}
CPDict = {}
EUDict = []
ordered_list_2 = []
ordered_list_3 = []
maxDictMEU ={}
class bNetwork:
def __init__(self, Parent, table):
self.Parent = copy.deepcopy(Parent)
self.table = copy.deepcopy(table)
inputfile = rfile.readlines()
def create_inputfile(inputfile):
maininputlist = [x.strip() for x in inputfile]
return maininputlist
maininputlist = create_inputfile(inputfile)
for i in maininputlist:
if (i == "******\n"):
break
query_var.append(str(i))
flag = True
while (flag == True):
mark = True
mark_til = False
flag = False
ramp = 1
if ramp == 1:
while (flag == True):
mark = True
mark_til = False
for j in maininputlist:
if j != "******" and mark and not mark_til:
query_var1.append(str(j))
elif j == "******" and mark and not mark_til:
mark = False
elif j == "******" and not mark and not mark_til:
mark_til = True
elif j != "******" and not mark and mark_til:
utility_list.append(j)
utility_list[:1][0] =utility_list[:1][0].replace("\n", "")
else:
abc = abc + j + '\n'
peqere = abc
key =[]
parent = []
xyz = peqere.split("***")
def removeparenthesis(seg):
while '' in seg:
seg.remove('')
return seg
for seg in xyz:
cpt_dict ={}
seg =seg.split("\n")
seg = removeparenthesis(seg)
j = seg[0].split(' | ')
key = j[0]
if len(j) > 1:
parent = j[1].split(' ')
dict_to_sort[key] = parent
else:
dict_to_sort[key] = []
for y in range(1, len(seg)):
if seg[y] != "decision":
seg[y] =seg[y].split(" ")
strtemp = ""
for i in range(1,len(seg[y])):
strtemp = strtemp + (seg[y][i])
cpt_dict[strtemp] = float(seg[y][0])
else:
dec_node.append(seg[y-1])
parent = []
cpt_dict={}
Main_dict[key] = bNetwork(parent,cpt_dict)
if utility_list != []:
utility_list[0]= utility_list[0].split(' | ')
utility_list[0][-1].strip()
key = utility_list[0][0]
parent = utility_list[0][1]
parent = parent.replace("\n", "")
parent = parent.replace(" ", "")
parents=[]
for i in parent:
parents.append(i)
#print parent
dict_to_sort[key] = parents
tempdiut = {}
for i in range(1, len(utility_list)):
strtemp1 = ""
utility_list[i] = utility_list[i].split(' ')
for z in range(1,len(utility_list[i])):
strtemp1 = strtemp1 + utility_list[i][z]
tempdiut[strtemp1] = utility_list[i][0]
Main_dict[key]= bNetwork(parents,tempdiut)
def topological_sort():
global dict_to_sort
global ord_list
while(len(dict_to_sort) != 0):
for item in dict_to_sort.keys():
if dict_to_sort[item] == []:
var = item
ord_list.append(var)
del dict_to_sort[item]
for values in dict_to_sort.values():
if var in values:
values.remove(var)
return ord_list
dict_sort = topological_sort()
def order_list():
dec_node_list = []
for i in dec_node:
for x in i:
dec_node_list.append(x)
for i in dec_node_list:
dec_node_list_wo_util.append(i)
dec_node_list.append("utility")
ord_list1 = dict_sort
for z in dec_node_list:
if z in dict_sort:
ord_list1.remove(z)
return ord_list1
ordered_list_2 = order_list()
def ret_bla(bla):
return bla
def ret_blas(bla):
return 1- bla
def calprob(saa, node):
global Main_dict
if not Main_dict[node].Parent:
bla = Main_dict[node].table[""]
if saa[node]!= "+":
return ret_blas(bla)
else:
return ret_bla(bla)
else:
pick = ""
for sels in Main_dict[node].Parent:
pick = pick + saa[sels]
bla = Main_dict[node].table[pick]
if saa[node] != "+":
return 1-bla
else:
return bla
def joint(query_d,count):
global pval
global ordered_list_2
global ordered_list_3
Flag = False
if count ==len(ordered_list_3):
return 1
elif (query_d[ordered_list_3[count]] != "&"):
if Flag == False:
pval = calprob(query_d, ordered_list_3[count])
return pval * joint(query_d,count + 1)
else:
upc = True
while upc == True:
if Flag == False:
z1 = copy.deepcopy(query_d)
z1[ordered_list_3[count]] = "+"
pvalz1 = calprob(z1, ordered_list_3[count])
upc = False
puc = False
while puc == False:
if Flag == False:
z2 = copy.deepcopy(query_d)
z2[ordered_list_3[count]] = "-"
pvalz2 = calprob(z2, ordered_list_3[count])
puc = True
return (pvalz1*joint(z1,count + 1)+(pvalz2*joint(z2, count + 1)))
def strips(check,query_var1):
Flag = True
if Flag == True:
query_var1 = query_var1
check = check
return query_var_list
def cond_prob(event_d, query_d,count,yek):
global CPDict
global ordered_list_2
global ordered_list_3
n = len(event_d)
if n == len(yek):
#print ":: ", yek, ":", query_d
numerator = joint(query_d,0)
CPDict[yek] = numerator
else:
sap1 = copy.deepcopy(query_d)
sap1[event_d[count]] = "+"
cond_prob(event_d,sap1,count+1, yek + "+")
sap2 = copy.deepcopy(query_d)
sap2[event_d[count]] = "-"
cond_prob(event_d,sap2,count+1, yek + "-")
def cond_prob_sol(eve_list):
t = 0
num = ""
for rt in CPDict.keys():
t = t + CPDict[rt]
for rt in CPDict.keys():
if rt == eve_list:
num = CPDict[rt]
Answer = float(float((num))/float((t)))
return Answer
def EU_prob(event_d, query_d,count,yek):
global CPDict
global ordered_list_2
global ordered_list_3
numerator = joint(query_d,0)
n = len(event_d)
shlag = True
round = False
round1 = False
if n == len(yek):
CPDict[yek] = numerator
else:
while round==False:
if shlag == True:
sap1 = copy.deepcopy(query_d)
sap1[event_d[count]] = "+"
EU_prob(event_d,sap1,count+1,yek + "+")
round = True
while round1 == False:
if shlag == True:
sap2 = copy.deepcopy(query_d)
sap2[event_d[count]] = "-"
EU_prob(event_d,sap2,count+1, yek + "-")
round1 = True
def GKFEU(query_dict,util_parents,count,key):
global CPDict
global dec_node
global EUDict
if len(util_parents) == len(key):
EUDict.append(key)
elif util_parents[count] in dec_node:
GKFEU(query_dict,util_parents,count+1,key + query_dict[util_parents[count]])
else:
GKFEU(query_dict,util_parents, count+1, key + "+")
GKFEU(query_dict,util_parents,count+1, key + "-")
return EUDict
def EU_prob_sol(util_parents,query_dict):
global CPDict
t = 0
nt = 0
EUList = GKFEU(query_dict,util_parents,0,"")
for a in EUList:
nt = nt + (CPDict[a]*float(tempdiut[a]))
t = t + CPDict[a]
nt = nt/t
return (nt)
def MEU_Prob_sol(quey, ut_pt, qua):
t= 0
nt = 0
global maxDictMEU
global EUDict
EUDict = []
EUdiction = []
global CPDict
EUdiction = GKFEU(quey, ut_pt, 0, "")
for a in EUdiction:
nt = nt + (CPDict[a]* float(tempdiut[a]))
t = t + CPDict[a]
nt = nt/t
x = nt
maxDictMEU[qua]= x
return maxDictMEU
def onesinglefunction(query,evidence,count, k):
createMEU(query, evidence, count + 1, k + "+")
createMEU(query, evidence, count + 1, k + "-")
def createMEQuery_Dict(query_list2,MEUQueryDict):
for x in query_list2:
x = x.split("=")
MEUQueryDict[x[0]] = x[1]
return MEUQueryDict
def copy_quer_evidence(query, evidence):
query1 = copy.deepcopy(query)
evidence1 = copy.deepcopy(evidence)
return query1, evidence1
def createMEU(query, evidence,count, k):
query_MEU = []
query_MEUstr = ""
Evidence_MEU = []
Evidence_MEUstr =""
MEUQueryDict ={}
vrsti = ""
c = True
seeds = ""
query1,evidence1 = copy_quer_evidence(query,evidence)
while c == True:
if len(query) == len(k):
for i in range(0, len(query)):
query1[i] = query1[i] + "=" + k[i]
for x in query1:
vrsti = vrsti + x + ","
vrsti = vrsti[:-1]
if len(evidence1)>0:
seeds = evidence1[0]
vrsti = vrsti + "|" + seeds
beta = copy.deepcopy(vrsti)
beta = beta.split("|")
beta[0] = beta[0].split(",")
for x in beta[0]:
x = x.split("=")
query_MEU.append(x[0])
query_MEUstr = query_MEUstr + x[1]
if len(beta) > 1:
beta[1] = beta[1].split(",")
for x in beta[1]:
x = x.split("=")
Evidence_MEU.append(x[0])
Evidence_MEUstr = Evidence_MEUstr + x[1]
vrsti = vrsti.replace("|",",")
vrsti = vrsti.split(",")
query_list2 = vrsti
createMEQuery_Dict(query_list2,MEUQueryDict)
for k in ord_list:
#for k in ordered_list:
if k!="utility":
if k not in MEUQueryDict.keys():
MEUQueryDict[k] = "&"
counter = True
while counter == True:
util_parent = Main_dict['utility'].Parent
EU_prob(util_parent,MEUQueryDict,0,"")
MEU_Prob_sol(MEUQueryDict,util_parent,query_MEUstr)
counter = False
else:
onesinglefunction(query,evidence,count, k)
c = False
return
def create_quer_evidence(check):
global ordered_list_3
evidence = []
event = []
check = check.replace(" ", "")
check = check.split("(")
check[1] = check[1].strip(")")
beta = copy.deepcopy(check[1])
beta = beta.split("|")
quer = beta[0].split(",")
if len(beta) > 1:
evidence.append(beta[1])
return quer, evidence
def print_final_solution_MEU():
x = max(maxDictMEU.iteritems(), key=operator.itemgetter(1))[0]
abs = ""
abc = ""
finalstr = ""
abc1 = ""
abc1 = int(round((maxDictMEU[x])))
abc1 = str(abc1)
abc = str(x)
for pqr in range(0, len(abc)):
abs = abs + abc[pqr] + " "
abs = abs.strip(" ")
abs = abs + " " + abc1
finalstr += str(abs) + '\n'
print "This is MEU Sol"
print finalstr
wfile.write(str(finalstr))
def QueryInput():
global event_dict
global query_dict
global query_var_list
global ord_list
global maxDictMEU
list =[]
listed =[]
global CPDict
finalstr = ""
global ordered_list_3
for check in query_var1:
query_dict = {}
list =[]
listed =[]
query_var_list = []
maxDictMEU ={}
if check in query_var1:
if check[0] == 'P' and "|" not in check:
check = check.replace(" ", "")
check = check.split("(")
check[1] = check[1].strip(")")
check[1] = check[1].replace("|", ",")
check[1] = check[1].split(",")
query_var_list = check[1]
for q in query_var_list:
q = q.split("=")
query_dict[q[0]] = q[1]
list.append(q[0])
ordered_list_3 = copy.deepcopy(ordered_list_2)
index = []
if len(ordered_list_3) > 15:
for u in list:
if u in ordered_list_3:
index.append(ordered_list_3.index(u))
ind = max(index)
del ordered_list_3[ind + 1 :]
for i in ordered_list_3:
listed.append(i)
for item in listed:
if item not in list:
query_dict[item] = "&"
a = joint(query_dict, 0)
print "This sol is for Joint"
print '{:.2f}'.format(round(a,2))
wfile.write(str(format(float(round(a, 2)),".2f"))+'\n')
elif check[0] == "P" and "|" in check :
CPDict={}
event_dict = {}
var = check
check = check.replace(" ", "")
check = check.split("(")
check[1] = check[1].strip(")")
check[1] = check[1].replace("|", ",")
check[1] = check[1].split(",")
query_var_list = check[1]
for q in query_var_list:
q = q.split("=")
query_dict[q[0]] = q[1]
list.append(q[0])
var = var.replace(" ", "")
var = var.replace("P(", "")
var = var.replace(")", "")
var = var.split("|")
var[0] = var[0].split(",")
vare = var
eve_list = []
eve_val = ""
for seed in vare[0]:
seed = seed.split("=")
eve_list.append(seed[0])
eve_val += seed[1]
ordered_list_3 = copy.deepcopy(ordered_list_2)
index = []
if len(ordered_list_3) > 15:
for u in eve_list:
if u in ordered_list_3:
index.append(ordered_list_3.index(u))
ind = max(index)
del ordered_list_3[ind+1 :]
print ordered_list_3
for i in ordered_list_3:
listed.append(i)
for item in listed:
if item not in list:
query_dict[item] = "&"
b = cond_prob(eve_list,query_dict,0,"")
sol = cond_prob_sol(eve_val)
print ("This Solution is for CP")
print (round(sol,2))
wfile.write(str(format(float(round(sol, 2)),".2f"))+'\n')
elif check[0] == "E":
floating_pt = True
CPDict = {}
EUDict = []
query_dict = {}
rave = check
check = check.replace("EU(", "")
check = check.strip(")")
check = check.replace(" ", "")
check = check.replace("|", ",")
check = check.split(",")
query_var_list = check
while (floating_pt != False):
if floating_pt == True:
for q in query_var_list:
q = q.split("=")
query_dict[q[0]] = q[1]
list.append(q[0])
ordered_list_3 = copy.deepcopy(ordered_list_2)
index = []
if len(ordered_list_3) > 15:
for u in list:
if u in ordered_list_3:
index.append(ordered_list_3.index(u))
ind = max(index)
ordered_list_3 = ordered_list_3[ind + 1:]
for i in ordered_list_3:
listed.append(i)
if floating_pt == True:
for item in ord_list:
if item not in list:
query_dict[item] = "&"
floating_pt = False
util_parents = []
temp = utility_list[0][1]
temp = temp.strip(" ")
for i in temp:
if i != " ":
util_parents.append(i)
c = EU_prob(util_parents, query_dict, 0, "")
sol = EU_prob_sol(util_parents, query_dict)
print round(sol,2)
wfile.write(str(int(round(sol)))+'\n')
elif check[0] == "M":
floating_pt = True
if floating_pt == True:
quer, evidence = create_quer_evidence(check)
ordered_list_3 = copy.deepcopy(ordered_list_2)
index = []
if len(ordered_list_3) > 15:
for u in quer:
if u in ordered_list_3:
index.append(ordered_list_3.index(u))
ind = max(index)
del ordered_list_3[ind + 1:]
createMEU(quer, evidence,0,"")
print_final_solution_MEU()
wfile.close()
query_input = QueryInput()