-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdempster.c
510 lines (411 loc) · 13.6 KB
/
dempster.c
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* ---------------------------------------------------------------------- */
/* Module : evidences / dempster shafer */
/* ---------------------------------------------------------------------- */
/* Filename : dempster.c */
/* Version History : 1.0 - 4.7.2004 uncommented running version */
/* 1.1 - 13.11.04 minor refinements */
/* Last Changes : 13.11.04 Comments and refinements */
/* Author : Dr. Dirk Reichardt / Berufsakademie Stuttgart */
/* Created : May 2004 */
/* ---------------------------------------------------------------------- */
/* Compiler Variables : - */
/* ---------------------------------------------------------------------- */
/* Description : */
/* This modules provides functions to handle evidences and accumulate */
/* measures using the dempster shafer rule. */
/* ---------------------------------------------------------------------- */
#include <math.h>
#include <stdlib.h>
#include "dempster.h"
/* ---------------------------------------------------------------------- */
/* Function name : createBasicMeasure */
/* ---------------------------------------------------------------------- */
/* input parameters : basicMeasure *m - pointer to an empty basic measure*/
/* which is to be initialized */
/* int size - number of alternatives */
/* output parameters: basicMeasure *m - pointer to the filled structure */
/* return value : - */
/* used routines : - */
/* error handling : - */
/* ---------------------------------------------------------------------- */
void createBasicMeasure(basicMeasure *m, int size)
{
int i;
m->a.size = size; /* setting the alternatives in the structure */
m->value = 1.0f; /* default evidence for the alternative set */
/* all alternatives are marked by 1 - signifying the complete set of */
/* alternatives (A). */
for (i=0;i<m->a.size;i++)
{
m->a.alt[i] = 1;
}
/* no further elements belong to the core of the measurement */
m->next = NULL;
}
/* ---------------------------------------------------------------------- */
/* Function name : createAlternatives */
/* ---------------------------------------------------------------------- */
/* input parameters : int *a - array of membership flags */
/* int size - number of alternatives */
/* output parameters: - */
/* return value : set * - a filled (and allocated) set */
/* structure */
/* used routines : - */
/* error handling : corrects values of a beyond (0,1) and limits size */
/* to the valid borders [0,MAX_ALTERNATIVES] */
/* ---------------------------------------------------------------------- */
/* Description : */
/* Transforms an array of membership flags to a set representation. */
/* ---------------------------------------------------------------------- */
set * createAlternatives(int a[MAX_ALTERNATIVES], int size)
{
set *ret;
int i;
ret = (set *)malloc(sizeof(set));
ret->size = size;
/* checking for invalid values of size parameter */
if (size > MAX_ALTERNATIVES)
ret->size = MAX_ALTERNATIVES;
if (size < 0)
ret->size = 0;
for(i=0;i<ret->size;i++)
{
/* correcting invalid flags: if >= 1 assume 1 else assume 0 */
if (a[i] >= 1)
ret->alt[i] = 1;
else
ret->alt[i] = 0;
}
return ret;
}
int completeAlternatives(set *p)
{
int ret = 0;
int i;
for(i=0;i<p->size;i++)
{
ret += p->alt[i];
}
return (ret == p->size);
}
int emptyBasicMeasure(basicMeasure *m)
{
return (completeAlternatives(&(m->a)));
}
int equalAlternatives(set *m1, set *m2)
{
int i;
int ret = 1;
for (i=0;i<m1->size;i++)
{
ret = ret * (m1->alt[i] == m2->alt[i]);
}
return ret;
}
/* ---------------------------------------------------------------------- */
/* Function name : getBasicMeasure */
/* ---------------------------------------------------------------------- */
/* input parameters : basicMeasure *m - a basic measure in which to look */
/* set *p - the alternative set to look up */
/* output parameters: - */
/* return value : float - the evidence for the set p */
/* used routines : emptyBasicMeasure, completeAlternatives */
/* error handling : - */
/* ---------------------------------------------------------------------- */
/* Description : */
/* the function looks up the evidence for a given alternative set p in a */
/* basic measure. */
/* ---------------------------------------------------------------------- */
float getBasicMeasure(basicMeasure *m, set *p)
{
basicMeasure *tmp;
float ret;
if (emptyBasicMeasure(m))
{
if (completeAlternatives(p))
{
ret = m->value;
}
else
ret = 0.0f;
}
else
{
if (equalAlternatives(&(m->a),p))
{
ret = m->value;
}
else
{
ret = getBasicMeasure(m->next,p);
}
}
return ret;
}
/* ---------------------------------------------------------------------- */
/* Function name : addMeasureEntry */
/* ---------------------------------------------------------------------- */
/* input parameters : basicMeasure *m - a basic measure to extend */
/* set *p - the alternative set enter */
/* float v - the evidence for this set */
/* output parameters: basicMeasure *m - the extended basic measure */
/* return value : - */
/* used routines : emptyBasicMeasure, equalAlternatives */
/* error handling : - */
/* ---------------------------------------------------------------------- */
/* Description : */
/* an evidence value is assigned to a subset of alternatives within a */
/* given measure. */
/* ---------------------------------------------------------------------- */
void addMeasureEntry(basicMeasure *m, set p, float v)
{
basicMeasure *tmp;
int i;
tmp = m;
/* go through until alternative set is found or it is not included */
while((!(emptyBasicMeasure(tmp))) && (!(equalAlternatives(&(tmp->a),&p))))
{
tmp = tmp->next;
}
if (equalAlternatives(&(tmp->a),&p))
{
/* it was already included */
tmp->value += v;
}
else if (emptyBasicMeasure(tmp))
{
/* it is not yet included ...*/
tmp = m->next;
/* allocate memory for the new entry */
m->next = (basicMeasure *)malloc(sizeof(basicMeasure));
/* move the first entry to the new element */
m->next->value = m->value;
m->next->next = tmp;
m->next->a = m->a;
/* move the new entry to the first field */
m->value = v;
m->a = p;
}
/* update assignment to "A" */
tmp = m;
while (!(emptyBasicMeasure(tmp)))
{
tmp = tmp->next;
}
tmp->value -= v;
}
int emptySet(set p)
{
int i;
int ret = 0;
for(i=0;i<p.size;i++)
{
ret+= p.alt[i];
}
return (ret == 0);
}
set getIntersection(set *p1, set *p2)
{
int i;
set ret;
ret.size = p1->size;
for(i=0;i<ret.size;i++)
{
ret.alt[i] = (p1->alt[i] && p2->alt[i]);
}
return ret;
}
float getConflict(basicMeasure *m1, basicMeasure *m2)
{
float conflict = 0.0f;
basicMeasure *tmp1,*tmp2;
tmp1 = m1;
while(!emptyBasicMeasure(tmp1))
{
tmp2 = m2;
while(!(emptyBasicMeasure(tmp2)))
{
if (emptySet(getIntersection(&(tmp1->a),&(tmp2->a))))
{
conflict += tmp1->value * tmp2->value;
}
tmp2 = tmp2->next;
}
tmp1=tmp1->next;
}
return conflict;
}
basicMeasure * getAccumulatedMeasure (basicMeasure *m1, basicMeasure *m2)
{
basicMeasure *ret;
basicMeasure *tmp1,*tmp2;
set p;
float val;
float conflict;
float correction;
ret = (basicMeasure *)malloc(sizeof(basicMeasure));
conflict = getConflict(m1,m2);
if (conflict <= 0.99f)
{
correction = 1.0f/(1.0f-conflict);
createBasicMeasure(ret,m1->a.size);
tmp1 = m1;
while(!(tmp1 == NULL))
{
tmp2 = m2;
while (!(tmp2 == NULL))
{
p = getIntersection(&(tmp1->a),&(tmp2->a));
val = tmp1->value * tmp2->value * correction;
if ((!emptySet(p)) && (val > 0.0f))
addMeasureEntry(ret,p,val);
tmp2=tmp2->next;
}
tmp1=tmp1->next;
}
}
return ret;
}
/* ---------------------------------------------------------------------- */
/* Function name : plausibility */
/* ---------------------------------------------------------------------- */
/* Description : */
/* Determines the plausibility value for an alternative "index" for a */
/* given basic measure m. */
/* ---------------------------------------------------------------------- */
float plausibility(basicMeasure *m, int index)
{
float p = 0.0f;
basicMeasure *tmp;
tmp = m;
if (!((index < 0) || (index > m->a.size -1)))
{
while (!(tmp == NULL))
{
if (tmp->a.alt[index] == 1)
{
p += tmp->value;
}
tmp = tmp->next;
}
}
return p;
}
/* ---------------------------------------------------------------------- */
/* Function name : generateAlternatives */
/* ---------------------------------------------------------------------- */
/* Description : */
/* generates an alternative set by a binary coded number. for example: */
/* index 6 results in an alternative set 0110. */
/* ---------------------------------------------------------------------- */
set generateAlternatives(int size, int index)
{
set p;
int i,ind;
int base = 1;
p.size = size;
base = (int)pow(2,size-1);
ind = index;
for(i=0;i<size;i++)
{
p.alt[i] = (ind / base);
ind = ind % base;
base = base/2;
}
return p;
}
int isSubset(set *p, set *q)
{
int i;
int ret = 1;
for(i=0;i<p->size;i++)
{
if ((q->alt[i] == 0) &&
(p->alt[i] == 1))
ret = 0;
}
return ret;
}
set invert(set *p)
{
set ret;
int i;
ret.size = p->size;
for(i=0;i<p->size;i++)
ret.alt[i] = (p->alt[i] == 0);
return ret;
}
float belief(basicMeasure *m, set *p)
{
int i,lim;
set pt;
float res = 0.0f;
pt.size = p->size;
lim = (int)pow(2,p->size);
for (i=0;i<lim;i++)
{
pt = generateAlternatives(p->size,i);
if (isSubset(&pt,p))
{
res += getBasicMeasure(m,&pt);
}
}
return res;
}
float singleBelief (basicMeasure *m, int index)
{
set p;
int i;
for (i=0;i<m->a.size;i++)
{
p.alt[i] = 0;
}
p.alt[index] = 1;
p.size = m->a.size;
return belief(m,&p);
}
float singleDoubt (basicMeasure *m, int index)
{
set p;
int i;
for (i=0;i<m->a.size;i++)
{
p.alt[i] = 1;
}
p.alt[index] = 0;
p.size = m->a.size;
return belief(m,&p);
}
/* ---------------------------------------------------------------------- */
/* Function name : deleteBasicMeasure */
/* ---------------------------------------------------------------------- */
/* Description : */
/* deallocates the dynamically allocated memory for the basic measure m. */
/* ---------------------------------------------------------------------- */
void deleteBasicMeasure(basicMeasure *m)
{
if (m->next == NULL)
{
free(m);
}
else
{
deleteBasicMeasure(m->next);
free(m);
}
}
void printBasicMeasure(basicMeasure *m)
{
basicMeasure *tmp;
int i;
printf("Basic Measure:\n");
tmp = m;
while(tmp != NULL)
{
printf("m([");
for(i=0;i<tmp->a.size;i++)
printf("%1d",tmp->a.alt[i]);
printf("]) = %7.3f\n",tmp->value);
tmp = tmp->next;
}
}