-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·97 lines (65 loc) · 2.11 KB
/
Copy pathexample.py
File metadata and controls
executable file
·97 lines (65 loc) · 2.11 KB
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
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
# import pybn library
from pybn import *
# Define a main() function.
def main():
# Create a Network
net = Network('CarProblem')
# Create Node 'Fuel'
Fu = Node('Fu')
# Setting number (and name) of outcomes
Fu.addOutcome('yes')
Fu.addOutcome('no')
# Create node 'Clean Spark Plugs'
SP = Node('SP')
# Setting number (and name) of outcomes
SP.addOutcomes(['yes','no'])
# Create node 'Fuel Meter Standing'
FM = Node('FM')
# Setting number (and name) of outcomes
FM.addOutcomes(['full','half','empty'])
# Create node 'Start'
St = Node('St')
# Setting number (and name) of outcomes
St.addOutcomes(['yes','no'])
# Add arc from 'Fuel' to 'Fuel Meter Standing'
arc_Fu_FM = Arc(Fu,FM)
# Add arc from 'Fuel' to 'Start'
arc_Fu_St = Arc(Fu,St)
# Add arc from 'Clean Spark Plugs' to 'Start'
arc_SP_St = Arc(SP,St)
# Shows the size of the probability matrix 'Start'
# print FM.getTableSize()
# Conditional distribution for node 'Fuel'
Fu.setProbabilities([0.98,0.02])
# Conditional distribution for node 'Clean Spark Plugs'
SP.setProbabilities([0.96,0.04])
# Conditional distribution for node 'Fuel Meter Standing'
FM.setProbabilities([0.39, 0.60, 0.01, 0.001, 0.001, 0.998])
# Conditional distribution for node 'Start'
St.setProbabilities([0.99, 0.01, 0.01, 0.99, 0.0, 1.0, 0.0, 1.0])
# Changing the nodes spacial and visual attributes:
Fu.setNodePosition(100,10)
SP.setNodePosition(300,10)
FM.setNodePosition(0,150)
FM.setInteriorColor('cc99ff')
St.setNodePosition(200,150)
St.setInteriorColor('ff0000')
# Add notes to network
net.addNodes([Fu,SP,FM,St])
# Write file
net.writeFile('CarProblem.xdsl')
# Set evidence
net.setEvidence('FM',2)
net.setEvidence('St',2)
# Compute the beliefs for the network
net.computeBeliefs()
# Print the results for each node
print('Fu', Fu.getBeliefs())
print('SP', SP.getBeliefs())
print('St', St.getBeliefs())
print('FM', FM.getBeliefs())
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()