-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.cpp
172 lines (132 loc) · 5.02 KB
/
knn.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
#include "knn.hpp"
size_t getFileSize(FILE * input)
{
size_t initial = ftell(input);
fseek(input, 0, SEEK_END);
size_t fileSize = ftell(input);
fseek(input, initial, SEEK_SET);
return fileSize;
}
void ** getDatabase(int * dataCount)
{
FILE * input = fopen("database.dat", "r");
if (input==NULL) {
printf("Unnable to open file!\n");
return NULL;
}
void ** database = NULL;
*dataCount = 0;
long fileSize = getFileSize(input);
while (ftell(input) != fileSize){
ulong auxTotal;
if (fread(&auxTotal, sizeof(ulong), 1, input)!=1)
printf("Reading Error!");
database = (void**) realloc(database, (*dataCount + 1) * sizeof(void*));
database[*dataCount] = malloc(sizeof(ulong) + (auxTotal+1)*sizeof(double) + sizeof(int));
memcpy(database[*dataCount], &auxTotal, sizeof(ulong));
if (fread(database[*dataCount]+sizeof(ulong), (sizeof(double)*auxTotal), 1, input)!=1 ||
fread(database[*dataCount]+sizeof(ulong)+(sizeof(double)*auxTotal), sizeof(int), 1, input)!=1)
printf("Reading Error!");
memset(database[(*dataCount)++]+sizeof(ulong)+(sizeof(double)*auxTotal)+sizeof(int), 0, sizeof(double));
}
return database;
}
int compareVotes(void * A, void * B, ulong total)
{
return
(*((Candidate**)A))->votes >
(*((Candidate**)B))->votes;
}
int compareDist(void * A, void * B, ulong total)
{
return
*((double*)((*((void**)A)) + sizeof(ulong)+(sizeof(double)*total)+sizeof(int))) <
*((double*)((*((void**)B)) + sizeof(ulong)+(sizeof(double)*total)+sizeof(int)));
}
void mergeSort(void * vect, size_t n, size_t size, int (*compare)(void *, void *, ulong), ulong total)
{
size_t lowerHalf = n >> 1;
if (lowerHalf > 1) mergeSort(vect, lowerHalf, size, compare, total);
size_t upperHalf = n - lowerHalf;
void * upperVect = vect + (size * lowerHalf);
if (upperHalf > 1) mergeSort(upperVect, upperHalf, size, compare, total);
void * auxVect = malloc (n * size);
size_t lowerIndex, upperIndex, auxIndex;
for (lowerIndex = 0, upperIndex = 0, auxIndex = 0;
(auxIndex < n) && (lowerIndex < lowerHalf) && (upperIndex < upperHalf);
auxIndex++)
memcpy (auxVect + (auxIndex * size),
compare(upperVect + (upperIndex * size), vect + (lowerIndex * size), total) ?
upperVect + (upperIndex++ * size) : vect + (lowerIndex++ * size),
size);
memcpy (auxVect + (auxIndex * size),
(lowerIndex < lowerHalf) ? vect + (lowerIndex * size) : upperVect + (upperIndex * size),
(n-auxIndex) * size);
memcpy (vect, auxVect, n * size);
free(auxVect);
}
double getDist(double * A, double * B, ulong size)
{
double sum = 0;
for (ulong i=0; i<size; i++) sum+=(A[i]-B[i])*(A[i]-B[i]);
return sqrt(sum);
}
void calcDists(void ** database, int dataCount, Histogram * histA)
{
for (int i=0; i<dataCount; i++){
double dist = getDist(histA->elements, (double*)(database[i]+sizeof(ulong)), histA->total);
memcpy(database[i]+sizeof(ulong)+(sizeof(double)*(histA->total))+sizeof(int), &dist, sizeof(double));
}
mergeSort(database, dataCount, sizeof(void*), &compareDist, histA->total);
}
Candidate * election(void ** database, int dataCount, Histogram * hist, int k)
{
Candidate ** elec = NULL;
int elecCount = 0;
for (int i=0; i<k; i++){
int exists, j;
for (j=0, exists=0; j<elecCount && exists==0 && j<dataCount; j++)
if (*((int*)(database[i]+sizeof(ulong)+(sizeof(double)*(hist->total)))) ==
elec[j]->value)
exists = 1;
if (exists) elec[j-1]->votes++;
else {
elec = (Candidate**) realloc(elec, (elecCount + 1) * sizeof(Candidate*));
elec[elecCount] = (Candidate*) malloc(sizeof(Candidate));
elec[elecCount]->value = *((int*)(database[i]+sizeof(ulong)+(sizeof(double)*(hist->total))));
elec[elecCount++]->votes = 1;
}
}
mergeSort(elec, elecCount, sizeof(Candidate*), &compareVotes, 0);
Candidate * output = (Candidate*) malloc(sizeof(Candidate));
memcpy(output, elec[0], sizeof(Candidate));
for (int i=0; i<elecCount; i++) free(elec[i]);
free(elec);
return output;
}
int getClass(void ** database, int dataCount, Histogram * hist, int k)
{
calcDists(database, dataCount, hist);
Candidate * auxCand = election(database, dataCount, hist, k);
int output = auxCand->value;
printf("Classe:\t%d Centavos %s (%s)\t(%05d)\nProbabilidade:\t%.3lf\t(%d/%d)\n\n",
auxCand->value%1000,
(auxCand->value/1000)%10 ? "Nova" : "Antiga",
auxCand->value/10000 ? "Coroa" : "Cara",
auxCand->value, auxCand->votes/(double)k, auxCand->votes, k);
free(auxCand);
/*
void ** auxDatabase = (void**) malloc(dataCount*sizeof(void*));
for (int i=0; i<dataCount; i++){
auxDatabase[i] = malloc(sizeof(ulong) + (hist->total+1)*sizeof(double) + sizeof(int));
memcpy(auxDatabase[i], database[i], sizeof(ulong) + (hist->total+1)*sizeof(double) + sizeof(int));
*((int*)(auxDatabase[i]+sizeof(ulong)+(sizeof(double)*(hist->total)))) =
(*((int*)(auxDatabase[i]+sizeof(ulong)+(sizeof(double)*(hist->total)))))%1000;
}
auxCand = election(auxDatabase, dataCount, hist, k);
printf("Valor:\t%d Centavos\nProbabilidade:\t%.3lf\t(%d/%d)\n",
auxCand->value, auxCand->votes/(double)k, auxCand->votes, k);
free(auxCand);
*/
return output;
}