-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfer.c
More file actions
409 lines (351 loc) · 8.82 KB
/
infer.c
File metadata and controls
409 lines (351 loc) · 8.82 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/********************************************************************
$RCSfile: infer.c,v $
$Author: alexvk $
$Revision: 1.1 $
$Date: 1997/10/15 02:52:53 $
********************************************************************/
static char rcsid[] = "$Id: infer.c,v 1.1 1997/10/15 02:52:53 alexvk Exp alexvk $";
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "network.h"
#include "utils.h"
#include "cmd.h"
char *AppName;
char cmdLine[512];
/*******************************************************************
Cmd Functions
*******************************************************************/
CMD cmdList[];
int CmdExit(net)
NETWORK *net;
{
#ifdef MULTIPROC
CompparExit();
#else
NetworkFree(net);
printf("Total memory used %#lx bytes (%ld Mb, %ld TB)\n",
TotalMemGet(), TotalMemGet()/1024L/1024L, TotalMemGet()/1024L/1024L/1024L/1024L);
#endif /* MULTIPROC */
return(1);
}
int CmdDumpLabels(net)
NETWORK *net;
{
char str[32];
int i, k;
NODE *node = NULL;
net->abNum = 0;
while(1) {
printf("Enter node (0 ... %d): ", net->numNodes - 1);
gets(str);
if (!*str || sscanf(str, "%d", &k) != 1) break;
if (k < 0 || k >= net->numNodes) continue;
if ((node = &net->nodes[k]) != NULL) {
printf("Node %s [", node->nodeName);
for (i = 0; i < node->numValues; i++) {
if (i > 0) printf(", ");
printf("%s", node->nodeLabels[i]);
}
printf("]\n");
}
}
return (0);
}
int CmdOptFact(net)
NETWORK *net;
{
char str[32];
int node;
net->abNum = 0;
while(1) {
printf("Enter query node (0 ... %d): ", net->numNodes - 1);
gets(str);
if(!*str || sscanf(str, "%d", &node) != 1) break;
if(node < 0 || node >= net->numNodes) continue;
if(SetMember(node, net->ab, net->abNum)) continue;
if(net->nodeEvidence[node] != NOTSET) continue;
net->ab[net->abNum++] = node;
}
/*
printf("Query nodes ");
SetDump(net->ab, net->abNum);
*/
net->aNum = net->abNum;
while(1) {
printf("Enter conditioning node (0 ... %d): ", net->numNodes - 1);
gets(str);
if(!*str || sscanf(str, "%d", &node) != 1) break;
if(node < 0 || node >= net->numNodes) continue;
if(SetMember(node, net->ab, net->abNum)) continue;
if(net->nodeEvidence[node] != NOTSET) continue;
net->ab[net->abNum++] = node;
}
/*
printf("Conditioning nodes ");
SetDump(net->ab + net->aNum, net->abNum - net->aNum);
*/
if(net->abNum == 0) return(0);
if(net->aNum == 0) net->aNum = net->abNum;
printf("You have entered query ");
OptfactPrintQuery(net);
printf("proceed (y/n)?");
gets(str);
if(tolower(*str) == 'y') {
printf("Making a query\n");
TIMER("OptfactQuery", OptfactQuery(net));
if(!net->numGroups) return (0);
OptfactDumpResult(net);
}
OptfactTreeFree(net);
return (0);
}
int CmdDisplayNetwork(net)
NETWORK *net;
{
NetworkDisplay(net);
return(0);
}
int CmdDisplayTree(net)
NETWORK *net;
{
ClusterTreeDisplay(net);
return(0);
}
int CmdProbDump(net)
NETWORK *net;
{
char str[32];
int node;
if(net->priors == NULL) {
printf("Network not initialized\n");
return (0);
}
while(1) {
printf("Enter a node (0 ... %d): ", net->numCliques - 1);
gets(str);
if (!*str || sscanf(str, "%d", &node) != 1) break;
if (node < 0 || node >= net->numCliques) continue;
ClusterDumpProb(net, node);
}
return(0);
}
int CmdEnterEvidence(net)
NETWORK *net;
{
int i, node, val;
NODE *x;
char str[32];
while(1) {
printf("Enter node to instantiate (0 ... %d): ", net->numNodes - 1);
gets(str);
if (!*str || (node = atoi(str)) == -1) break;
if(node < 0 || node >= net->numNodes) continue;
x = net->nodes + node;
if(net->nodeEvidence[node] != NOTSET) {
printf(" Node %s is already instantiated to %s\n",
x->nodeName, x->nodeLabels[net->nodeEvidence[node]]);
} else {
printf(" Enter value of node %s (0 ... %d): ",
x->nodeName, x->numValues - 1);
gets(str);
if ((val = atoi(str)) < 0) continue;
if ( val >= x->numValues ) continue;
net->nodeEvidence[node] = val;
net->nodeMarked[node] = TRUE;
}
}
NetworkDumpEvidence(net);
return(0);
}
int CmdResetEvidence(net)
NETWORK *net;
{
if(net->numCliques) {
return CmdInitializeTree(net);
} else {
NetworkResetEvidence(net);
return(0);
}
}
int CmdTreeUpdate(net)
NETWORK *net;
{
if(net->priors == NULL) {
printf("Network not initialized\n");
return (0);
} else {
TIMER("ClusterTreeUpdate", ClusterTreeUpdate(net));
}
return(0);
}
int CmdDisplayBeliefs(net)
NETWORK *net;
{
if(net->priors == NULL) {
printf("Network not initialized\n");
} else {
TIMER("ClusterTreeUpdateBeliefs", ClusterTreeUpdateBeliefs(net));
printf("**. p(\"evidence\") = %.6lg\n", probEvid);
NetworkDumpBeliefs(net);
}
return(0);
}
int CmdNodeQuery(net)
NETWORK *net;
{
int i;
char str[32];
int node;
NODE *x;
if(net->priors == NULL) {
printf("Network not initialized\n");
return (0);
}
while(1) {
printf("Enter a node (0 ... %d): ", net->numNodes - 1);
gets(str);
if (!*str || sscanf(str, "%d", &node) != 1) break;
if (node < 0 || node >= net->numNodes) continue;
x = net->nodes + node;
TIMER("ClusterTreeSingleNodeQuery", ClusterTreeSingleNodeQuery(net, node));
VectorSum(x->beliefs, x->numValues, &probEvid);
printf("x%d. %-20s [ ", node, x->nodeName);
for(i=0; i<x->numValues; i++) {
printf("%6.4lf ", x->beliefs[i]/probEvid);
}
printf("]\n");
}
return(0);
}
int CmdDisplayEvidence(net)
NETWORK *net;
{
NetworkDumpEvidence(net);
return(0);
}
int CmdInitializeTree(net)
NETWORK *net;
{
if(!net->priors) {
if(net->numCliques) {
printf("This will take a while ...\n");
TIMER("ClusterTreeInit", ClusterTreeInit(net));
TIMER("StorePriors", STOREPRIORS(net));
Dbg(printf("Local memory used: %#lx bytes\n", TotalMemGet()));
} else {
printf("No join tree in memory ...\n");
}
} else {
TIMER("RestoreTree", RESTORETREE(net));
NetworkResetEvidence(net);
}
return(0);
}
int CmdToggleDebug(net)
NETWORK *net;
{
DbgFlag = (DbgFlag ? false : true);
printf("Debugging is %s\n", DbgFlag ? "on" : "off");
return(0);
}
int CmdHelp(net)
NETWORK *net;
{
CMD *item;
for (item = cmdList; item->cmdstr != 0; item++)
printf("%-15s %s\n", item->cmdstr, item->itemstr);
return(0);
}
CMD cmdList[] = {
{ "q", "Quit", CmdExit },
{ "=", "Toggle debugging", CmdToggleDebug },
{ "a", "Display all evidence", CmdDisplayEvidence },
{ "b", "Display beliefs", CmdDisplayBeliefs },
{ "d", "Dump probability distribution", CmdProbDump },
{ "e", "Enter evidence", CmdEnterEvidence },
{ "h", "Display Help", CmdHelp },
{ "i", "Initialize join tree", CmdInitializeTree },
{ "l", "Dump node labels", CmdDumpLabels },
{ "n", "Display network", CmdDisplayNetwork },
{ "o", "Optimal factoring query", CmdOptFact },
{ "r", "Reset evidence", CmdResetEvidence },
{ "s", "Evaluate a single node query", CmdNodeQuery },
{ "t", "Display join tree", CmdDisplayTree },
{ "u", "Update join tree", CmdTreeUpdate },
{ 0 }
};
/*******************************************************************
Application Functions
*******************************************************************/
void AppInit(s)
char *s;
{
AppName = s;
Randomize(); /* initialize random number generator */
}
void AppRunCmd(net)
NETWORK *net;
{
char *q;
CMD *item;
printf("Commands available:\n");
CmdHelp(net);
while(true) {
printf("infer > ");
gets(cmdLine);
if (*cmdLine) {
q = strtok(cmdLine, " \t\n");
for (item = cmdList; item->cmdstr; item++) {
if (!strcasecmp(q, item->cmdstr)) {
if ((item->funcptr)(net))
return;
break;
}
}
if (!item->cmdstr)
printf("Unrecognized command: %s\n", q);
}
}
}
void AppUsageMessage(s)
char *s;
{
#ifdef MULTIPROC
printf("usage: %s networkFile numProcs\n",s);
#else
printf("usage: %s networkFile\n",s);
#endif /* MULTIPROC */
}
int main(argc, argv)
int argc;
char **argv;
{
NETWORK *net;
AppInit(argv[0]);
#ifdef MULTIPROC
if (argc < 3) {
#else
if (argc < 2) {
#endif /* MULTIPROC */
AppUsageMessage(argv[0]);
return(1);
}
NetworkNew(&net);
net->algorithm = ALG_UNKNOWN;
net->netName = argv[1];
NetworkFileReadErgo(net, net->netName);
NetworkAnalyze(net);
TIMER("ClusterTreeGenerate", ClusterTreeGenerate(net));
Dbg(printf("Local memory used: %#lx bytes\n", TotalMemGet()));
#ifdef MULTIPROC
CompparInit(net, (int) atoi(argv[2]));
#endif /* MULTIPROC */
#ifdef MEASURE
TIMER("ClusterTreeInit", ClusterTreeInit(net));
CmdExit(net);
#else
AppRunCmd(net);
#endif /* MEASURE */
return(0);
}