-
Notifications
You must be signed in to change notification settings - Fork 0
/
Format_Checker.cpp
395 lines (305 loc) · 8.72 KB
/
Format_Checker.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
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
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <math.h>
using namespace std;
class Graph_Node{
private:
string Node_Name;
vector<int> Children;
vector<string> Parents;
int nvalues;
vector<string> values;
vector<float> CPT;
public:
//Graph_Node(string name, vector<Graph_Node*> Child_Nodes,vector<Graph_Node*> Parent_Nodes,int n, vector<string> vals,vector<float> curr_CPT)
Graph_Node(string name,int n,vector<string> vals)
{
Node_Name=name;
//Children=Child_Nodes;
//Parents=Parent_Nodes;
nvalues=n;
values=vals;
//CPT=curr_CPT;
}
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;
}
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;
}
};
class network{
list <Graph_Node> Pres_Graph;
public:
int addNode(Graph_Node node)
{
Pres_Graph.push_back(node);
return 0;
}
list<Graph_Node>::iterator getNode(int i)
{
int count=0;
list<Graph_Node>::iterator listIt;
for(listIt=Pres_Graph.begin();listIt!=Pres_Graph.end();listIt++)
{
if(count++==i)
break;
}
return listIt;
}
int netSize()
{
return Pres_Graph.size();
}
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;
}
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;
}
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;
}
};
void check_format()
{
network Alarm;
string line,testline;
int find=0;
ifstream myfile("alarm.bif");
ifstream testfile("solved_alarm.bif");
string temp;
string name;
vector<string> values;
int line_count=1;
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
getline (testfile,testline);
if(testline.compare(line)!=0)
{
cout<<"Error Here in line number"<<line_count<<"\n";
exit(0);
}
line_count++;
stringstream ss;
ss.str(line);
ss>>temp;
if(temp.compare("probability")==0)
{
string test_temp;
getline (myfile,line);
getline (testfile,testline);
stringstream ss2;
stringstream testss2;
ss2.str(line);
ss2>> temp;
testss2.str(testline);
testss2>>test_temp;
if(test_temp.compare(temp)!=0)
{
cout<<"Error Here in line number"<<line_count<<"\n";
exit(0);
}
ss2>> temp;
testss2>>test_temp;
vector<float> curr_CPT;
string::size_type sz;
while(temp.compare(";")!=0)
{
if(!atof(test_temp.c_str()))
{
cout<<" Probem in Probab values in line "<<line_count<<"\n";
exit(0);
}
//cout<<"here"<<temp<<"\n";
ss2>>temp;
testss2>>test_temp;
}
if(test_temp.compare(";")!=0)
{
cout<<" Probem in Semi-colon in line "<<line_count<<"\n";
exit(0);
}
line_count++;
}
//myfile.close();
}
if(!testfile.eof())
{
cout<<" Test File contains more lines\n";
exit(0);
}
//cout<<line;
//if(find==1)
myfile.close();
testfile.close();
}
}
network read_network(char* filename)
{
network Alarm;
string line;
int find=0;
ifstream myfile(filename);
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 Alarm1,Alarm2;
check_format();
Alarm1=read_network((char*)"solved_alarm.bif");
Alarm2=read_network((char*)"gold_alarm.bif");
float score=0;
for(int i=0;i<Alarm1.netSize();i++)
{
list<Graph_Node>::iterator listIt1=Alarm1.get_nth_node(i);
list<Graph_Node>::iterator listIt2=Alarm2.get_nth_node(i);
vector<float> cpt1=listIt1->get_CPT();
vector<float> cpt2=listIt2->get_CPT();
for(int j=0;j<cpt1.size();j++)
score+=fabs(cpt1[j]-cpt2[j]);
}
cout <<"Score is "<<score;
//cout<<Alarm.netSize();
}