Skip to content
Snippets Groups Projects
Select Git revision
  • 8f49aebfcab7ee7cc5d93de496403db247721141
  • master default protected
  • release/1.1.4
  • release/1.1.3
  • release/1.1.1
  • 1.4.2
  • 1.4.1
  • 1.4.0
  • 1.3.0
  • 1.2.1
  • 1.2.0
  • 1.1.5
  • 1.1.4
  • 1.1.3
  • 1.1.1
  • 1.1.0
  • 1.0.9
  • 1.0.8
  • 1.0.7
  • v1.0.5
  • 1.0.5
21 results

BOperation.java

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    adaboost_test.pl 12.78 KiB
    
    :- module(adaboost_tests, [run_adaboost_tests/0]).
    
    :- use_module(library(plunit)).
    
    :- use_module(adaboost).
    
    :- use_module(library(csv)).
    :- use_module('../../helper_files/helper.pl').
    
    
    
    reset_Model_No_Train(Learner) :-
            adaboost_initModelNoTraining(0.0001, Learner).
    
    reset_Model_With_Train(Learner) :-
            adaboost_initModelWithTraining([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, 1, 0], 2, Learner, 50, 0.0001).
    
    
    
    %%
    %% TESTING predicate adaboost_initModelWithTraining/7
    %%
    :- begin_tests(adaboost_initModelWithTraining).
    
    %% Failure Tests
    
    test(initModelWithTraining_WrongInputTypes, fail) :-
            adaboost_initModelWithTraining(wrong, 3, [0.2,0.2,0.2,0.2], 2, perceptron, 50, 0.0001).
    
    test(initModelWithTraining_WrongTol, fail) :-
            adaboost_initModelWithTraining([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, 1, 0], -2, perceptron, 50, 0.0001).
    
    test(initModelWithTraining_WrongLearner, [error(domain_error('perceptron or decision_stump' ,wrongLearner), _)]) :-
            adaboost_initModelWithTraining([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, 1, 0], 2, wrongLearner, 50, 0.0001).
    
    test(initModelWithTraining_WrongIterations, fail) :-
            adaboost_initModelWithTraining([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, 1, 0], 2, perceptron, -50, 0.0001).
    
    test(initModelWithTraining_WrongTol, fail) :-
            adaboost_initModelWithTraining([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, 1, 0], 2, perceptron, 50, -10.0).
    
    test(initModelWithTraining_MissmatchingLabels) :-
            adaboost_initModelWithTraining([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, 1, 0, 1 ,1], 2, perceptron, 50, 0.0001).
    
    
    %% Successful Tests
    
    test(initModelWithTraining_Perceptron) :-
            adaboost_initModelWithTraining([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, 1, 0], 2, perceptron, 50, 0.0001).
    
    test(initModelWithTraining_DecisionStump) :-
            adaboost_initModelWithTraining([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3], 3, [0, 0, 1], 2, decision_stump, 50, 0.0001).
    
    :- end_tests(adaboost_initModelWithTraining).
    
    
    
    %%
    %% TESTING predicate adaboost_initModelNoTraining/2
    %%
    :- begin_tests(adaboost_initModelNoTraining).
    
    %% Failure Tests
    
    test(initModelNoTraining_WrongLearner, [error(domain_error('perceptron or decision_stump' ,wrongLearner), _)]) :-
            adaboost_initModelNoTraining(0.0001, wrongLearner).
    
    test(initModelNoTraining_WrongTol, fail) :-
            adaboost_initModelNoTraining(-1.0, perceptron).
    
    %% Successful Tests
    
    test(initModelNoTraining_Perceptron) :-
            adaboost_initModelNoTraining(0.001, perceptron).
    
    test(initModelNoTraining_DecisionStump) :-
            adaboost_initModelNoTraining(0.000014, decision_stump).
    
    :- end_tests(adaboost_initModelNoTraining).
    
    
    %%
    %% TESTING predicate adaboost_classify/8
    %%
    :- begin_tests(adaboost_classify).
    
    %% Failure Tests
    test(classify_on_untrained_model, [error(_,system_error('The model is not trained!'))]) :-
            reset_Model_No_Train(perceptron),
            adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, _, _, _).
    
    test(classify_with_bad_data_input_perceptron, [error(_,system_error('The given data matrix has incorrect dimensions compared to the training data!'))]) :-
            reset_Model_With_Train(perceptron),
            adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 2, _, _, _).
    
    %% should cause an exeption but doesnt TODO:
    test(classify_with_bad_data_input_decision_stump) :-
            reset_Model_With_Train(decision_stump),
            adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 5, _, _, _).
            
    
    %% Successful Tests
    test(classify_perceptron) :-
            reset_Model_No_Train(perceptron),
            take_rows_from_iris_CSV(10, Records),
            adaboost_train(Records, 4, [0,1,0,1,1,0,1,1,1,0], 2, perceptron, 50, 0.0001, _),
            adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4,
                     [1.0,1.0,1.0,1.0,0.0],
                     [[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0]], 2).
    
    test(classify_decision_stump) :-
            reset_Model_No_Train(decision_stump),
            take_rows_from_iris_CSV(10, Records),
            adaboost_train(Records, 4, [0,1,0,1,1,0,1,1,1,0], 2, decision_stump, 50, 0.0001, _),
            adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4,
                     [1.0,1.0,1.0,1.0,1.0],
                     [[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0]], 2).
    
    :- end_tests(adaboost_classify).
    
    
    %%
    %% TESTING predicate adaboost_numClasses/1
    %%
    :- begin_tests(adaboost_numClasses).
    
    test(numClasses_Perceptron_NoTrain, [true(Amount =:= 0)]) :-
            reset_Model_No_Train(perceptron),
            adaboost_numClasses(Amount).
    
    test(numClasses_Decision_Stump_NoTrain, [true(Amount =:= 0)]) :-
            reset_Model_No_Train(decision_stump),
            adaboost_numClasses(Amount).
    
    test(numClasses_Perceptron_WithTrain, [true(Amount =:= 2)]) :-
            reset_Model_With_Train(perceptron),
            adaboost_numClasses(Amount).
    
    test(numClasses_Decision_Stump_WithTrain, [true(Amount =:= 2)]) :-
            reset_Model_With_Train(decision_stump),
            adaboost_numClasses(Amount).
    
    test(numClasses_Custom_NumClasses, [true(Amount =:= 3)]) :-
            adaboost_initModelWithTraining([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, 1, 0], 3, perceptron, 50, 0.0001),
            adaboost_numClasses(Amount).
            
    
    test(numClasses_afterTrain_Perceptron, [true(Amount =:= 2)]) :-
            reset_Model_No_Train(perceptron),
            take_rows_from_iris_CSV(10, Records),
            adaboost_train(Records, 4, [0,1,0,1,1,0,1,1,1,0], 2, perceptron, 50, 0.0001, _),
            adaboost_numClasses(Amount).
            
    :- end_tests(adaboost_numClasses).
    
    
    %%
    %% TESTING predicate adaboost_getTolerance/1
    %%
    :- begin_tests(adaboost_getTolerance).
    
    test(getTolerance_Perceptron_NoTrain, [true(Amount =:= 0.0001)]) :-
            reset_Model_No_Train(perceptron),
            adaboost_getTolerance(Amount).
    
    test(getTolerance_Decision_Stump_NoTrain, [true(Amount =:= 0.0001)]) :-
            reset_Model_No_Train(decision_stump),
            adaboost_getTolerance(Amount).
    
    test(getTolerance_Perceptron_WithTrain, [true(Amount =:= 0.0001)]) :-
            reset_Model_With_Train(perceptron),
            adaboost_getTolerance(Amount).
    
    test(getTolerance_Decision_Stump_WithTrain, [true(Amount =:= 0.0001)]) :-
            reset_Model_With_Train(decision_stump),
            adaboost_getTolerance(Amount).
    
    test(getTolerance_Custom_Tolerance, [true(Amount =:= 0.0009)]) :-
            adaboost_initModelWithTraining([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, 1, 0], 3, perceptron, 50, 0.0009),
            adaboost_getTolerance(Amount).
    
    test(getTolerance_afterTrain, [true(Amount =:= 0.0005)]) :-
            reset_Model_No_Train(perceptron),
            take_rows_from_iris_CSV(10, Records),
            adaboost_train(Records, 4, [0,1,0,1,1,0,1,1,1,0], 2, perceptron, 50, 0.0005, _),
            adaboost_getTolerance(Amount).
            
    :- end_tests(adaboost_getTolerance).
    
    
    %%
    %% TESTING predicate adaboost_modifyTolerance/1
    %%
    :- begin_tests(adaboost_modifyTolerance).
    
    %% Failure Tests
    test(modifyTolerance_With_Negative_Input, fail) :-
            reset_Model_No_Train(perceptron),
            adaboost_modifyTolerance(-0.02).
    
    
    %% Successful Tests
    test(modifyTolerance_Perceptron_NoTrain, [true(Amount =:= 0.02)]) :-
            reset_Model_No_Train(perceptron),
            adaboost_modifyTolerance(0.02),
            adaboost_getTolerance(Amount).
    
    test(modifyTolerance_Decision_Stump_NoTrain, [true(Amount =:= 0.02)]) :-
            reset_Model_No_Train(decision_stump),
            adaboost_modifyTolerance(0.02),
            adaboost_getTolerance(Amount).
    
    test(modifyTolerance_Perceptron_WithTrain, [true(Amount =:= 0.02)]) :-
            reset_Model_With_Train(perceptron),
            adaboost_modifyTolerance(0.02),
            adaboost_getTolerance(Amount).
    
    test(modifyTolerance_Decision_Stump_WithTrain, [true(Amount =:= 0.02)]) :-
            reset_Model_With_Train(decision_stump),
            adaboost_modifyTolerance(0.02),
            adaboost_getTolerance(Amount).
    
    test(modifyTolerance_Custom_Tolerance, [true(Amount =:= 0.02)]) :-
            adaboost_initModelWithTraining([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, 1, 0], 3, perceptron, 50, 0.0001),
            adaboost_modifyTolerance(0.02),
            adaboost_getTolerance(Amount).
    
    test(modifyTolerance_afterTrain, [true(Amount =:= 0.02)]) :-
            reset_Model_No_Train(perceptron),
            take_rows_from_iris_CSV(10, Records),
            adaboost_train(Records, 4, [0,1,0,1,1,0,1,1,1,0], 2, perceptron, 50, 0.0001, _),
            adaboost_modifyTolerance(0.02),
            adaboost_getTolerance(Amount).
            
    :- end_tests(adaboost_modifyTolerance).
    
    
    %%
    %% TESTING predicate adaboost_train/8
    %%
    :- begin_tests(adaboost_train).      
    
    %% Failure Tests
    test(train_With_Bad_NumClass_Input, fail) :-
            reset_Model_No_Train(perceptron),
            adaboost_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, _).
                                                
    test(train_With_Bad_Learner_Input, [error(domain_error('perceptron or decision_stump' ,wrongLearner), _)]) :-
            reset_Model_No_Train(perceptron),
            adaboost_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, wrongLearner, 50, 0.0001, _).
    
    test(train_With_Bad_Iterations_Input, fail) :-
            reset_Model_No_Train(perceptron),
            adaboost_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, _).
    
    test(train_With_Bad_Tol_Input, fail) :-
            reset_Model_No_Train(perceptron),
            adaboost_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, _).
    
    test(train_With_Bad_Labels_Too_Many_Classes, [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),
            adaboost_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(train_With_Bad_Labels_Negative_Perceptron, [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),
            adaboost_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,-1], 2, perceptron, 50, 0.0001, _).
    
    %% should cause an exeption but doesnt TODO:
    test(train_With_Bad_Labels_Negative_Decision_Stump) :-
            reset_Model_No_Train(decision_stump),
            adaboost_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,-1], 2, decision_stump, 50, 0.0001, _).
    
    %% seems to be allowed
    test(train_With_Too_Many_Labels) :-
            reset_Model_No_Train(perceptron),
            adaboost_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,0,0,1], 2, perceptron, 50, 0.0001, _).
    
    test(train_With_Too_Little_Labels, [error(_,system_error('The given Labels Vector is too short!'))]) :-
            reset_Model_No_Train(decision_stump),
            adaboost_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], 2, decision_stump, 50, 0.0001, _).
    
    test(train_With_Negative_RowNumber, fail) :-
            reset_Model_No_Train(decision_stump),
            adaboost_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, decision_stump, 50, 0.0001, _).
            
    
    %% Successful Tests
    
    test(train_With_Direct_Input_Perceptron, [true(Error =:= 1)]) :-
            reset_Model_No_Train(perceptron),
            adaboost_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(train_With_Data_From_CSV_Perceptron, [true(Error =:= 0.9797958971132711)]) :-
            reset_Model_No_Train(perceptron),
            take_rows_from_iris_CSV(10, Records),
            adaboost_train(Records, 4, [0,1,0,1,1,0,1,1,1,0], 2, perceptron, 50, 0.0001, Error).
    
    test(train_With_Direct_Input_Decision_Stump, [true(Error =:= 1)]) :-
            reset_Model_No_Train(decision_stump),
            adaboost_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, decision_stump, 50, 0.0001, Error).
    
    test(train_With_Data_From_CSV_Decision_Stump, [true(Error =:= 0.9797958971132711)]) :-
            reset_Model_No_Train(decision_stump),
            take_rows_from_iris_CSV(10, Records),
            adaboost_train(Records, 4, [0,1,0,1,1,0,1,1,1,0], 2, decision_stump, 50, 0.0001, Error).
    
    test(train_After_InitTrain_Perceptron, [true(Error =:= 1)]) :-
            reset_Model_With_Train(perceptron),
            adaboost_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], 3, perceptron, 100, 0.01, Error).
    
    test(train_After_InitTrain_Decision_Stump, [true(Error =:= 1)]) :-
            reset_Model_With_Train(decision_stump),
            adaboost_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], 3, decision_stump, 100, 0.01, Error).
    
    :- end_tests(adaboost_train).
    
    run_adaboost_tests :-
            run_tests.