| 50 | // ---------------------------------------------------------------------------------------- |
| 51 | |
| 52 | int main() |
| 53 | { |
| 54 | try |
| 55 | { |
| 56 | // There are many useful convenience functions in this namespace. They all |
| 57 | // perform simple access or modify operations on the nodes of a bayesian network. |
| 58 | // You don't have to use them but they are convenient and they also will check for |
| 59 | // various errors in your bayesian network when your application is built with |
| 60 | // the DEBUG or ENABLE_ASSERTS preprocessor definitions defined. So their use |
| 61 | // is recommended. In fact, most of the global functions used in this example |
| 62 | // program are from this namespace. |
| 63 | using namespace bayes_node_utils; |
| 64 | |
| 65 | // This statement declares a bayesian network called bn. Note that a bayesian network |
| 66 | // in the dlib world is just a directed_graph object that contains a special kind |
| 67 | // of node called a bayes_node. |
| 68 | directed_graph<bayes_node>::kernel_1a_c bn; |
| 69 | |
| 70 | // Use an enum to make some more readable names for our nodes. |
| 71 | enum nodes |
| 72 | { |
| 73 | A = 0, |
| 74 | B = 1, |
| 75 | C = 2, |
| 76 | D = 3 |
| 77 | }; |
| 78 | |
| 79 | // The next few blocks of code setup our bayesian network. |
| 80 | |
| 81 | // The first thing we do is tell the bn object how many nodes it has |
| 82 | // and also add the three edges. Again, we are using the network |
| 83 | // shown in ASCII art at the top of this file. |
| 84 | bn.set_number_of_nodes(4); |
| 85 | bn.add_edge(A, D); |
| 86 | bn.add_edge(B, A); |
| 87 | bn.add_edge(C, A); |
| 88 | |
| 89 | |
| 90 | // Now we inform all the nodes in the network that they are binary |
| 91 | // nodes. That is, they only have two possible values. |
| 92 | set_node_num_values(bn, A, 2); |
| 93 | set_node_num_values(bn, B, 2); |
| 94 | set_node_num_values(bn, C, 2); |
| 95 | set_node_num_values(bn, D, 2); |
| 96 | |
| 97 | assignment parent_state; |
| 98 | // Now we will enter all the conditional probability information for each node. |
| 99 | // Each node's conditional probability is dependent on the state of its parents. |
| 100 | // To specify this state we need to use the assignment object. This assignment |
| 101 | // object allows us to specify the state of each nodes parents. |
| 102 | |
| 103 | |
| 104 | // Here we specify that p(B=1) = 0.01 |
| 105 | // parent_state is empty in this case since B is a root node. |
| 106 | set_node_probability(bn, B, 1, parent_state, 0.01); |
| 107 | // Here we specify that p(B=0) = 1-0.01 |
| 108 | set_node_probability(bn, B, 0, parent_state, 1-0.01); |
| 109 |
nothing calls this directly
no test coverage detected