Skip to content

Commit

Permalink
Merge branch 'Master' into 47-job-sceduler
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyMich authored Jan 11, 2024
2 parents b35f482 + ce18c14 commit 71a92e2
Show file tree
Hide file tree
Showing 16 changed files with 545 additions and 29 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@

* **git Actions**: Ο Βασιλής ασχολήθηκε με την δημιουργία και την σωστή λειτουργία των git actions. Παρέχοντας αυτοματοποιημένα σενάρια που εκτελούνται με βάση συγκεκριμένες συνθήκες. Αυτά τα σενάρια εκτελούνται κάθε φορά που γίνεται η υποβολή νέου κώδικα.

- Unit tests: Δημιουργήθηκαν unit tests για την εξέταση του καινούργιου κώδικά.
- Unit tests: Δημιουργήθηκαν unit tests για την εξέταση του καινούργιου κώδικά (από την Άννα και τον Βασίλη).

### Κύρια Branches
- main και K23-NNS-Project-v2: Σε αυτά τα branch υπάρχουν όλα τα αρχεία για τις δύο πρώτες εργασίες (ΚΝΝ και ΚΝΝ με βελτιώσεις).

- K23-NNS-Project-v1: Σε αυτό το branch υπάρχουν όλα τα αρχεία για την πρώτη εργασία, τα οποία τροποποιήθηκαν σύμφωνα με τις οδηγίες που μας δώθηκαν.

### Makefiles
Οι εντολές είναι ίδιες με αυτές στο 1ο παραδοτέο και μόνο στο Makefile στον φάκελο tests προστέθηκε έξτρα:
- Με make runKNN_imp: τρέχουν τα τεστ για τις συναρτήσεις του knn_improvements.

### Extra
1. Για να τρέξει ο αλγόριθμος της 1ης εργασίας, πρέπει απλά στο αρχείο main.c να βγεί από το σχόλιο η γραμμή 25 και να μπει σε σχόλιο η γραμμή 24.
2. Υπάρχει και ένα αρχείο times.txt, όπου αναφέρονται οι χρόνοι αλλά και η ακρίβεια των αλγορίθμων ΚΝΝ και ΚΝΝ με βελτιώσεις για Ευκλείδια και για Manhattan αποστάσεις.


----------------------
Expand Down Expand Up @@ -85,5 +98,8 @@
7. Με make run-all: τρέχουν τα τεστ για όλε τις συναρτήσεις

* Γενικο Makefile
Όπως και παραπάνω οι make εντολές είναι ίδιες, απλά με make run-all τρέχει και ο αλγόριθμος αλλά και τα τεστ.
Όπως και παραπάνω οι make εντολές είναι ίδιες, και υπάρχουν έξτρα οι εντολές:
1. make all: τρέχoυν και ο αλγόριθμος αλλά και τα τεστ.
2. main-all: τρέχει ο αλγόριθμος με Ευκλείδια απόσταση αλλά και Manhattan
3. run-all-tests: τρέχουν μόνο τα τεστ.

