-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
119 lines (71 loc) · 3.2 KB
/
main.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
# coding=utf-8
# This is a sample Python script.
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import Inference as I
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print("Hi, {0}".format(name)) # Press ⌘F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
# Memorizzo la rete del file .bn , abrrevio il nome dei nodi alla iniziale
bde = I.BeliefNetwork(["B", "E", "A", "J", "M"], {"B": [], "E": [], "A": ["B", "E"], "J": ["A"],
"M": ["A"]}, {"B": [0.99, 0.01], "E": [0.98, 0.02],
"A":
[0.999, 0.71, 0.06, 0.05, 0.001, 0.29, 0.94, 0.95],
"J": [0.95, 0.1, 0.05, 0.9],
"M": [0.99, 0.3, 0.01, 0.7]})
# Memorizzo la rete simple.bn
simple = I.BeliefNetwork(["A", "B", "C"], {"A": [], "B": ["A"], "C": []},
{"A": [0.3, 0.7], "B": [0.4, 0.6, 0.1, 0.9], "C": [0.4, 0.6, 0.1, 0.9]})
# Memorizzo il Junction Tree del file bde_test.bn , https://github.com/francescobaio/Elaborato_AI/blob/main/JunctionTree%20EarthQuake.bn.png
Jt1 = I.JunctionTree(["AJ", "AM", "ABE"], ["A"],
[("AJ", "A", "AM"), ("AM", "A", "AJ"), ("ABE", "A", "AM"), ("AM", "A", "ABE"), ("ABE", "A", "AJ"),
("AJ", "A", "ABE")], bde)
# Memorizzo il Junction Tree del file simple_bn , https://github.com/francescobaio/Elaborato_AI/blob/main/JunctionTree%20simple.bn.png
Jt2 = I.JunctionTree(["AB", "BC"], ["B"], ["AB", "B", "BC"], simple)
# Esempi relativi all'uso della definizione di probabilità condizionale
# Calcolo della probabilità congiuntà tramite la def. di questa per una Belief NetWork
for i in range(len(Jt1.clusters)):
print(Jt1.CPTc[Jt1.clusters[i]])
print("____")
for i in range(len(bde.nodes)):
print(bde.cpt[bde.nodes[i]])
print("____")
print(Jt1.CPTc["AJ"])
print("____")
Jt1.inizialization()
print("____")
for i in range(len(Jt1.clusters)):
print(Jt1.CPTc[Jt1.clusters[i]])
print("_____")
for i in range(len(Jt1.separators)):
print(Jt1.CPTs[Jt1.separators[i]])
print("_____")
for i in range(len(Jt1.clusters)):
print(Jt1.CPTc[Jt1.clusters[i]])
print("_____")
sum = bde.joint_probability()
print(bde.joint_p)
print(sum)
print("_____")
for i in range(len(Jt1.clusters)):
print(Jt1.CPTc[Jt1.clusters[i]])
print("_____")
for i in range(len(Jt1.clusters)):
print(Jt1.CPTc[Jt1.clusters[i]])
print("_____")
cpt = bde.calculate_cp("B", ["J", "M"], {"J": 1, "M": 0})
print(cpt)
CPT1 = bde.marginalize("B", cpt)
print(CPT1)
print("_____")
Jt1.give_evidence(["J", "M"], {"J": 1, "M": 0})
for i in range(len(Jt1.clusters)):
print(Jt1.CPTc[Jt1.clusters[i]])
Jt1.collect_evidence("ABE")
Jt1.distribute_evidence("ABE")
bcpt = Jt1.marginalize("B")
print(bcpt)