-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cpp
269 lines (208 loc) · 5.54 KB
/
code.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <fstream>
#include <sstream>
#include <cstdlib>
// Format checker just assumes you have Alarm.bif and Solved_Alarm.bif (your file) in current directory
using namespace std;
// Our graph consists of a list of nodes where each node is represented as follows:
class Graph_Node{
private:
string Node_Name; // Variable name
vector<int> Children; // Children of a particular node - these are index of nodes in graph.
vector<string> Parents; // Parents of a particular node- note these are names of parents
int nvalues; // Number of categories a variable represented by this node can take
vector<string> values; // Categories of possible values
vector<float> CPT; // conditional probability table as a 1-d array . Look for BIF format to understand its meaning
public:
// Constructor- a node is initialised with its name and its categories
Graph_Node(string name,int n,vector<string> vals)
{
Node_Name=name;
nvalues=n;
values=vals;
}
string get_name()
{
return Node_Name;
}
vector<int> get_children()
{
return Children;
}
vector<string> get_Parents()
{
return Parents;
}
vector<float> get_CPT()
{
return CPT;
}
int get_nvalues()
{
return nvalues;
}
vector<string> get_values()
{
return values;
}
void set_CPT(vector<float> new_CPT)
{
CPT.clear();
CPT=new_CPT;
}
void set_Parents(vector<string> Parent_Nodes)
{
Parents.clear();
Parents=Parent_Nodes;
}
// add another node in a graph as a child of this node
int add_child(int new_child_index )
{
for(int i=0;i<Children.size();i++)
{
if(Children[i]==new_child_index)
return 0;
}
Children.push_back(new_child_index);
return 1;
}
};
// The whole network represted as a list of nodes
class network{
list <Graph_Node> Pres_Graph;
public:
int addNode(Graph_Node node)
{
Pres_Graph.push_back(node);
return 0;
}
int netSize()
{
return Pres_Graph.size();
}
// get the index of node with a given name
int get_index(string val_name)
{
list<Graph_Node>::iterator listIt;
int count=0;
for(listIt=Pres_Graph.begin();listIt!=Pres_Graph.end();listIt++)
{
if(listIt->get_name().compare(val_name)==0)
return count;
count++;
}
return -1;
}
// get the node at nth index
list<Graph_Node>::iterator get_nth_node(int n)
{
list<Graph_Node>::iterator listIt;
int count=0;
for(listIt=Pres_Graph.begin();listIt!=Pres_Graph.end();listIt++)
{
if(count==n)
return listIt;
count++;
}
return listIt;
}
//get the iterator of a node with a given name
list<Graph_Node>::iterator search_node(string val_name)
{
list<Graph_Node>::iterator listIt;
for(listIt=Pres_Graph.begin();listIt!=Pres_Graph.end();listIt++)
{
if(listIt->get_name().compare(val_name)==0)
return listIt;
}
cout<<"node not found\n";
return listIt;
}
};
network read_network()
{
network Alarm;
string line;
int find=0;
ifstream myfile("alarm.bif");
string temp;
string name;
vector<string> values;
if (myfile.is_open())
{
while (! myfile.eof() )
{
stringstream ss;
getline (myfile,line);
ss.str(line);
ss>>temp;
if(temp.compare("variable")==0)
{
ss>>name;
getline (myfile,line);
stringstream ss2;
ss2.str(line);
for(int i=0;i<4;i++)
{
ss2>>temp;
}
values.clear();
while(temp.compare("};")!=0)
{
values.push_back(temp);
ss2>>temp;
}
Graph_Node new_node(name,values.size(),values);
int pos=Alarm.addNode(new_node);
}
else if(temp.compare("probability")==0)
{
ss>>temp;
ss>>temp;
list<Graph_Node>::iterator listIt;
list<Graph_Node>::iterator listIt1;
listIt=Alarm.search_node(temp);
int index=Alarm.get_index(temp);
ss>>temp;
values.clear();
while(temp.compare(")")!=0)
{
listIt1=Alarm.search_node(temp);
listIt1->add_child(index);
values.push_back(temp);
ss>>temp;
}
listIt->set_Parents(values);
getline (myfile,line);
stringstream ss2;
ss2.str(line);
ss2>> temp;
ss2>> temp;
vector<float> curr_CPT;
string::size_type sz;
while(temp.compare(";")!=0)
{
curr_CPT.push_back(atof(temp.c_str()));
ss2>>temp;
}
listIt->set_CPT(curr_CPT);
}
else
{
}
}
if(find==1)
myfile.close();
}
return Alarm;
}
int main()
{
network Alarm;
Alarm=read_network();
// Example: to do something
cout<<"Perfect! Hurrah! \n";
}