8 changes: 6 additions & 2 deletions include/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ double findAccurationResultSuperAccurate(Graph* graph , Graph* graphRightResults

void freeGraph(Graph* graph);

void makeFile(String filename);

void writeGraphToFile(Graph* graph, const char* filename);


/*-----for creating binary files-----*/
double randDouble(double min, double max, long long int i);

void createBinary(String filename);

#endif // GRAPH_H
16 changes: 14 additions & 2 deletions include/MathematicalFunctions.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#include "Dimension.h"
#ifndef MATHEMATICAL_FUNCTIONS_H
#define MATHEMATICAL_FUNCTIONS_H

#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include "Node.h"
#include "common_types.h"
#include <string.h>

double euclidean_distance(Dimension* nodeDimension1 , Dimension* nodeDimension2);

double manhattan_distance(Dimension* nodeDimension1 , Dimension* nodeDimension2);

double distance(Dimension* nodeDimension1 , Dimension* nodeDimension2, String distance_function);
double calculate_norm(Dimension* head);

double euclidean_distance_improved(Node* node1, Node* node2);

double distance(Node* node1, Node* node2, String distance_function);

#endif
3 changes: 2 additions & 1 deletion include/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef struct NodeNeighborsLinkedList NodeNeighborsLinkedList;

typedef struct Node {
int nodeNameInt;
double norm;
Dimension* dimension;
NodeNeighborsLinkedList* neighbors;
NodeNeighborsLinkedList* reversedNeighbors;
Expand All @@ -27,4 +28,4 @@ void addNode(Node** headNode, Dimension* headDimension);

void freeNode(Node* node);

#endif
#endif
7 changes: 6 additions & 1 deletion include/knn_improvments.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef KNN_IMPROVEMENTS_H
#define KNN_IMPROVEMENTS_H

#include "../include/knn.h"
#include "knn.h"
#include "projection_trees.h"


typedef struct DataJob {
Node* node;
Expand All @@ -22,6 +24,9 @@ typedef struct {

void knn_improved_algorithm(Graph** graph, int K, String distance_function, double p,double earlyTerminationParameter);

void knn_improved_algorithm(Graph** graph, TreeNode* treeRoot, int K, String distance_function, double p,double earlyTerminationParameter);


void changeNeighbors(Node* node);

void localJoin(Node** node, String distance_function, int pK);
Expand Down
41 changes: 41 additions & 0 deletions include/projection_trees.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef PROJECTIONTREES_H
#define PROJECTIONTREES_H

#include <stdio.h>
#include <stdlib.h>
#include "common_types.h"
#include "Node.h"
#include "Graph.h"
#include "MathematicalFunctions.h"
#include "knn.h"

// Define a data structure for a node in the tree
typedef struct TreeNode {
int* projection; // Random projection vector
struct TreeNode* left;
struct TreeNode* right;
Node* data; // Only for leaf nodes
} TreeNode;


int* generateRandomProjection(int dimension);

double computeProjection(Node* point, int* projection);

void addData(Node** headData, Node* data);

TreeNode* buildRandomProjectionTree(Node* data, int dimension, int D, int numNodes);

Node* searchTree(TreeNode* root, Node* node);

void randomNeighbors(Graph** graph, TreeNode* root, int K, String distance_function);

void remainingRandomNodes(Graph** graph, Node* currentNode, int* usedNumbers, int remainingNodes, int K, String distance_function);

void freeTree(TreeNode* root);

void freeNodeProj(Node* node);

void freeDimensionProj(Dimension* dimension);

#endif
19 changes: 17 additions & 2 deletions main/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ CC = gcc

# Compile options. Το -I<dir> tells to compiler where to find the include files
# -Werror when complite code add -Werror

CFLAGS = -Wall -Werror -g -I$(INCLUDE)
LDFLAGS = -lm -fsanitize=address -lpthread


# .o files
OBJS = main.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o $(MODULES)/knn_improvements.o $(MODULES)/Cost.o
OBJS = main.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o $(MODULES)/knn_improvements.o $(MODULES)/Cost.o $(MODULES)/projection_trees.o

OBJSAccuarion = accuration.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o $(MODULES)/knn_improvements.o $(MODULES)/Cost.o
# Executable
Expand All @@ -22,10 +24,19 @@ EXECAccuration = programAccuration


# Args for examples

ARGSEUCL = $(DATASETS)/1000000.bin 500 100 euclidean 0.4 0.002050

ARGSMAN = $(DATASETS)/1000000.bin 100 100 manhattan 0.9 0.1

ARGSEUCL = $(DATASETS)/1000000.bin 1000 500 euclidean 0.8 0.001 50

ARGSMAN = $(DATASETS)/100000.bin 100 80 manhattan 0.1 0.001 50

ARGSIMP = $(DATASETS)/asciiData3.bin 10 50 improved 0.1 0.001 50



$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(EXEC) $(LDFLAGS)
@if [ -f $(EXEC).exe ]; then ln -sf $(EXEC).exe $(EXEC); fi
Expand All @@ -47,5 +58,9 @@ run-accuration: $(EXECAccuration)
run-manhattan: $(EXEC)
./$(EXEC) $(ARGSMAN)

run-euclidean-improved: $(EXEC)
./$(EXEC) $(ARGSIMP)


valgrind: $(EXEC)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXEC) $(ARGSEUCL)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXEC) $(ARGSIMP)
20 changes: 20 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,34 @@ int main(int argc, char* argv[]) {
String distance_function = argv[4];
double p = atof(argv[5]);
double earlyTerminationParameter = atof(argv[6]);

//int threads = atoi(argv[7]);
//int jobs = atoi(argv[8]);
int proccesId = atoi(argv[9]);

Graph* graph = createGraphFromBinaryFile(argv[1], dimensions);


int D = atoi(argv[7]);

//TODO-- time calc
Graph* graph = createGraphFromBinaryFile(argv[1], dimensions);

//TODO-- time calc
TreeNode* treeRoot = buildRandomProjectionTree(graph->nodes, dimensions, D, graph->numNodes);


printf("\n----- Starting KNN algorithm -----\n");

clock_t knn_start, knn_end;
knn_start = clock();


knn_improved_algorithm(&graph, K, distance_function, p, earlyTerminationParameter);


knn_improved_algorithm(&graph, treeRoot, K, distance_function, p, earlyTerminationParameter);

// knn_algorithm(&graph, K, distance_function);

printf("\n----- Finished KNN algorithm -----\n");
Expand All @@ -50,5 +66,9 @@ int main(int argc, char* argv[]) {
freeGraph(graph);
freeGraph(graphRightResults);

freeTree(treeRoot);

// createBinary("100000.bin");

return 0;
}
3 changes: 2 additions & 1 deletion modules/FindAllRightNeighborsAlgorithm.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ void FindAllRightNeighbors(Graph * graph, String distance_function,int neighbors


void OrderNodesByNeighbor(Node *dest , Node *src, String distance_function){
addNeighbor(&dest->neighbors,src, distance(dest->dimension,src->dimension, distance_function));}
addNeighbor(&dest->neighbors,src, distance(dest->dimension,src->dimension, distance_function));} //Here or in the addNeigbor i would like to add a sorting method base on distance
}
40 changes: 39 additions & 1 deletion modules/Graph.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../include/Graph.h"
#include "time.h"


Graph* initGraph() {
Expand Down Expand Up @@ -62,6 +63,7 @@ Graph* createGraphFromBinaryFile(String filename, int dimensions) {
flag = 1;
break;
}
// printf("coordinate-%f\n",coordinate);
addDimension(&headDimension, i, coordinate);
}

Expand Down Expand Up @@ -150,6 +152,9 @@ double findAccurationResult(Graph* graph , Graph* graphRightResults, int neighbo
}

// Function to write the graph data to a file
/// @brief
/// @param graph
/// @param filename
void writeGraphToFile(Graph* graph, const char* filename) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
Expand All @@ -173,4 +178,37 @@ void writeGraphToFile(Graph* graph, const char* filename) {
}

fclose(file);
}
}


/*-----for creating binary files-----*/
/// @brief
/// @param min
/// @param max
/// @param i
/// @return
double randDouble(double min, double max, long long int i) {
time_t t;
srand((unsigned) time(&t)*i);

double range = (max-min);
double div = RAND_MAX/range;
return min + (rand()/div);
}

/// @brief
/// @param filename
void createBinary(String filename) {
FILE* file = fopen(filename, "wb");
if (file == NULL) {
perror("Error opening file");
exit(1);
}

for(int i = 0; i < 1000000; i++) {
double num = randDouble(-100, 100, i);
//randomly make doubles and write one by one in the filename
fwrite(&num, sizeof(double), 1, file); // Read one double at a time
}
fclose(file);
}
Loading

0 comments on commit 71a92e2

Please sign in to comment.