diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 3af3404..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: CI - -on: [pull_request] - -jobs: - build-and-test: - runs-on: ubuntu-latest - - steps: - - name: Check out code - uses: actions/checkout@v2 - - - name: Install dependencies - run: sudo apt-get install -y gcc make - - - name: Build - run: make all - - - name: Run tests - run: make run-all diff --git a/datasets/00020000-1.bin b/datasets/00020000-1.bin new file mode 100644 index 0000000..aacefed Binary files /dev/null and b/datasets/00020000-1.bin differ diff --git a/datasets/00050000-3.bin b/datasets/00050000-3.bin new file mode 100644 index 0000000..ab553d7 Binary files /dev/null and b/datasets/00050000-3.bin differ diff --git a/include/knn_improvments.h b/include/knn_improvments.h index 6da735f..9808552 100644 --- a/include/knn_improvments.h +++ b/include/knn_improvments.h @@ -3,15 +3,16 @@ #include "../include/knn.h" -void knn_improved_algorithm(Graph** graph, int K, String distance_function); +void knn_improved_algorithm(Graph** graph, int K, String distance_function, double p); void changeNeighbors(Graph** graph); -void localJoin(Node** node, String distance_function); +void localJoin(Node** node, String distance_function, int pK); int incrementalSearch(NodeNeighborsLinkedList* neighbor1, NodeNeighborsLinkedList* neighbor2); -//sampling +NodeNeighborsLinkedList* sampling(NodeNeighborsLinkedList* neighbors, int pK); + //earlyTermination #endif \ No newline at end of file diff --git a/main/Makefile b/main/Makefile index 0773f59..f7dbba8 100644 --- a/main/Makefile +++ b/main/Makefile @@ -18,9 +18,9 @@ OBJS = main.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODU EXEC = program # Args for examples -ARGSEUCL = $(DATASETS)/00001000-4.bin 60 399 euclidean +ARGSEUCL = $(DATASETS)/00000020.bin 10 65 euclidean 0.8 -ARGSMAN = $(DATASETS)/00001000-4.bin 60 399 manhattan +ARGSMAN = $(DATASETS)/00001000-4.bin 100 399 manhattan 0.1 $(EXEC): $(OBJS) $(CC) $(OBJS) -o $(EXEC) $(LDFLAGS) diff --git a/main/main.c b/main/main.c index 3aab336..45bf661 100644 --- a/main/main.c +++ b/main/main.c @@ -11,6 +11,7 @@ int main(int argc, char *argv[]) { int dimensions = atoi(argv[2]); int K = atoi(argv[3]); String distance_function = argv[4]; + double p = atof(argv[5]); // printf("Starting Creating Graph\n"); @@ -22,7 +23,7 @@ int main(int argc, char *argv[]) { clock_t knn_start, knn_end; knn_start = clock(); - knn_improved_algorithm(&graph, K, distance_function); + knn_improved_algorithm(&graph, K, distance_function, p); // knn_algorithm(&graph, K, distance_function); knn_end = clock(); diff --git a/modules/knn.c b/modules/knn.c index ad38c97..b608732 100644 --- a/modules/knn.c +++ b/modules/knn.c @@ -118,6 +118,8 @@ void KRandomNodes(Graph** graph, int K, String distance_function) { Node* currentNode = (*graph)->nodes; int numNodes = (*graph)->numNodes; + printf("The Nodes are %d\n", numNodes); + if(K > numNodes){ fprintf(stderr, "Too many Neighbors. The Nodes are %d\n", numNodes); exit(EXIT_FAILURE); diff --git a/modules/knn_improvements.c b/modules/knn_improvements.c index 5666150..dd64d41 100644 --- a/modules/knn_improvements.c +++ b/modules/knn_improvements.c @@ -8,16 +8,19 @@ static int changes; /// @param graph /// @param K /// @param distance_function -void knn_improved_algorithm(Graph** graph, int K, String distance_function){ +void knn_improved_algorithm(Graph** graph, int K, String distance_function, double p){ KRandomNodes(graph, K, distance_function); - Node * tempNode = (*graph)->nodes; + Node * tempNode = (*graph)->nodes; + int pK = p*K; + printf("The pK (neighbor) nodes for Sampling is %d\n", pK); + do { changes = 0; - while (tempNode !=NULL) { - localJoin(&tempNode, distance_function); + while (tempNode != NULL) { + localJoin(&tempNode, distance_function, pK); tempNode = tempNode->next; } @@ -33,7 +36,7 @@ void knn_improved_algorithm(Graph** graph, int K, String distance_function){ } tempNode = (*graph)->nodes; - printf("---- changes: %d\n", changes); + printf("Changes: %d\n", changes); } while (changes>0); } @@ -87,10 +90,16 @@ void changeNeighbors(Graph** graph) { } } +//functions for improvements +void localJoin(Node** node, String distance_function, int pK) { + // without sampling, we use the neighbours of the Node + // NodeNeighborsLinkedList* temp = (*node)->neighbors; + + // With the sampling, we create a new list of the pK neighbors (that have the flag True) + NodeNeighborsLinkedList* head = sampling((*node)->neighbors, pK); + NodeNeighborsLinkedList* temp = head; -void localJoin(Node** node, String distance_function) { - NodeNeighborsLinkedList* temp = (*node)->neighbors; while(temp != NULL) { NodeNeighborsLinkedList* tempNeig = (*node)->neighbors; while(tempNeig != NULL) { @@ -124,6 +133,8 @@ void localJoin(Node** node, String distance_function) { } temp = temp->next; } + // free the list of the Neighbours with true flags + freeNeighbors(head); } @@ -142,4 +153,34 @@ int incrementalSearch(NodeNeighborsLinkedList* neighbor1, NodeNeighborsLinkedLis //sampling -//earlyTermination +NodeNeighborsLinkedList* sampling(NodeNeighborsLinkedList* neighbors, int pK) { + NodeNeighborsLinkedList* tempNeighbor = neighbors; + NodeNeighborsLinkedList* samplingNeighborsHead = NULL; + + while (tempNeighbor != NULL && pK > 0) { + if (tempNeighbor->flag == 1) { + addNeighbor(&samplingNeighborsHead, tempNeighbor->node, tempNeighbor->cost); //free after use + pK--; + } + tempNeighbor = tempNeighbor->next; + } + return samplingNeighborsHead; + + //if the above doesnt work + + // int trueNeighbors[pK]; //maybe malloc in order to return it and free after use + // for (int i = 0; i < pK; i++) { + // trueNeighbors[i] = -1; + // } + + // int count = 0; + + // while (tempNeighbor != NULL && count < pK) { + // if(tempNeighbor->flag == 1){ + // trueNeighbors[count] = tempNeighbor->node->nodeNameInt; + // count++; + // } + // tempNeighbor = tempNeighbor->next; + // } + // return trueNeighbors; +} diff --git a/tests/Makefile b/tests/Makefile index e7eeb40..e1acc21 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -11,6 +11,8 @@ CFLAGS = -Wall -Werror -g -I$(INCLUDE) LDFLAGS = -lm # .o files +OBJSKNN_IMP = $(TESTS)/knn_improvements_test.o $(MODULES)/Cost.o $(MODULES)/knn_improvements.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o + OBJSKNN = $(TESTS)/knn_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o OBJSG = $(TESTS)/graph_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o @@ -27,6 +29,8 @@ OBJSMATH = $(TESTS)/mathematical_test.o $(MODULES)/MathematicalFunctions.o $(MOD OBJSK = $(TESTS)/knn_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o # Executables +EXECKNN_IMP = knn_improvements_test + EXECKNN = knn_test EXECG = graph_test @@ -39,6 +43,9 @@ EXECNEI = neighbours_test EXECMATH = mathFunctions_test +$(EXECKNN_IMP): $(OBJSKNN_IMP) + $(CC) $(OBJSKNN_IMP) -o $(EXECKNN_IMP) $(LDFLAGS) + $(EXECKNN): $(OBJSKNN) $(CC) $(OBJSKNN) -o $(EXECKNN) $(LDFLAGS) @@ -58,7 +65,10 @@ $(EXECMATH): $(OBJSMATH) $(CC) $(OBJSMATH) -o $(EXECMATH) $(LDFLAGS) clean: - rm -f $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH) + rm -f $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSKNN_IMP) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH) $(EXECKNN_IMP) + +runKNN_imp: $(EXECKNN_IMP) + ./$(EXECKNN_IMP) runKNN: $(EXECKNN) ./$(EXECKNN) @@ -78,7 +88,7 @@ runNEI: $(EXECNEI) runMath: $(EXECMATH) ./$(EXECMATH) -run-all: runKNN runG runN runD runNEI runMath +run-all: runMath runG runN runD runNEI runKNN runKNN_imp valgrind-graph: $(EXECG) valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECG) @@ -94,3 +104,6 @@ valgrind-neighbours: $(EXECNEI) valgrind-knn: $(EXECKNN) valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECKNN) + +valgrind-knn-imp: $(EXECKNN_IMP) + valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECKNN_IMP) diff --git a/tests/knn_improvements_test.c b/tests/knn_improvements_test.c new file mode 100644 index 0000000..563f6ff --- /dev/null +++ b/tests/knn_improvements_test.c @@ -0,0 +1,34 @@ +///////////////////////////////////////////////////////////////// +// +// +// Unit tests for KNN improvements. +// +// +////////////////////////////////////////////////////////////////// + +#include "acutest.h" +#include "knn_improvments.h" +#include "knn.h" + + +void testLocalJoin() { + +} + + +void testIncrementalSearch() { + +} + + +void testSampling() { + +} + + +TEST_LIST = { + {"testLocalJoin", testLocalJoin}, + {"testIncrementalSearch", testIncrementalSearch}, + {"testSampling", testSampling}, + {NULL, NULL} +};