-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
377 lines (241 loc) · 12.8 KB
/
Code.py
File metadata and controls
377 lines (241 loc) · 12.8 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
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
print("Network written to Network.xdsl")
print("Inferences are successfully done")
#Description :This code is written with a python wrapper of a C++ library called " Structural Modeling, Inference and Learning Engine(SMILE) it contains two classes, one for the creation of a Bayesian network that models the causal relationships between metacognitive skills and their influence on the level of self-regulation. The other class is for the modification of the values of each evidence and the beliefs update (Bayesian inference). It is necessary to import a "pysmile" package from the site https://www.bayesfusion.com/
import pysmile
import pysmile_license
class CreatingBN:
def __init__(self):
net = pysmile.Network()
#Defining of nodes
lof = self.create_cpt_node(net,"Level_of_SelfRegulation", "Level of Self-regulation",["Bad","Intermediate","Good"],200, 40)
eng = self.create_fcpt_node(net,"Engage_in_Learning_Goals", "Engage in Learning Goals",["NotLearned","InProgress","Learned"],160, 48)
eval = self.create_cpt_node(net,"MonitorEvaluateProgress", "Evaluate and Monitor the progress",["NotLearned","InProgress","Learned"],110, 140)
under = self.create_cpt_node(net,"Understand_and_describe_Goals_Plan", "Understand and describe Goals ( Plan )",["NotLearned","InProgress","Learned"],111, 160)
mang = self.create_cpt_node(net,"Manage_the_obstacles_Challenges", "Manage the obstacles/Challenges",["NotLearned","InProgress","Learned"],98, 170)
res = self.create_cpt_node(net,"ResourceManagement", "Manage the Resources",["NotLearned","InProgress","Learned"],141, 180)
htd = self.create_cpt_node(net,"HowToDo", "Execute Tasks (How To Do)",["NotLearned","InProgress","Learned"],181, 98)
task = self.create_cpt_node(net,"Identify_and_Descibe_tasks", "Identify and Descibe tasks",["NotLearned","InProgress","Learned"],171, 150)
react = self.create_cpt_node(net,"React_to_different_situations_Adaptability", "React to different situations / Adaptability",["NotLearned","InProgress","Learned"],45, 180)
#Defining of arcs
net.add_arc(eng, lof)
net.add_arc(eval, lof)
net.add_arc(under, eng)
net.add_arc(eng, task)
net.add_arc(react, task)
net.add_arc(task, res)
net.add_arc(res, htd)
net.add_arc(htd, mang)
net.add_arc(mang, eval)
#Defining of Conditional Probability Table (CPT) for each node
underCPT = [
0.091856061, # P(under=N)
0.38162879, # P(under=I)
0.52651515 # P(under=L)
]
net.set_node_definition(under, underCPT)
reactCPT = [
0.1174242424242424, # P(react=N)
0.4839015151515152, # P(react=I)
0.3986742424242424 # P(react=L)
]
net.set_node_definition(react, reactCPT)
evalCPT = [
#--------State=NotLearned
0.27693333, # P(eval=N|mang=N)
0.229771138561716, # P(eval=N|mang=I)
0.1561223732085839, # P(eval=N|mang=L)
#--------State=InProgress
0.4649333333333333, # P(eval=I|mang=N)
0.5256631236234798, # P(eval=I|mang=I)
0.6753916982252913, # P(eval=I|mang=L)
#--------State=Learned
0.2581333333333333, # P(eval=L|mang=N)
0.2445657378148041, # P(eval=L|mang=I)
0.1684859285661246 # P(eval=L|mang=L)
]
net.set_node_definition(eval, evalCPT)
engCPT = [
#--------State=NotLearned
0.3482304422842233, # P(eng=N|under=N)
0.324035516777932, # P(eng=N|under=I)
0.2131292517006803, # P(eng=N|under=L)
#--------State=InProgress
0.3258847788578882, # P(eng=I|under=N)
0.4216625906096459, # P(eng=I|under=I)
0.4341496598639455, # P(eng=I|under=L)
#--------State=Learned
0.3258847788578882, # P(eng=L|under=N)
0.2543018926124221, # P(eng=L|under=I)
0.3527210884353741 # P(eng=L|under=L)
]
net.set_node_definition(eng, engCPT)
mangCPT = [
#--------State=NotLearned
0.2606293333333333, # P(mang=N|htd=N)
0.2230315643426986, # P(mang=N|htd=I)
0.2613190058755145, # P(mang=N|htd=L)
#--------State=InProgress
0.3424213333333334, # P(mang=I|htd=N)
0.3057578910856746, # P(mang=I|htd=I)
0.3693404970622428, # P(mang=I|htd=L)
#--------State=Learned
0.3969493333333333, # P(mang=L|htd=N)
0.4712105445716268, # P(mang=L|htd=I)
0.3693404970622428 # P(mang=L|htd=L)
]
net.set_node_definition(mang, mangCPT)
htdCPT = [
#--------State=NotLearned
0.2606293333333333, # P(htd=N|res=N)
0.2230315643426986, # P(htd=N|res=I)
0.2613190058755145, # P(htd=N|res=L)
#--------State=InProgress
0.3424213333333334, # P(htd=I|res=N)
0.3057578910856746, # P(htd=I|res=I)
0.3693404970622428, # P(htd=I|res=L)
#--------State=Learned
0.3969493333333333, # P(htd=L|res=N)
0.4712105445716268, # P(htd=L|res=I)
0.3693404970622428 # P(htd=L|res=L)
]
net.set_node_definition(htd, htdCPT)
taskCPT = [
#--------State=NotLearned
0.3333333333333333, # P(task=N|eng=N|react=N)
0.3062973222530009, # P(task=N|eng=N|react=I)
0.3145333333333334, # P(task=N|eng=N|react=L)
0.3406584362139918, # P(task=N|eng=I|react=N)
0.4130527210884354, # P(task=N|eng=I|react=I)
0.2988014714607808, # P(task=N|eng=I|react=L)
0.3626337448559671, # P(task=N|eng=L|react=N)
0.4161735700197238, # P(task=N|eng=L|react=I)
0.3265743305632502, # P(task=N|eng=L|react=L)
#--------State=InProgress
0.3333333333333333, # P(task=I|eng=N|react=N)
0.3468513388734995, # P(task=I|eng=N|react=I)
0.3145333333333334, # P(task=I|eng=N|react=L)
0.3406584362139918, # P(task=I|eng=I|react=N)
0.3333333333333333, # P(task=I|eng=I|react=I)
0.4196629880147146, # P(task=I|eng=I|react=L)
0.3186831275720165, # P(task=I|eng=L|react=N)
0.3274161735700197, # P(task=I|eng=L|react=I)
0.3468513388734996, # P(task=I|eng=L|react=L)
#--------State=Learned
0.3333333333333333, # P(task=L|eng=N|react=N)
0.3468513388734995, # P(task=L|eng=N|react=I)
0.3709333333333333, # P(task=L|eng=N|react=L)
0.3186831275720165, # P(task=L|eng=I|react=N)
0.2536139455782313, # P(task=L|eng=I|react=I)
0.2815355405245045, # P(task=L|eng=I|react=L)
0.3186831275720165, # P(task=L|eng=L|react=N)
0.2564102564102564, # P(task=L|eng=L|react=I)
0.3265743305632502 # P(task=L|eng=L|react=L)
]
net.set_node_definition(task, taskCPT)
resCPT = [
#--------State=NotLearned
0.4023970570784384, # P(res=N|task=N)
0.4053476607911521, # P(res=N|task=I)
0.3549490802238055, # P(res=N|task=L)
#--------State=InProgress
0.3505992642696095, # P(res=I|task=N)
0.351336915197788, # P(res=I|task=I)
0.3117175864428612, # P(res=I|task=L)
#--------State=Learned
0.2470036786519521, # P(res=L|task=N)
0.2433154240110598, # P(res=L|task=I)
0.3333333333333334, # P(res=L|task=L)
]
net.set_node_definition(res, resCPT)
lofCPT = [
#--------State=Bad
0.1, # P(lof=B|eval=N|eng=N)
0.3, # P(lof=B|eval=N|eng=I)
0.0, # P(lof=B|eval=N|eng=L)
0.1, # P(lof=B|eval=I|eng=N)
0.1, # P(lof=B|eval=I|eng=I)
0.1, # P(lof=B|eval=I|eng=L)
0.1, # P(lof=B|eval=L|eng=N)
0.1, # P(lof=B|eval=L|eng=I)
0.1, # P(lof=B|eval=L|eng=L)
#--------State=Intermediate
0.5, # P(lof=I|eval=N|eng=N)
0.4, # P(lof=I|eval=N|eng=I)
0.6, # P(lof=I|eval=N|eng=L)
0.5, # P(lof=I|eval=I|eng=N)
0.5, # P(lof=I|eval=I|eng=I)
0.6, # P(lof=I|eval=I|eng=L)
0.6, # P(lof=I|eval=L|eng=N)
0.6, # P(lof=I|eval=L|eng=I)
0.6, # P(lof=I|eval=L|eng=L)
#--------State=Good
0.4, # P(lof=G|eval=N|eng=N)
0.3, # P(lof=G|eval=N|eng=I)
0.4, # P(lof=G|eval=N|eng=L)
0.4, # P(lof=G|eval=I|eng=N)
0.4, # P(lof=G|eval=I|eng=I)
0.3, # P(lof=G|eval=I|eng=L)
0.3, # P(lof=G|eval=L|eng=N)
0.3, # P(lof=G|eval=L|eng=I)
0.3 # P(lof=G|eval=L|eng=L)
]
net.set_node_definition(lof, lofCPT)
#save the file "Network.xdsl" in the current directory
net.write_file("Network.xdsl")
print("Network written to Network.xdsl")
def create_cpt_node(self, net, id, name, outcomes, x_pos, y_pos):
handle = net.add_node(pysmile.NodeType.CPT, id)
net.set_node_name(handle, name)
net.set_node_position(handle, x_pos, y_pos, 85, 55)
initial_outcome_count = net.get_outcome_count(handle)
for i in range(0, initial_outcome_count):
net.set_outcome_id(handle, i, outcomes[i])
for i in range(initial_outcome_count, len(outcomes)):
net.add_outcome(handle, outcomes[i])
return handle
#---------------------------------------------------------------------
#performs the series of inference calls
class InferenceCalls:
def __init__(self):
net = pysmile.Network()
# load the network created in the class CreatingBN
net.read_file("Network.xdsl")
#Update the probabilities and display them using the method print_all_posteriors() defined below
print("Posteriors with no evidence set:")
net.update_beliefs()
self.print_all_posteriors(net)
#Fix the state of each node using its id
print("Setting Engage_in_Learning_Goals =Learned.")
self.change_evidence_and_update(net, "Engage_in_Learning_Goals", "Learned")
print("MonitorEvaluateProgress=InProgress.")
self.change_evidence_and_update(net, "MonitorEvaluateProgress", "InProgress")
print("Changing MonitorEvaluateProgress to NotLearned, Keeping Engage_in_Learning_Goals=Learned.")
self.change_evidence_and_update(net, "MonitorEvaluateProgress", "NotLearned")
print("Removing evidence from Engage_in_Learning_Goals , keeping MonitorEvaluateProgress=NotLearned.")
self.change_evidence_and_update(net, "Engage_in_Learning_Goals", None)
print("Inferences are successfully done")
#Definig the used methods
def print_posteriors(self, net, node_handle):
node_id = net.get_node_id(node_handle)
if net.is_evidence(node_handle):
#print(node_id + " has evidence set (" +net.get_outcome_id(node_handle, net.get_evidence(node_handle)) + ")")
ListOfEvidencesStates = Liste (net.get_outcome_id(node_handle, net.get_evidence(node_handle)))
posteriors1 = net.get_node_value(node_handle)
for i in range(0, len(posteriors1)):
print("P(" + node_id + "|" + ListOfEvidencesStates +")=" + str(posteriors1[i]))
else:
posteriors2 = net.get_node_value(node_handle)
for i in range(0, len(posteriors2)):
print("P(" + node_id + "=" + net.get_outcome_id(node_handle, i) +")=" + str(posteriors2[i]))
def print_all_posteriors(self, net):
handle = net.get_first_node()
while (handle >= 0):
self.print_posteriors(net, handle)
handle = net.get_next_node(handle)
def change_evidence_and_update(self, net, node_id, outcome_id):
if outcome_id is not None:
net.set_evidence(node_id, outcome_id)
else:
net.clear_evidence(node_id)
net.update_beliefs()
self.print_all_posteriors(net)