-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbo.c
324 lines (294 loc) · 10 KB
/
cbo.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
clock_t start, end;
extern int errno; // globally holds the error no
int data_size; // holds the data set size read from .cxt file
int attribute_size; // holds the attribute size read from .cxt file
int **cross_table; // holds data set cross table from .cxt file
int concept_count = 0; // holds generated concept count
// define concept_t for hold each concept objects and attribute sets
typedef struct {
int *objects;
int *attributes;
} concept_t;
concept_t *concept_latice; // holds main concept latice, generated output
// local functions
void loadData(char *file_path);
void buildInitialConcept(int obj[], int attr[]);
void computeConceptFrom(int *obj, int *attr, int attr_index);
void processConcept(int *obj, int *attr);
bool checkAttribute(int j, int *attr);
void makeExtent(int *extent, int *obj, int attr_index);
void makeIntent(int *intent, int *extent, int attr_index);
bool canonicity(int *attr, int *intent, int attr_index);
int main(int argc, char *argv[]) {
loadData(argv[1]); // read data from file path
int ini_obj[data_size]; // initial concept object list
int ini_attr[attribute_size]; // initial concept attribute list
buildInitialConcept(ini_obj, ini_attr); // make object and attribute list
concept_latice = malloc(
data_size * attribute_size * sizeof(concept_t *)); // allocate memory on concept latice
start = clock(); // start timing
computeConceptFrom(ini_obj, ini_attr, 0); // invoke Close-by-One
end = clock(); // stop timing
printf("\nTotal Concepts : %d\n\n", concept_count);
printf("execution time : %f seconds\n\n", ((double) (end - start) / CLOCKS_PER_SEC));
return 0;
}
// load data set file from given location
void loadData(char *file_path) {
int errnum;
FILE *file;
if ((file = fopen(file_path, "rt")) == NULL) {
errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror(errnum));
} else {
printf("\n~~~ Dataset Cross Table ~~~\n\n");
char buffer[256];
int line_count = 0;
int data_read_count = 0;
int obj_count = 0;
int atr_count = 0;
while (fgets(buffer, sizeof(buffer), file)) {
// process lines
if (buffer[0] == '\n') {
// new line found
} else {
// skip first character on the .cxt file
if (line_count != 0) {
if (line_count == 1) {
// data size found
data_size = atoi(buffer);
} else if (line_count == 2) {
// attribute size found
attribute_size = atoi(buffer);
// set cross table memory
cross_table = (int **) malloc(sizeof(int *) * data_size);
} else if (line_count > 2 && line_count <= (data_size + 2)) {
// read data set objects
obj_count++;
} else if (line_count > (2 + data_size) && line_count <= (2 + data_size + attribute_size)) {
// read attributes
atr_count++;
obj_count = 0; // reset obj count
} else if (line_count > (2 + data_size + attribute_size)) {
// read cross table
cross_table[obj_count] = (int *) malloc(
sizeof(int) * attribute_size); // allocate cross table row
int x;
for (x = 0; x < attribute_size; x++) {
// check attribute present or not
if (buffer[x] == 'X') {
cross_table[obj_count][x] = 1; // assign one when 'X'
} else {
cross_table[obj_count][x] = 0; // assign zero when '.'
}
printf("%d", cross_table[obj_count][x]);
}
printf("\n");
obj_count++;
}
}
line_count++;
}
}
fclose(file);
printf("\n");
}
}
// build up initial concept
// out: objects, attributes
void buildInitialConcept(int obj[], int attr[]) {
int i;
int a;
/**
* assign objects
* pass all objects into list, according to the theorem, (X)
*/
for (i = 0; i < data_size; i++) {
obj[i] = i;
}
/**
* assign attributes
* set common attribute list for all objects on cross table (X up)
*/
// go through attributes
for (a = 0; a < attribute_size; a++) {
bool status = true;
// go through objects
for (i = 0; i < data_size; i++) {
if (cross_table[i][a] == 0) {
status = false;
break;
}
}
if (status) {
// attribute available for all objects, assign 1
attr[a] = 1;
} else {
attr[a] = 0;
}
}
}
/**
* Close-by-One Algorithm
*
* input : 1. object list
* 2. attribute list
* 3. current attribute index
*/
void computeConceptFrom(int *obj, int *attr, int attr_index) {
// 1. Process Concept
processConcept(obj, attr);
// 2. go through attribute list
int j;
for (j = attr_index; j < attribute_size; j++) {
// 3. check current attribute exist or not
if (!checkAttribute(j, attr)) {
// 4. make extent
int extent[data_size];
makeExtent(extent, obj, j);
// 5. make intent
int intent[attribute_size];
makeIntent(intent, extent, j);
// 6. do canonicity test
if (canonicity(attr, intent, j)) {
// 7. call computeConceptFrom
computeConceptFrom(extent, intent, (j + 1));
}
}
}
}
// store concept
void processConcept(int *obj, int *attr) {
printf("\n-------------------------------\n");
printf("Concept - %d\n\n", concept_count);
int i;
// set objects details on concept latice
// concept_latice[concept_count] = (concept_t)malloc(sizeof(concept_t));
concept_latice[concept_count].objects = (int *) malloc(sizeof(int) * data_size);
printf("Object Set : ");
for (i = 0; i < data_size; i++) {
concept_latice[concept_count].objects[i] = obj[i];
printf("%d ", concept_latice[concept_count].objects[i]);
}
printf("\n");
// set attribute details on concept latice
concept_latice[concept_count].attributes = (int *) malloc(sizeof(int) * attribute_size);
printf("Attribute Set : ");
for (i = 0; i < attribute_size; i++) {
concept_latice[concept_count].attributes[i] = attr[i];
printf("%d ", concept_latice[concept_count].attributes[i]);
}
printf("\n-------------------------------\n\n");
concept_count++;
}
// check attribute contains on attribute list or not
bool checkAttribute(int j, int *attr) {
bool status = true;
if (attr[j] == 0) {
status = false;
}
return status;
}
// make extent
void makeExtent(int *extent, int *obj, int attr_index) {
int i, z;
printf("extent (attr : %d): ", attr_index);
// go through cross table
for (i = 0; i < data_size; i++) {
extent[i] = -1; // set default value
if (cross_table[i][attr_index] == 1 && obj[i] != -1) {
extent[i] = i; // set object index to extent list
}
printf("%d ", extent[i]);
}
printf("\n");
}
// make intent
void makeIntent(int *intent, int *extent, int attr_index) {
int i, a;
int empty_count = 0;
printf("intent (attr : %d): ", attr_index);
// check extent is empty set
for (i = 0; i < data_size; i++) {
if (extent[i] == -1) {
empty_count++;
}
}
for (a = 0; a < attribute_size; a++) {
// validate on empty extent set
if (empty_count != data_size) {
bool status = true;
for (i = 0; i < data_size; i++) {
// check extent available
if (extent[i] != -1) {
// check related cross table index of current exten attribute availability
if (cross_table[i][a] != 1) {
status = false;
break;
}
}
}
if (status) {
intent[a] = 1;
} else {
intent[a] = 0;
}
} else {
intent[a] = 1;
}
printf("%d ", intent[a]);
}
printf("\n");
}
// perform canonicity test
bool canonicity(int *attr, int *intent, int attr_index) {
bool status = false;
int set_1[attr_index];
int set_2[attr_index];
int set_1_c = 0;// holds set 1 found count
int set_2_c = 0;// holds set 2 found count
int i;
// 1. check on atribute list
for (i = 0; i < attr_index; i++) {
// check attr set
if (attr[i] == 1) {
set_1_c++;
set_1[i] = 1;
} else {
set_1[i] = 0;
}
// check intent set
if (intent[i] == 1) {
set_2_c++;
set_2[i] = 1;
} else {
set_2[i] = 0;
}
}
if (set_1_c == 0 && set_2_c == 0) {
// both are empty set
status = true;
} else if ((set_1_c != 0 && set_2_c == 0) || (set_1_c == 0 && set_2_c != 0)) {
// found some element(s) on either of set
status = false;
} else if (set_1_c == set_2_c) {
// found element(s) on both sets
for (i = 0; i < attr_index; i++) {
if (set_1[i] != set_2[i]) {
status = false;
break;
} else {
status = true;
}
}
}
return status;
}