-
Notifications
You must be signed in to change notification settings - Fork 1
/
bn.h
109 lines (83 loc) · 2.04 KB
/
bn.h
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
#ifndef EXACT_PE_BN_H_
#define EXACT_PE_BN_H_
#include <iostream>
#include <string>
#include <vector>
#include "node.h"
#include "cpt.h"
using namespace std;
class CPT;
class Node;
extern double pe_result;
extern bool debug;
extern int max_cpt;
inline string trim_right_copy(
const string& s,
const string& delimiters = " \f\n\r\t\v" )
{
if(!s.length()) return "";
return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
}
inline string trim_left_copy(
const string& s,
const string& delimiters = " \f\n\r\t\v" )
{
if(!s.length()) return "";
return s.substr( s.find_first_not_of( delimiters ) );
}
inline string trim_copy(
const string& s,
const string& delimiters = " \f\n\r\t\v" )
{
return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
}
inline string dec_to_bin(int number,int parent_count)
{
if(parent_count <= 0) return "";
string leading_zero = "";
for(int i = 0; i < parent_count - 1; i++){
leading_zero += "0";
}
if ( number == 0 ) return leading_zero + "0";
if ( number == 1 ) return leading_zero + "1";
if ( number % 2 == 0 )
return dec_to_bin(number / 2, parent_count - 1) + "0";
else
return dec_to_bin(number / 2, parent_count - 1) + "1";
}
class BayesianNetwork
{
public:
/* constructor */
BayesianNetwork();
/* TODO */
bool removeNonEvidenceLeaves();
/* read data from file */
bool parseXbif(string);
/* search all cpts containing the given node */
vector<CPT*> searchCPT(Node*);
/* add node to the network */
bool addNode(Node);
/* remove node from the network */
bool removeNode(Node*);
/* search node by name */
Node* searchNode(string);
/* all nodes */
map<string, Node*> getNodes();
/* add cpt to the network */
bool addCPT(CPT*);
/* remove cpt from the network */
bool removeCPT(CPT*);
/* all cpts */
vector<CPT*> getCPTs();
/* min-fill heuristic */
Node* nextNodeByMinFill();
/* min-degree heuristic */
Node* nextNodeByMinDegree();
private:
/* all nodes */
map<string, Node*> _nodes;
/* all cpts */
vector<CPT*> _cpts;
};
#endif