From 78c4f1cfa56936767e8ee6880d89477f2d81d50c Mon Sep 17 00:00:00 2001 From: Jakhes <dean.schmitz@schmitzbauer.de> Date: Wed, 26 Oct 2022 22:08:26 +0200 Subject: [PATCH] Adding knn tests --- src/methods/knn/knn.cpp | 25 +++-- src/methods/knn/knn.pl | 21 ++-- src/methods/knn/knn_test.pl | 213 +++++++++++++++++++++++++++++++++--- 3 files changed, 229 insertions(+), 30 deletions(-) diff --git a/src/methods/knn/knn.cpp b/src/methods/knn/knn.cpp index f7d4451..98d3b34 100644 --- a/src/methods/knn/knn.cpp +++ b/src/methods/knn/knn.cpp @@ -17,6 +17,7 @@ using namespace mlpack::neighbor; // Global Variable of the NSModel object so it can be accessed from all functions NSModel<NearestNS> nsModelObj; +int trainedDimension = 0; // input: TreeTypes treeType = TreeTypes::KD_TREE, @@ -49,26 +50,28 @@ void initAndBuildModel(char const *treeType, char const *algorithm, tree = NSModel<NearestNS>::COVER_TREE; else if (strcmp(treeType, "r") == 0) tree = NSModel<NearestNS>::R_TREE; - else if (strcmp(treeType, "r-star") == 0) + else if (strcmp(treeType, "r_star") == 0) tree = NSModel<NearestNS>::R_STAR_TREE; else if (strcmp(treeType, "ball") == 0) tree = NSModel<NearestNS>::BALL_TREE; else if (strcmp(treeType, "x") == 0) tree = NSModel<NearestNS>::X_TREE; - else if (strcmp(treeType, "hilbert-r") == 0) + else if (strcmp(treeType, "hilbert_r") == 0) tree = NSModel<NearestNS>::HILBERT_R_TREE; - else if (strcmp(treeType, "r-plus") == 0) + else if (strcmp(treeType, "r_plus") == 0) tree = NSModel<NearestNS>::R_PLUS_TREE; - else if (strcmp(treeType, "r-plus-plus") == 0) + else if (strcmp(treeType, "r_plus_plus") == 0) tree = NSModel<NearestNS>::R_PLUS_PLUS_TREE; else if (strcmp(treeType, "vp") == 0) tree = NSModel<NearestNS>::VP_TREE; else if (strcmp(treeType, "rp") == 0) tree = NSModel<NearestNS>::RP_TREE; - else if (strcmp(treeType, "max-rp") == 0) + else if (strcmp(treeType, "max_rp") == 0) tree = NSModel<NearestNS>::MAX_RP_TREE; else if (strcmp(treeType, "ub") == 0) tree = NSModel<NearestNS>::UB_TREE; + else if (strcmp(treeType, "spill") == 0) + tree = NSModel<NearestNS>::SPILL_TREE; else if (strcmp(treeType, "oct") == 0) tree = NSModel<NearestNS>::OCTREE; else @@ -96,7 +99,7 @@ void initAndBuildModel(char const *treeType, char const *algorithm, nsModelObj.Tau() = tau; nsModelObj.Rho() = rho; - + trainedDimension = reference.n_rows; nsModelObj.BuildModel(std::move(reference), size_t(leafSize), searchMode, epsilon); } @@ -115,6 +118,14 @@ void searchWithQuery( float *queryMatArr, SP_integer queryMatSize, SP_integer float **neighborsMatArr, SP_integer *neighborsMatColNum, SP_integer *neighborsMatRowNum, float **distancesMatArr, SP_integer *distancesMatColNum, SP_integer *distancesMatRowNum) { + if (trainedDimension != queryMatRowNum) + { + std::ostringstream stream; + stream << "Queryset has Dim(" << queryMatRowNum << ") but the Referenceset has Dim(" << trainedDimension << ")"; + raisePrologSystemExeption(stream.str().c_str()); + return; + } + // convert the Prolog array to arma::mat mat query = convertArrayToMat(queryMatArr, queryMatSize, queryMatRowNum); @@ -169,4 +180,4 @@ void searchNoQuery( SP_integer k, // return the Matrices returnMatrixInformation(neighborsReturnMat, neighborsMatArr, neighborsMatColNum, neighborsMatRowNum); returnMatrixInformation(distancesReturnMat, distancesMatArr, distancesMatColNum, distancesMatRowNum); -} \ No newline at end of file +} diff --git a/src/methods/knn/knn.pl b/src/methods/knn/knn.pl index ee1144d..f4d0cac 100644 --- a/src/methods/knn/knn.pl +++ b/src/methods/knn/knn.pl @@ -20,7 +20,7 @@ %% --Input-- -%% string treeType => "kd", "vp", "rp", "max-rp", "ub", "cover", "r", "r-star", "x", "ball", "hilbert-r", "r-plus", "r-plus-plus", "spill", "oct", +%% string treeType => "kd", "vp", "rp", "max_rp", "ub", "cover", "r", "r_star", "x", "ball", "hilbert_r", "r_plus", "r_plus_plus", "spill", "oct", %% string searchMode => "naive", "single_tree", "dual_tree", "greedy", %% bool randomBasis => (1)true / (0)false, %% int leafSize => 30, @@ -35,13 +35,18 @@ %% Initialize the Model and build it. %% initAndBuildModel(TreeType, SearchMode, RandomBasis, LeafSize, Tau, Rho, Epsilon, ReferenceList, ReferenceRows) :- + LeafSize >= 1, + Tau >= 0, + Rho >= 0, + Rho =< 1, + Epsilon >= 0, convert_list_to_float_array(ReferenceList, ReferenceRows, array(Xsize, Xrownum, X)), initAndBuildModelI(TreeType, SearchMode, RandomBasis, LeafSize, Tau, Rho, Epsilon, X, Xsize, Xrownum). -foreign(initAndBuildModel, c, initAndBuildModelI(+string, +string, - +integer, - +integer, +float32, +float32, +float32, - +pointer(float_array), +integer, +integer)). +foreign(initAndBuildModel, c, initAndBuildModelI( +string, +string, + +integer, + +integer, +float32, +float32, +float32, + +pointer(float_array), +integer, +integer)). %% --Input-- @@ -56,12 +61,13 @@ foreign(initAndBuildModel, c, initAndBuildModelI(+string, +string, %% Perform neighbor search on the queryset. %% searchWithQuery(QueryList, QueryRows, K, NeighborsList, YCols, DistancesList, ZCols) :- + K > 0, convert_list_to_float_array(QueryList, QueryRows, array(Xsize, Xrownum, X)), searchWithQueryI(X, Xsize, Xrownum, K, Y, YCols, YRows, Z, ZCols, ZRows), convert_float_array_to_2d_list(Y, YCols, YRows, NeighborsList), convert_float_array_to_2d_list(Z, ZCols, ZRows, DistancesList). -foreign(searchWithQuery, c, searchWithQueryI( +pointer(float_array), +integer, +integer, +foreign(searchWithQuery, c, searchWithQueryI( +pointer(float_array), +integer, +integer, +integer, -pointer(float_array), -integer, -integer, -pointer(float_array), -integer, -integer)). @@ -78,11 +84,12 @@ foreign(searchWithQuery, c, searchWithQueryI( +pointer(float_array), +integer %% Perform monochromatic neighbor search. %% searchNoQuery(K, NeighborsList, YCols, DistancesList, ZCols) :- + K > 0, searchNoQueryI(K, Y, YCols, YRows, Z, ZCols, ZRows), convert_float_array_to_2d_list(Y, YCols, YRows, NeighborsList), convert_float_array_to_2d_list(Z, ZCols, ZRows, DistancesList). -foreign(searchNoQuery, c, searchNoQueryI( +integer, +foreign(searchNoQuery, c, searchNoQueryI( +integer, -pointer(float_array), -integer, -integer, -pointer(float_array), -integer, -integer)). diff --git a/src/methods/knn/knn_test.pl b/src/methods/knn/knn_test.pl index 8d29958..6b37752 100644 --- a/src/methods/knn/knn_test.pl +++ b/src/methods/knn/knn_test.pl @@ -7,37 +7,218 @@ :- use_module('../../helper_files/helper.pl'). reset_Model :- - initModel(1,0,50,0.0001). + initAndBuildModel(kd, dual_tree, 0, 20, 0.7, 0.0, 0.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). %% -%% TESTING predicate predicate/10 +%% TESTING predicate initAndBuildModel/9 %% -:- begin_tests(predicate). +:- begin_tests(initAndBuildModel). %% Failure Tests -test(testDescription, [error(domain_error('expectation' , culprit), _)]) :- - reset_Model_No_Train(perceptron), - train([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,0,0,0], 2, culprit, 50, 0.0001, _). +test(knn_InitAndBuildModel_Wrong_TreeType_Input, [error(domain_error('The given TreeType is unknown!' , wrongInput), _)]) :- + initAndBuildModel(wrongInput, dual_tree, 0, 20, 0.7, 0.0, 0.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). -test(testDescription2, [error(_,system_error('The values of the Label have to start at 0 and be >= 0 and < the given numClass!'))]) :- - reset_Model_No_Train(perceptron), - train([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,1,0,2], 2, perceptron, 50, 0.0001, _). +test(knn_InitAndBuildModel_Wrong_SearchMode_Input, [error(domain_error('The given SearchMode is unknown!' , wrongInput), _)]) :- + initAndBuildModel(kd, wrongInput, 0, 20, 0.7, 0.0, 0.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_Negative_LeafSize, fail) :- + initAndBuildModel(kd, dual_tree, 0, 0, 0.7, 0.0, 0.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_Negative_Tau, fail) :- + initAndBuildModel(kd, dual_tree, 0, 20, -0.7, 0.0, 0.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_Bad_Rho_Input, fail) :- + initAndBuildModel(kd, dual_tree, 0, 20, 0.7, -0.5, 0.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(kd, dual_tree, 0, 20, 0.7, 1.5, 0.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_Negative_Epsilon, fail) :- + initAndBuildModel(kd, dual_tree, 0, 20, 0.7, 0.0, -1.0, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + + +%% Successful Tests + +test(knn_InitAndBuildModel_KD) :- + initAndBuildModel(kd, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(kd, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(kd, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(kd, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_VP) :- + initAndBuildModel(vp, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(vp, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(vp, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(vp, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_RP) :- + initAndBuildModel(rp, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(rp, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(rp, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(rp, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_MAX_RP) :- + initAndBuildModel(max_rp, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(max_rp, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(max_rp, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(max_rp, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_UB) :- + initAndBuildModel(ub, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(ub, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(ub, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(ub, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_COVER) :- + initAndBuildModel(cover, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(cover, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(cover, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(cover, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_R) :- + initAndBuildModel(r, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_R_STAR) :- + initAndBuildModel(r_star, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_star, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_star, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_star, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_X) :- + initAndBuildModel(x, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(x, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(x, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(x, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_BALL) :- + initAndBuildModel(ball, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(ball, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(ball, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(ball, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_Hilbert_R) :- + initAndBuildModel(hilbert_r, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(hilbert_r, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(hilbert_r, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(hilbert_r, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_R_Plus) :- + initAndBuildModel(r_plus, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_plus, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_plus, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_plus, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_R_Plus_Plus) :- + initAndBuildModel(r_plus_plus, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_plus_plus, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_plus_plus, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(r_plus_plus, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_SPILL) :- + initAndBuildModel(spill, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(spill, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(spill, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(spill, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_OCT) :- + initAndBuildModel(oct, naive, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(oct, single_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(oct, dual_tree, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3), + initAndBuildModel(oct, greedy, 0, 20, 0.7, 0.01, 0.005, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3). + +test(knn_InitAndBuildModel_CSV_Input) :- + reset_Model, + open('src/data_csv/iris2.csv', read, File), + take_csv_row(File, skipFirstRow,10, Data), + initAndBuildModel(kd, dual_tree, 1, 20, 0.7, 0.01, 0.005, Data, 4). + +:- end_tests(initAndBuildModel). + + + +%% +%% TESTING predicate searchWithQuery/7 +%% +:- begin_tests(searchWithQuery). + +%% Failure Tests + +test(knn_SearchWithQuery_Wrong_Query_Dims, [error(_,system_error('Queryset has Dim(4) but the Referenceset has Dim(3)'))]) :- + reset_Model, + searchWithQuery([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 4, 2, _, _, _, _). + +test(knn_SearchWithQuery_Negative_K, fail) :- + reset_Model, + searchWithQuery([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, -1, _, _, _, _). + +test(knn_SearchWithQuery_Too_Large_K, [error(_,system_error('Requested value of k (10) is greater than the number of points in the reference set (4)'))]) :- + reset_Model, + searchWithQuery([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, 10, _, _, _, _). + + +%% Successful Tests + +test(knn_SearchWithQuery_Normal) :- + reset_Model, + searchWithQuery([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, 2, NeighborsList, _, DistancesList, _), + print('\nNeighbors: '), + print(NeighborsList), + print('\nDistances: '), + print(DistancesList). + +test(knn_SearchWithQuery_CSV_Input) :- + open('src/data_csv/iris2.csv', read, File), + take_csv_row(File, skipFirstRow,10, Data), + initAndBuildModel(kd, dual_tree, 0, 20, 0.7, 0.0, 0.0, Data, 4), + searchWithQuery([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, 7, NeighborsList, _, DistancesList, _), + print('\nNeighbors: '), + print(NeighborsList), + print('\nDistances: '), + print(DistancesList). + +:- end_tests(searchWithQuery). + + + +%% +%% TESTING predicate searchNoQuery/10 +%% +:- begin_tests(searchNoQuery). + +%% Failure Tests + +test(knn_SearchNoQuery_Negative_K, fail) :- + reset_Model, + searchNoQuery(-1, _, _, _, _). + +test(knn_SearchNoQuery_Too_Large_K, [error(_,system_error('Requested value of k (15) is greater than the number of points in the reference set (4)'))]) :- + reset_Model, + searchNoQuery(15, _, _, _, _). %% Successful Tests -test(testDescription3, [true(Error =:= 1)]) :- - reset_Model_No_Train(perceptron), - train([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,0,0,0], 2, perceptron, 50, 0.0001, Error). +test(knn_SearchNoQuery_Normal) :- + reset_Model, + searchNoQuery(2, NeighborsList, _, DistancesList, _), + print('\nNeighbors: '), + print(NeighborsList), + print('\nDistances: '), + print(DistancesList). -test(testDescription4, [true(Error =:= 0.9797958971132711)]) :- - reset_Model_No_Train(perceptron), +test(knn_SearchNoQuery_CSV_Input) :- open('src/data_csv/iris2.csv', read, File), take_csv_row(File, skipFirstRow,10, Data), - train(Data, 4, [0,1,0,1,1,0,1,1,1,0], 2, perceptron, 50, 0.0001, Error). + initAndBuildModel(kd, dual_tree, 0, 20, 0.7, 0.0, 0.0, Data, 4), + searchNoQuery(7, NeighborsList, _, DistancesList, _), + print('\nNeighbors: '), + print(NeighborsList), + print('\nDistances: '), + print(DistancesList). -:- end_tests(predicate). +:- end_tests(searchNoQuery). run_knn_tests :- run_tests. -- GitLab