bayes_net_from_disk_ex.cpp 3.12 KB
Newer Older
1
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
2
3
4
5
6
7
8
/*
    This is an example illustrating the use of the Bayesian Network 
    inference utilities found in the dlib C++ library.  In this example
    we load a saved Bayesian Network from disk.  
*/


9
10
11
12
#include <dlib/bayes_utils.h>
#include <dlib/graph_utils.h>
#include <dlib/graph.h>
#include <dlib/directed_graph.h>
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
#include <iostream>
#include <fstream>


using namespace dlib;
using namespace std;

// ----------------------------------------------------------------------------------------

int main(int argc, char** argv)
{
    try
    {
        // 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;

        if (argc != 2)
        {
            cout << "You must supply a file name on the command line.  The file should "
                 << "contain a serialized Bayesian Network" << endl;
            return 1;
        }

        ifstream fin(argv[1],ios::binary);

        // Note that the saved networks produced by the bayes_net_gui_ex.cpp example can be deserialized
        // into a network.  So you can make your networks using that GUI if you like.
        cout << "Loading the network from disk..." << endl;
        deserialize(bn, fin);

        cout << "Number of nodes in the network: " << bn.number_of_nodes() << endl;

        // Lets compute some probability values using the loaded network using the join tree (aka. Junction 
        // Tree) algorithm.

        // First we need to create an undirected graph which contains set objects at each node and
        // edge.  This long declaration does the trick.
52
        typedef graph<dlib::set<unsigned long>::compare_1b_c, dlib::set<unsigned long>::compare_1b_c>::kernel_1a_c join_tree_type;
53
54
        join_tree_type join_tree;

Davis King's avatar
Davis King committed
55
        // Now we need to populate the join_tree with data from our bayesian network.  The next two 
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
        // 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 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
        cout << "Using the join tree algorithm:\n";
        for (unsigned long i = 0; i < bn.number_of_nodes(); ++i)
        {
            // print out the probability distribution for node i.  
            cout << "p(node " << i <<") = " << solution.probability(i);
        }
    }
    catch (exception& e)
    {
        cout << "exception thrown: " << e.what() << endl;
        return 1;
    }
}