You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.1 KiB
188 lines
5.1 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
|
|
#include "knnSerialDeclarations.h"
|
|
|
|
void getArguments(int argc, char** argv){
|
|
if (argc != 6) {
|
|
printf("Usage: %s p d k filename tfilename\nwhere:\n", argv[0]);
|
|
printf("\tp is the the number of points\n");
|
|
printf("\td is the number of dimensions of each point\n");
|
|
printf("\tk is the number of neighbors to search for\n");
|
|
printf("\tfilename is the filename of the dataset file\n");
|
|
printf("\tfilename is the filename of the test file\n");
|
|
exit(1);
|
|
}
|
|
numberOfPoints = atoi(argv[1]);
|
|
numberOfDimensions = atoi(argv[2]);
|
|
numberOfNeighbors = numberOfPoints - 1;
|
|
k = atoi(argv[3]);
|
|
if (k >= numberOfPoints){
|
|
k = numberOfPoints - 1;
|
|
}
|
|
pointsFilename = argv[4];
|
|
testFilename = argv[5];
|
|
}
|
|
|
|
void init(double ***pointsArray, neighbor ***sortedNeighborsArray){
|
|
//Allocates continuous memory for points array
|
|
*pointsArray = cMalloc(numberOfPoints, numberOfDimensions);
|
|
|
|
//Allocates memory for neighbors array
|
|
if ( (*sortedNeighborsArray = (neighbor**)(malloc((sizeof(neighbor *)) * numberOfPoints)) )
|
|
!= NULL){
|
|
for (int row = 0; row < numberOfPoints; ++row){
|
|
if ( ((*sortedNeighborsArray)[row]
|
|
= (neighbor*)(malloc((sizeof(neighbor)) * numberOfNeighbors)) ) == NULL){
|
|
printf("Error allocating memory\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
} else {
|
|
printf("Error allocating memory\n");
|
|
exit(1);
|
|
}
|
|
|
|
//Reads coordinates from the file
|
|
if (readPointsArrayFromFile(pointsArray)){
|
|
printf("Press any key to exit.\n");
|
|
getchar();
|
|
exit(1);
|
|
}
|
|
|
|
//Initializes neighbors array distances and ID's to -1
|
|
for (int point=0; point<numberOfPoints; ++point){
|
|
for(int neighbor=0; neighbor<numberOfNeighbors; ++neighbor){
|
|
(*sortedNeighborsArray)[point][neighbor].distance = -1;
|
|
(*sortedNeighborsArray)[point][neighbor].neighborId = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
double **cMalloc(int rows, int columns){
|
|
double *tempArray = (double *)malloc(rows * columns * sizeof(double));
|
|
double **array;
|
|
|
|
if ((array = ((double**)(malloc((sizeof(double *)) * rows)))) != NULL){
|
|
for (int row = 0; row < rows; ++row){
|
|
array[row] = &(tempArray[columns * row]);
|
|
}
|
|
} else {
|
|
printf("Error allocating memory\n");
|
|
exit(1);
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
int readPointsArrayFromFile(double ***array){
|
|
FILE *pointsBinaryFile = fopen(pointsFilename, "rb");
|
|
|
|
if (pointsBinaryFile == NULL){
|
|
printf("Couldn't open points file.\n");
|
|
return 1;
|
|
}
|
|
for (int point=0; point<numberOfPoints; ++point){
|
|
if (fread((*array)[point], sizeof(double), numberOfDimensions, pointsBinaryFile)
|
|
!= numberOfDimensions) {
|
|
if(feof(pointsBinaryFile)){
|
|
printf("Premature end of file reached.\n");
|
|
} else{
|
|
printf("Error reading points file.");
|
|
}
|
|
fclose(pointsBinaryFile);
|
|
return 1;
|
|
}
|
|
}
|
|
fclose(pointsBinaryFile);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void printArray(int rows, int columns, double ***array){
|
|
for (int row=0; row<rows; ++row){
|
|
for (int column=0; column<columns; ++column){
|
|
printf("[%d][%d] = %f\n", row, column, (*array)[row][column]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void calculateDistances(double ***firstPointsSet, double ***secondPointsSet
|
|
, neighbor ***sortedNeighborsArray){
|
|
for (int firstPoint=0; firstPoint<numberOfPoints; ++firstPoint){
|
|
for (int secondPoint=0; secondPoint<numberOfPoints; ++secondPoint){
|
|
if (firstPoint == secondPoint){
|
|
continue;
|
|
}
|
|
double distance = 0;
|
|
for (int dimensionIndex=0; dimensionIndex<numberOfDimensions; ++dimensionIndex){
|
|
distance += pow((*firstPointsSet)[firstPoint][dimensionIndex]
|
|
- (*secondPointsSet)[secondPoint][dimensionIndex] ,2);
|
|
}
|
|
addDistance(sortedNeighborsArray, firstPoint, secondPoint, distance);
|
|
}
|
|
}
|
|
}
|
|
|
|
void addDistance(neighbor ***sortedNeighborsArray, int firstPointId, int secondPointId
|
|
, double pointsDistance){
|
|
//Determines actual index in which the distance will be added
|
|
int insertIndex = secondPointId;
|
|
if (secondPointId > firstPointId){
|
|
--insertIndex;
|
|
}
|
|
|
|
(*sortedNeighborsArray)[firstPointId][insertIndex].distance = pointsDistance;
|
|
(*sortedNeighborsArray)[firstPointId][insertIndex].neighborId = secondPointId;
|
|
}
|
|
|
|
int compare (const void *a, const void *b){
|
|
//Casts arguments to pointers to neighbors
|
|
const neighbor *neighborA = (neighbor*) a;
|
|
const neighbor *neighborB = (neighbor*) b;
|
|
|
|
if (neighborA->distance == neighborB->distance){
|
|
return 0;
|
|
} else{
|
|
return neighborA->distance > neighborB->distance ? 1 : -1;
|
|
}
|
|
}
|
|
|
|
int test(neighbor ***sortedNeighborsArray, char *testFilename){
|
|
FILE *testFile = fopen(testFilename, "r");
|
|
|
|
if (testFile == NULL){
|
|
printf("Couldn't open test file.\n");
|
|
perror("fopen");
|
|
return 1;
|
|
}
|
|
for (int point=0; point<numberOfPoints; ++point){
|
|
for (int i=0; i<k; ++i){
|
|
int tempID = 0;
|
|
if (fscanf(testFile, "%d,", &tempID) == EOF){
|
|
printf("Premature end of file reached.\n");
|
|
fclose(testFile);
|
|
return 1;
|
|
}
|
|
if ((*sortedNeighborsArray)[point][i].neighborId != tempID - 1){
|
|
fclose(testFile);
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
fclose(testFile);
|
|
return 0;
|
|
}
|
|
|
|
void cleanUp(double ***pointsArray, neighbor ***sortedNeighborsArray){
|
|
free((*pointsArray)[0]);
|
|
free(*pointsArray);
|
|
|
|
for (int row=0; row<numberOfPoints; ++row){
|
|
free((*sortedNeighborsArray)[row]);
|
|
}
|
|
free(*sortedNeighborsArray);
|
|
}
|