-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayes_net_bak.cpp
More file actions
604 lines (555 loc) · 25.6 KB
/
bayes_net_bak.cpp
File metadata and controls
604 lines (555 loc) · 25.6 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
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
#include "dlib/bayes_utils.h"
#include "dlib/graph_utils.h"
#include "dlib/graph.h"
#include "dlib/directed_graph.h"
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include "rapidxml/rapidxml.hpp"
using namespace rapidxml;
using namespace dlib;
using namespace std;
#include "bayes_net.h"
// ----------------------------------------------------------------------------------------
char* bayesian_test(char * passed_courses) {
// There are many useful convenience functions in this namespace. They all
// perform simple access or modify operations on the nodes of a bayesian network.
// You don't have to use them but they are convenient and they also will check for
// various errors in your bayesian network when your application is built with
// the DEBUG or ENABLE_ASSERTS preprocessor definitions defined. So their use
// is recommended. In fact, most of the global functions used in this example
// program are from this namespace.
using namespace bayes_node_utils;
// This statement declares a bayesian network called bn. Note that a bayesian network
// in the dlib world is just a directed_graph object that contains a special kind
// of node called a bayes_node.
directed_graph<bayes_node>::kernel_1a_c bn;
// Use an enum to make some more readable names for our nodes.
enum nodes { cs110, cs201, cs207, cs115, math105, math110, cs270, cs100, engl100, math122, math111, cs261, cs280, cs301, cs203, cs271 };
//enum for the array of difficulty levels
enum difficulties { easy, middle, hard };
//set the pass rate for each difficulty
float difficulties[3];
difficulties[easy] = 0.8;
difficulties[middle] = 0.7;
difficulties[hard] = 0.6;
// The next few blocks of code setup our bayesian network.
// The first thing we do is tell the bn object how many nodes it has
// and also add the three edges. Again, we are using the network
// shown in ASCII art at the top of this file.
bn.set_number_of_nodes(16);
bn.add_edge(cs110, cs201);
bn.add_edge(cs110, cs207);
bn.add_edge(cs110, cs115);
bn.add_edge(math110, cs115);
bn.add_edge(math105, cs115);
bn.add_edge(cs110, cs270);
bn.add_edge(cs100, cs270);
bn.add_edge(engl100, cs270);
bn.add_edge(math122, cs261);
bn.add_edge(math111, cs261);
bn.add_edge(cs110, cs261);
bn.add_edge(cs115, cs261);
bn.add_edge(engl100, cs280);
bn.add_edge(cs110, cs280);
bn.add_edge(cs201, cs301);
bn.add_edge(cs115, cs203);
bn.add_edge(cs110, cs271);
bn.add_edge(cs270, cs271);
// Now we inform all the nodes in the network that they are binary
// nodes. That is, they only have two possible values.
set_node_num_values(bn, cs110, 2);
set_node_num_values(bn, cs201, 2);
set_node_num_values(bn, cs207, 2);
set_node_num_values(bn, cs115, 2);
set_node_num_values(bn, math105, 2);
set_node_num_values(bn, math110, 2);
set_node_num_values(bn, cs270, 2);
set_node_num_values(bn, cs100, 2);
set_node_num_values(bn, engl100, 2);
set_node_num_values(bn, math122, 2);
set_node_num_values(bn, math111, 2);
set_node_num_values(bn, cs261, 2);
set_node_num_values(bn, cs280, 2);
set_node_num_values(bn, cs301, 2);
set_node_num_values(bn, cs203, 2);
set_node_num_values(bn, cs271, 2);
assignment parent_state;
// Now we will enter all the conditional probability information for each node.
// Each node's conditional probability is dependent on the state of its parents.
// To specify this state we need to use the assignment object. This assignment
// object allows us to specify the state of each nodes parents.
//==============================p(cs110=1) = 0.5==============================
// parent_state is empty in this case since cs110 is a root node.
set_node_probability(bn, cs110, 1, parent_state, 0.5);
set_node_probability(bn, cs110, 0, parent_state, 1-0.5);
//==============================p(cs100=1) = 0.5==============================
set_node_probability(bn, cs100, 1, parent_state, 0.5);
set_node_probability(bn, cs100, 0, parent_state, 1-0.5);
//==============================p(engl100=1) = 0.5==============================
set_node_probability(bn, engl100, 1, parent_state, 0.5);
set_node_probability(bn, engl100, 0, parent_state, 1-0.5);
//==============================p(math105=1) = 0.5==============================
set_node_probability(bn, math105, 1, parent_state, 0.5);
set_node_probability(bn, math105, 0, parent_state, 1-0.5);
//==============================p(math110=1) = 0.5==============================
set_node_probability(bn, math110, 1, parent_state, 0.5);
set_node_probability(bn, math110, 0, parent_state, 1-0.5);
//==============================p(math111=1) = 0.5==============================
set_node_probability(bn, math111, 1, parent_state, 0.5);
set_node_probability(bn, math111, 0, parent_state, 1-0.5);
//==============================p(math122=1) = 0.5==============================
set_node_probability(bn, math122, 1, parent_state, 0.5);
set_node_probability(bn, math122, 0, parent_state, 1-0.5);
// This is our first node that has parents. So we set the parent_state
// object to reflect that cs201 has cs110 as parents.
parent_state.add(cs110, 1);
//==============================p(cs201=1 | cs110=1) = 0.8==============================
set_node_probability(bn, cs201, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs201, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 0;
//==============================p(cs201=1 | cs110=0) = 0==============================
set_node_probability(bn, cs201, 1, parent_state, 0);
set_node_probability(bn, cs201, 0, parent_state, 1-0);
//==============================p(cs207 | cs110) CPT==============================
parent_state.clear();
parent_state.add(cs110,1);
set_node_probability(bn, cs207, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs207, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 0;
set_node_probability(bn, cs207, 1, parent_state, 0);
set_node_probability(bn, cs207, 0, parent_state, 1-0);
//==================p(cs115 | cs110, math105, math110) CPT=====================
parent_state.clear();
parent_state.add(cs110,1);
parent_state.add(math105, 1);
parent_state.add(math110, 1);
set_node_probability(bn, cs115, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs115, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 1;
parent_state[math105] = 1;
parent_state[math110] = 0;
set_node_probability(bn, cs115, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs115, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 1;
parent_state[math105] = 0;
parent_state[math110] = 1;
set_node_probability(bn, cs115, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs115, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 1;
parent_state[math105] = 0;
parent_state[math110] = 0;
set_node_probability(bn, cs115, 1, parent_state, 0);
set_node_probability(bn, cs115, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[math105] = 1;
parent_state[math110] = 1;
set_node_probability(bn, cs115, 1, parent_state, 0);
set_node_probability(bn, cs115, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[math105] = 1;
parent_state[math110] = 0;
set_node_probability(bn, cs115, 1, parent_state, 0);
set_node_probability(bn, cs115, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[math105] = 0;
parent_state[math110] = 1;
set_node_probability(bn, cs115, 1, parent_state, 0);
set_node_probability(bn, cs115, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[math105] = 0;
parent_state[math110] = 0;
set_node_probability(bn, cs115, 1, parent_state, 0);
set_node_probability(bn, cs115, 0, parent_state, 1-0);
//==================p(cs270 | cs110, cs100, engl100) CPT=====================
parent_state.clear();
parent_state.add(cs110,1);
parent_state.add(cs100, 1);
parent_state.add(engl100, 1);
set_node_probability(bn, cs270, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs270, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 1;
parent_state[cs100] = 1;
parent_state[engl100] = 0;
set_node_probability(bn, cs270, 1, parent_state, 0);
set_node_probability(bn, cs270, 0, parent_state, 1-0);
parent_state[cs110] = 1;
parent_state[cs100] = 0;
parent_state[engl100] = 1;
set_node_probability(bn, cs270, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs270, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 1;
parent_state[cs100] = 0;
parent_state[engl100] = 0;
set_node_probability(bn, cs270, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs270, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 0;
parent_state[cs100] = 1;
parent_state[engl100] = 1;
set_node_probability(bn, cs270, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs270, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 0;
parent_state[cs100] = 1;
parent_state[engl100] = 0;
set_node_probability(bn, cs270, 1, parent_state, 0);
set_node_probability(bn, cs270, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[cs100] = 0;
parent_state[engl100] = 1;
set_node_probability(bn, cs270, 1, parent_state, 0);
set_node_probability(bn, cs270, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[cs100] = 0;
parent_state[engl100] = 0;
set_node_probability(bn, cs270, 1, parent_state, 0);
set_node_probability(bn, cs270, 0, parent_state, 1-0);
//==================p(cs261 | math111, math122, cs110, cs115) CPT=====================
parent_state.clear();
parent_state.add(math111,1);
parent_state.add(math122,1);
parent_state.add(cs110,1);
parent_state.add(cs115,1);
set_node_probability(bn, cs261, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs261, 0, parent_state, 1-difficulties[easy]);
parent_state[math111] = 1;
parent_state[math122] = 1;
parent_state[cs110] = 1;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs261, 0, parent_state, 1-difficulties[easy]);
parent_state[math111] = 1;
parent_state[math122] = 1;
parent_state[cs110] = 0;
parent_state[cs115] = 1;
set_node_probability(bn, cs261, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs261, 0, parent_state, 1-difficulties[easy]);
parent_state[math111] = 1;
parent_state[math122] = 1;
parent_state[cs110] = 0;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 1;
parent_state[math122] = 0;
parent_state[cs110] = 1;
parent_state[cs115] = 1;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 1;
parent_state[math122] = 0;
parent_state[cs110] = 1;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 1;
parent_state[math122] = 0;
parent_state[cs110] = 0;
parent_state[cs115] = 1;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 1;
parent_state[math122] = 0;
parent_state[cs110] = 0;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 1;
parent_state[cs110] = 1;
parent_state[cs115] = 1;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 1;
parent_state[cs110] = 1;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 1;
parent_state[cs110] = 0;
parent_state[cs115] = 1;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 1;
parent_state[cs110] = 0;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 0;
parent_state[cs110] = 1;
parent_state[cs115] = 1;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 0;
parent_state[cs110] = 1;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 0;
parent_state[cs110] = 0;
parent_state[cs115] = 1;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
parent_state[math111] = 0;
parent_state[math122] = 0;
parent_state[cs110] = 0;
parent_state[cs115] = 0;
set_node_probability(bn, cs261, 1, parent_state, 0);
set_node_probability(bn, cs261, 0, parent_state, 1-0);
//===========================p(cs280 | cs110, engl100) CPT==========================
parent_state.clear();
parent_state.add(cs110,1);
parent_state.add(engl100,1);
set_node_probability(bn, cs280, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs280, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 1;
parent_state[engl100] = 0;
set_node_probability(bn, cs280, 1, parent_state, 0);
set_node_probability(bn, cs280, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[engl100] = 1;
set_node_probability(bn, cs280, 1, parent_state, 0);
set_node_probability(bn, cs280, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[engl100] = 0;
set_node_probability(bn, cs280, 1, parent_state, 0);
set_node_probability(bn, cs280, 0, parent_state, 1-0);
//===========================p(cs301 | cs201) CPT==========================
parent_state.clear();
parent_state.add(cs201,1);
set_node_probability(bn, cs301, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs301, 0, parent_state, 1-difficulties[easy]);
parent_state[cs201] = 0;
set_node_probability(bn, cs301, 1, parent_state, 0);
set_node_probability(bn, cs301, 0, parent_state, 1-0);
//===========================p(cs203 | cs115) CPT==========================
parent_state.clear();
parent_state.add(cs115,1);
set_node_probability(bn, cs203, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs203, 0, parent_state, 1-difficulties[easy]);
parent_state[cs115] = 0;
set_node_probability(bn, cs203, 1, parent_state, 0);
set_node_probability(bn, cs203, 0, parent_state, 1-0);
//===========================p(cs271 | cs110, cs270) CPT==========================
parent_state.clear();
parent_state.add(cs110,1);
parent_state.add(cs270,1);
set_node_probability(bn, cs271, 1, parent_state, difficulties[easy]);
set_node_probability(bn, cs271, 0, parent_state, 1-difficulties[easy]);
parent_state[cs110] = 1;
parent_state[cs270] = 0;
set_node_probability(bn, cs271, 1, parent_state, 0);
set_node_probability(bn, cs271, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[cs270] = 1;
set_node_probability(bn, cs271, 1, parent_state, 0);
set_node_probability(bn, cs271, 0, parent_state, 1-0);
parent_state[cs110] = 0;
parent_state[cs270] = 0;
set_node_probability(bn, cs271, 1, parent_state, 0);
set_node_probability(bn, cs271, 0, parent_state, 1-0);
// We have now finished setting up our bayesian network. So lets compute some
// probability values. The first thing we will do is compute the prior probability
// of each node in the network. To do this we will use the join tree algorithm which
// is an algorithm for performing exact inference in a bayesian network.
// First we need to create an undirected graph which contains set objects at each node and
// edge. This long declaration does the trick.
typedef set<unsigned long>::compare_1b_c set_type;
typedef graph<set_type, set_type>::kernel_1a_c join_tree_type;
join_tree_type join_tree;
// Now we need to populate the join_tree with data from our bayesian network. The next
// function calls do this. Explaining exactly what they do is outside the scope of this
// example. Just think of them as filling join_tree with information that is useful
// later on for dealing with our bayesian network.
create_moral_graph(bn, join_tree);
create_join_tree(join_tree, join_tree);
// Now that we have a proper join_tree we can use it to obtain a solution to our
// bayesian network. Doing this is as simple as declaring an instance of
// the bayesian_network_join_tree object as follows:
bayesian_network_join_tree solution(bn, join_tree);
// now print out the probabilities for each node
printf("Using the join tree algorithm, prior:\n");
cout << "p(cs110=1) = " << solution.probability(cs110)(1) << endl;
cout << "p(math105=1) = " << solution.probability(math105)(1) << endl;
cout << "p(math110=1) = " << solution.probability(math110)(1) << endl;
cout << "p(cs201=1) = " << solution.probability(cs201)(1) << endl;
cout << "p(cs207=1) = " << solution.probability(cs207)(1) << endl;
cout << "p(cs115=1) = " << solution.probability(cs115)(1) << endl;
cout << "p(engl100=1) = " << solution.probability(engl100)(1) << endl;
cout << "p(cs100=1) = " << solution.probability(cs100)(1) << endl;
cout << "p(cs270=1) = " << solution.probability(cs270)(1) << endl;
cout << "p(math111=1) = " << solution.probability(math111)(1) << endl;
cout << "p(math122=1) = " << solution.probability(math122)(1) << endl;
cout << "p(cs261=1) = " << solution.probability(cs261)(1) << endl;
cout << "p(cs280=1) = " << solution.probability(cs280)(1) << endl;
cout << "p(cs301=1) = " << solution.probability(cs301)(1) << endl;
cout << "p(cs203=1) = " << solution.probability(cs203)(1) << endl;
cout << "p(cs271=1) = " << solution.probability(cs271)(1) << endl;
cout << "\n\n\n";
// Now to make things more interesting lets say that we have discovered that the cs110
// node really has a value of 1. That is to say, we now have evidence that
// cs110 is 1. We can represent this in the network using the following two function
// calls.
// cs110 = 0,
// cs201 = 1,
// cs207 = 2,
// cs115 = 3,
// math105 = 4,
// math110 = 5,
// cs270 = 6,
// cs100 = 7,
// engl100 = 8,
// math122 = 9,
// math111 = 10,
// cs261 = 11,
// cs280 = 12,
// cs301 = 13,
// cs203 = 14,
// cs271 = 15
//set leaf nodes to be 0:
set_node_value(bn, cs110, 0);
set_node_as_evidence(bn, cs110);
set_node_value(bn, cs100, 0);
set_node_as_evidence(bn, cs100);
set_node_value(bn, engl100, 0);
set_node_as_evidence(bn, engl100);
set_node_value(bn, math105, 0);
set_node_as_evidence(bn, math105);
set_node_value(bn, math110, 0);
set_node_as_evidence(bn, math110);
set_node_value(bn, math111, 0);
set_node_as_evidence(bn, math111);
set_node_value(bn, math122, 0);
set_node_as_evidence(bn, math122);
//update evidence according to user input
if (strstr(passed_courses, "-0-") != NULL) {
set_node_value(bn, cs110, 1);
set_node_as_evidence(bn, cs110);
}
if (strstr(passed_courses, "-1-") != NULL) {
set_node_value(bn, cs201, 1);
set_node_as_evidence(bn, cs201);
}
if (strstr(passed_courses, "-2-") != NULL) {
set_node_value(bn, cs207, 1);
set_node_as_evidence(bn, cs207);
}
if (strstr(passed_courses, "-3-") != NULL) {
set_node_value(bn, cs115, 1);
set_node_as_evidence(bn, cs115);
}
if (strstr(passed_courses, "-4-") != NULL) {
set_node_value(bn, math105, 1);
set_node_as_evidence(bn, math105);
}
if (strstr(passed_courses, "-5-") != NULL) {
set_node_value(bn, math110, 1);
set_node_as_evidence(bn, math110);
}
if (strstr(passed_courses, "-6-") != NULL) {
set_node_value(bn, cs270, 1);
set_node_as_evidence(bn, cs270);
}
if (strstr(passed_courses, "-7-") != NULL) {
set_node_value(bn, cs100, 1);
set_node_as_evidence(bn, cs100);
}
if (strstr(passed_courses, "-8-") != NULL) {
set_node_value(bn, engl100, 1);
set_node_as_evidence(bn, engl100);
}
if (strstr(passed_courses, "-9-") != NULL) {
set_node_value(bn, math122, 1);
set_node_as_evidence(bn, math122);
}
if (strstr(passed_courses, "-10-") != NULL) {
set_node_value(bn, math111, 1);
set_node_as_evidence(bn, math111);
}
if (strstr(passed_courses, "-11-") != NULL) {
set_node_value(bn, cs261, 1);
set_node_as_evidence(bn, cs261);
}
if (strstr(passed_courses, "-12-") != NULL) {
set_node_value(bn, cs280, 1);
set_node_as_evidence(bn, cs280);
}
if (strstr(passed_courses, "-13-") != NULL) {
set_node_value(bn, cs301, 1);
set_node_as_evidence(bn, cs301);
}
if (strstr(passed_courses, "-14-") != NULL) {
set_node_value(bn, cs203, 1);
set_node_as_evidence(bn, cs203);
}
if (strstr(passed_courses, "-15-") != NULL) {
set_node_value(bn, cs271, 1);
set_node_as_evidence(bn, cs271);
}
// Now we want to compute the probabilities of all the nodes in the network again
// given that we now know that cs110 is 1. We can do this as follows:
bayesian_network_join_tree solution_with_evidence(bn, join_tree);
// now print out the probabilities for each node
cout << "Using the join tree algorithm, post:\n";
cout << "p(cs110=1) = " << solution_with_evidence.probability(cs110)(1) << endl;
cout << "p(math105=1) = " << solution_with_evidence.probability(math105)(1) << endl;
cout << "p(math110=1) = " << solution_with_evidence.probability(math110)(1) << endl;
cout << "p(cs201=1) = " << solution_with_evidence.probability(cs201)(1) << endl;
cout << "p(cs207=1) = " << solution_with_evidence.probability(cs207)(1) << endl;
cout << "p(cs115=1) = " << solution_with_evidence.probability(cs115)(1) << endl;
cout << "p(engl100=1) = " << solution_with_evidence.probability(engl100)(1) << endl;
cout << "p(cs100=1) = " << solution_with_evidence.probability(cs100)(1) << endl;
cout << "p(cs270=1) = " << solution_with_evidence.probability(cs270)(1) << endl;
cout << "p(math111=1) = " << solution_with_evidence.probability(math111)(1) << endl;
cout << "p(math122=1) = " << solution_with_evidence.probability(math122)(1) << endl;
cout << "p(cs261=1) = " << solution_with_evidence.probability(cs261)(1) << endl;
cout << "p(cs280=1) = " << solution_with_evidence.probability(cs280)(1) << endl;
cout << "p(cs301=1) = " << solution_with_evidence.probability(cs301)(1) << endl;
cout << "p(cs203=1) = " << solution_with_evidence.probability(cs203)(1) << endl;
cout << "p(cs271=1) = " << solution_with_evidence.probability(cs271)(1) << endl;
cout << "\n\n\n";
// Note that when we made our solution_with_evidence object we reused our join_tree object.
// This saves us the time it takes to calculate the join_tree object from scratch. But
// it is important to note that we can only reuse the join_tree object if we haven't changed
// the structure of our bayesian network. That is, if we have added or removed nodes or
// edges from our bayesian network then we must recompute our join_tree. But in this example
// all we did was change the value of a bayes_node object (we made node cs110 be evidence)
// so we are ok.
//cout<<passed_courses<<endl;
ostringstream result_str;
result_str << "{ cs110:"
<< solution_with_evidence.probability(cs110)(1) << ", "
<< "math105:" << solution_with_evidence.probability(math105)(1)<< ", "
<< "math110:" << solution_with_evidence.probability(math110)(1)<< ", "
<< "cs201:" << solution_with_evidence.probability(cs201)(1)<< ", "
<< "cs207:" << solution_with_evidence.probability(cs207)(1)<< ", "
<< "cs115:" << solution_with_evidence.probability(cs115)(1)<< ", "
<< "engl100:" << solution_with_evidence.probability(engl100)(1)<< ", "
<< "cs100:" << solution_with_evidence.probability(cs100)(1)<< ", "
<< "cs270:" << solution_with_evidence.probability(cs270)(1)<< ", "
<< "math111:" << solution_with_evidence.probability(math111)(1)<< ", "
<< "math122:" << solution_with_evidence.probability(math122)(1)<< ", "
<< "cs261:" << solution_with_evidence.probability(cs261)(1)<< ", "
<< "cs280:" << solution_with_evidence.probability(cs280)(1)<< ", "
<< "cs301:" << solution_with_evidence.probability(cs301)(1)<< ", "
<< "cs203:" << solution_with_evidence.probability(cs203)(1)<< ", "
<< "cs271:" << solution_with_evidence.probability(cs271)(1)
<< "}";
cout<<"bayes_net.cpp:"<<endl;
cout<< result_str.str().c_str()<<endl;
//ostringstream s;
//s << 123791842.5;
//cout<<s.str()<<endl;
//char aCString[result_str.str().size()];
//strcpy(aCString, result_str.str().c_str());
//return aCString;
char* aCString = (char *) malloc(result_str.str().length()+1);
strncpy(aCString, result_str.str().c_str(), result_str.str().length() + 1);
return aCString;
}