diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9a93e97bf2c4b780fab5a8d571419c86717c0606..53de9e8d1cbbe6c86c0f8c2a4c265641abeca920 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,7 +31,7 @@ add_library(boost_unit_test SHARED tests/boost_test_lib_dummy.cpp) add_executable(neuron_test tests/neuron_test.cpp) target_link_libraries(neuron_test boost_unit_test neuron) -add_executable(linear_neuron_test tests/NeuronLinear_test.cpp) +add_executable(linear_neuron_test tests/NeuronLinear_test.cpp tests/NeuronBinary_test.cpp tests/NeuronLogistic_test.cpp tests/NeuronTanh.cpp tests/ConnectionWeight_test.cpp tests/Connection_test.cpp tests/NeuralNetwork_test.cpp tests/ConnectionWeightIdentity_test.cpp tests/ParticleSwarm_test.cpp tests/Particle_test.cpp) target_link_libraries(linear_neuron_test boost_unit_test neuron) add_executable(test_cases main.cpp) diff --git a/src/LearningMethods/ParticleSwarm.cpp b/src/LearningMethods/ParticleSwarm.cpp index 24d3a0451b712491411b943e3b122c486e2145b0..2b9421715c54d968ce1172ecf8fe9cf8f7ae317e 100644 --- a/src/LearningMethods/ParticleSwarm.cpp +++ b/src/LearningMethods/ParticleSwarm.cpp @@ -10,6 +10,13 @@ #include <stdexcept> #include "ParticleSwarm.h" +/** + * TODO + * domain_bound out_of_range check + * @param f_dim + * @param domain_bounds + * @param F + */ Particle::Particle(unsigned int f_dim, double *domain_bounds, double (*F)(double*)) { this->coordinate_dim = f_dim; diff --git a/src/NetConnection/ConnectionWeight.cpp b/src/NetConnection/ConnectionWeight.cpp index 3f56104256f9e6d173cdec91d05b27f78a645649..c05b7e18a07c4ce9ecba1c88594d033068da9956 100644 --- a/src/NetConnection/ConnectionWeight.cpp +++ b/src/NetConnection/ConnectionWeight.cpp @@ -16,7 +16,6 @@ ConnectionWeight::ConnectionWeight() { ConnectionWeight::ConnectionWeight(int param_count, std::vector<double>* w_array, std::function<double(double *, int*, int)> *f) { this->param_indices = new int[param_count]; this->n_params = param_count; - this->weight_array = w_array; this->weight_function = f; @@ -35,6 +34,10 @@ void ConnectionWeight::adjust_weights(double *values) { } } +std::vector<double> ConnectionWeight::get_weights(){ + return *this->weight_array; +} + void ConnectionWeight::set_weights(double *values) { for(int i = 0; i < this->n_params; ++i){ this->weight_array->at(this->param_indices[i]) = values[i]; @@ -53,4 +56,5 @@ void ConnectionWeight::SetParamIndex(int value, int idx) { double ConnectionWeight::eval() { return (*this->weight_function)(&this->weight_array->at(0),this->param_indices, this->n_params); -} \ No newline at end of file +} + diff --git a/src/NetConnection/ConnectionWeight.h b/src/NetConnection/ConnectionWeight.h index 8a34b04219f875875b61d347476c7dfb5aa926d3..6d771a40a979502e64f7174b4c0b16d324a5a7c6 100644 --- a/src/NetConnection/ConnectionWeight.h +++ b/src/NetConnection/ConnectionWeight.h @@ -75,8 +75,14 @@ public: * * @param values */ + void set_weights(double *values); + /** + * + * @return vector of weights + */ + std::vector<double> get_weights(); /** * * @param values diff --git a/src/Neuron/Neuron.cpp b/src/Neuron/Neuron.cpp index 5f3bad2de5cfed67fa16231bdac5a296f351c2eb..2105f28faa13999a6cab527dd8ee2a8afca44dce 100644 --- a/src/Neuron/Neuron.cpp +++ b/src/Neuron/Neuron.cpp @@ -87,7 +87,12 @@ void Neuron::activation_function_set_parameter(int param_idx, double value) { this->activation_function_parameters[param_idx] = value; } + double Neuron::activation_function_get_parameter(int param_idx) { + /** + * TODO + * Check out of range + */ return this->activation_function_parameters[param_idx]; } diff --git a/src/Neuron/NeuronLogistic.cpp b/src/Neuron/NeuronLogistic.cpp index b3b398dc94c3b9c576eec98d2e7e9f8665940f3c..d896d23a4d9495abe92d54132db257359de6e5e2 100644 --- a/src/Neuron/NeuronLogistic.cpp +++ b/src/Neuron/NeuronLogistic.cpp @@ -41,7 +41,10 @@ double NeuronLogistic::activation_function_get_partial_derivative(int param_idx return exa * std::log(ex + 1.0); } else if(param_idx == 1){ - + /** + * TODO + * Could be write as activation_function_get_derivative() * -1 + */ double ex = std::pow(E, a - x); double ex2 = std::pow(ex + 1.0, -b - 1.0); diff --git a/src/Neuron/NeuronTanh.cpp b/src/Neuron/NeuronTanh.cpp index 74d9258329f11393efa367acf2963603169d8f1d..690059b8e1c1e4b5fccf7f707b85a7d6299ebf04 100644 --- a/src/Neuron/NeuronTanh.cpp +++ b/src/Neuron/NeuronTanh.cpp @@ -31,7 +31,12 @@ double NeuronTanh::activation_function_get_partial_derivative(int param_idx) { double a = this->activation_function_parameters[0]; double x = this->potential; + if(param_idx == 0){ + /** + * TODO + * Same as activation_function_get_derivative() + */ double ex = -4.0 * std::pow(E, 2.0 * (x + a)); double exi = std::pow(E, 2.0 * a) + std::pow(E, 2.0 * x); diff --git a/src/Neuron/NeuronTanh.h b/src/Neuron/NeuronTanh.h index a3063c297d891841602a76ea293e4cbfe8c15857..bcd354da6ebcd850478896821431d86a0b906c12 100644 --- a/src/Neuron/NeuronTanh.h +++ b/src/Neuron/NeuronTanh.h @@ -47,6 +47,8 @@ public: double activation_function_get_partial_derivative(int param_idx) override; /** + * TODO + * Wrong return statement, there is no b * Calculates d/dx of (e^(x-a) - e^(a-x))/(e^(x-a) + e^(a-x)) * @return a * e^(b - x) * [e^(b - x) + 1]^(-a) */ diff --git a/src/tests/ConnectionWeightIdentity_test.cpp b/src/tests/ConnectionWeightIdentity_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eda28c787c8ee8d7fe4ccf8085b76b70f6c65447 --- /dev/null +++ b/src/tests/ConnectionWeightIdentity_test.cpp @@ -0,0 +1,33 @@ +/** +* DESCRIPTION OF THE CLASS +* +* @author David Vojtek +* @date 2018 +*/ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + +#include "../NetConnection/ConnectionWeightIdentity.h" +/** + * Boost testing suite for testing ConnectionWeightIdentity.h + */ + + BOOST_AUTO_TEST_SUITE(ConnectionWeightIdentity_test) + +BOOST_AUTO_TEST_CASE(ConnectionWeightIdentity_construction_test){ + std::vector<double> weight_array = {1,2,3,4,5}; + BOOST_CHECK_NO_THROW(ConnectionWeightIdentity CWI(&weight_array)); + + } + + BOOST_AUTO_TEST_CASE(ConnectionWeightIdentity_eval_test){ + std::vector<double> weight_array = {1,2,3,4,5}; + ConnectionWeightIdentity CWI(&weight_array); + int para[5]={3,1,2,3,4}; + CWI.SetParamIndices(para); + BOOST_CHECK_EQUAL(4,CWI.eval()); + } + + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/tests/ConnectionWeight_test.cpp b/src/tests/ConnectionWeight_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7c86ac166b41c3b0f69f08024462f728e90fcf8 --- /dev/null +++ b/src/tests/ConnectionWeight_test.cpp @@ -0,0 +1,101 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + + +#include "../NetConnection/ConnectionWeight.h" +/** + * Boost testing suite for testing ConnectionWeight.h + * TODO + */ + +BOOST_AUTO_TEST_SUITE(ConnectionWeight_test) + + BOOST_AUTO_TEST_CASE(ConnectionWeight_construction__test) { + std::vector<double> * w_array = nullptr ; + std::function<double(double *, int*, int)> *f= nullptr; + BOOST_CHECK_NO_THROW( ConnectionWeight conn(2, w_array, f )); + BOOST_CHECK_NO_THROW( ConnectionWeight conn); + + } + + BOOST_AUTO_TEST_CASE(ConnectionWeight_param_test){ + std::vector<double> w_array= {0,1,2,3,4} ; + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = 5; + double b = 4; + return (a + 1.5 * b); + }; + ConnectionWeight conn(5, &w_array, &f ); + int para[5]={0,1,2,3,4}; + BOOST_CHECK_NO_THROW(conn.SetParamIndices(para)); + BOOST_CHECK_NO_THROW(conn.SetParamIndex(2,2)); +} + BOOST_AUTO_TEST_CASE(ConnectionWeight_param_out_of_range_test){ + std::vector<double> w_array= {0,1,2,3,4} ; + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = 5; + double b = 4; + return (a + 1.5 * b); + }; + ConnectionWeight conn(7, &w_array, &f ); + int para[5]={0,1,2,3,4}; + //para out of range + BOOST_CHECK_THROW(conn.SetParamIndices(para), std::out_of_range); + //paramIndex out of range + BOOST_CHECK_THROW(conn.SetParamIndex(2,8), std::out_of_range); + } + + + BOOST_AUTO_TEST_CASE(ConnectionWeight_eval__test) { + std::vector<double> w_array= {2,3,4,5,6} ; + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = weight_array[0]; + double b = weight_array[1]; + return (a + 1.5 * b); + }; + ConnectionWeight conn(5, &w_array, &f ); + int para[5]={4,8,2,3,4}; + conn.SetParamIndices(para); + BOOST_CHECK_EQUAL(6.5, conn.eval()); + } + + BOOST_AUTO_TEST_CASE(ConnectionWeight_weight_adjustment_test) { + std::vector<double> w_array= {2,3,4,5,6} ; + double w_array2[5] = {1,2,3,4,5}; + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = weight_array[0]; + double b = weight_array[1]; + return (a + 1.5 * b); + }; + ConnectionWeight conn(5, &w_array, &f ); + int para[5]={0,1,2,3,4}; + conn.SetParamIndices(para); + conn.adjust_weights(w_array2); + BOOST_CHECK_EQUAL(10.5, conn.eval()); + } + + BOOST_AUTO_TEST_CASE(ConnectionWeight_weight_set_test) { + std::vector<double> w_array= {2,3,4,5,6} ; + double w_array2[5] = {1,2,3,4,5}; + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = weight_array[0]; + double b = weight_array[1]; + return (a + 1.5 * b); + }; + ConnectionWeight conn(5, &w_array, &f ); + int para[5]={0,1,2,3,4}; + conn.SetParamIndices(para); + conn.set_weights(w_array2); + BOOST_CHECK_EQUAL(4, conn.eval()); + } + + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/Connection_test.cpp b/src/tests/Connection_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27d167cd254f9b4c63c66f06bd19fb163835445a --- /dev/null +++ b/src/tests/Connection_test.cpp @@ -0,0 +1,71 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + + +#include "../NetConnection/Connection.h" +#include "../NetConnection/ConnectionWeight.h" +#include "../Neuron/NeuronLinear.h" +#include <iostream> +/** + * Boost testing suite for testing Connection.h + */ + +BOOST_AUTO_TEST_SUITE(Connection_test) + + BOOST_AUTO_TEST_CASE(Connection_construction__test) { + + Neuron* neuron1 = new NeuronLinear(2,3); + Neuron* neuron2 = new NeuronLinear(4,5); + std::vector<double> w_array= {2,3,4,5,6} ; + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = weight_array[0]; + double b = weight_array[1]; + return (a + 1.5 * b); + }; + + + ConnectionWeight* conn = new ConnectionWeight(2, &w_array, &f ); + int para[2]={0,1}; + conn->SetParamIndices(para); + Connection connection(neuron1, neuron2, conn); + + BOOST_CHECK_EQUAL(neuron1, connection.get_neuron_in()); + BOOST_CHECK_EQUAL(neuron2, connection.get_neuron_out()); + } + + BOOST_AUTO_TEST_CASE(Connection_pass_signal_test) { + + Neuron* neuron1 = new NeuronLinear(2,3); + Neuron* neuron2 = new NeuronLinear(4,5); + std::vector<double> w_array= {2,3,4,5,6} ; + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = weight_array[0]; + double b = weight_array[1]; + return (a + 1.5 * b); + }; + + + ConnectionWeight* conn = new ConnectionWeight(2, &w_array, &f ); + int para[2]={0,1}; + conn->SetParamIndices(para); + Connection connection(neuron1, neuron2, conn); + + neuron1->activate(); + neuron2->activate(); + BOOST_CHECK_EQUAL(4, neuron2->get_state()); + connection.pass_signal(); + neuron2->activate(); + BOOST_CHECK_EQUAL(69, neuron2->get_state()); + + } + + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/NeuralNetwork_test.cpp b/src/tests/NeuralNetwork_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6bdb59894d93d0c1ea3d50e8233bb6258e9f57c7 --- /dev/null +++ b/src/tests/NeuralNetwork_test.cpp @@ -0,0 +1,72 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + +#include "../Network/NeuralNetwork.h" +#include "../Neuron/NeuronLinear.h" +/** + * Boost testing suite for testing NeuralNetwork.h + */ +BOOST_AUTO_TEST_SUITE(NeuralNetwork_test) + + BOOST_AUTO_TEST_CASE(NeuralNetwork_constuction_test){ + BOOST_CHECK_NO_THROW(NeuralNetwork network); + } + BOOST_AUTO_TEST_CASE(NeuralNetwork_add_neuron_test){ + Neuron* n1 = new NeuronLinear(1,1); + Neuron* n2 = new NeuronLinear(2,2); + Neuron* n3 = new NeuronLinear(3,3); + Neuron* n4 = new NeuronLinear(4,4); + NeuralNetwork network; + + BOOST_CHECK_EQUAL(0,network.add_neuron(n1)); + BOOST_CHECK_EQUAL(1,network.add_neuron(n2)); + BOOST_CHECK_EQUAL(2,network.add_neuron(n3)); + BOOST_CHECK_EQUAL(3,network.add_neuron(n4)); + } + + BOOST_AUTO_TEST_CASE(NeuralNetwork_add_connection_simple_test){ + Neuron* n1 = new NeuronLinear(1,1); + Neuron* n2 = new NeuronLinear(2,2); + NeuralNetwork network; + network.add_neuron(n1); + network.add_neuron(n2); + + network.add_connection_simple(0,1,0,2.5); + BOOST_CHECK_EQUAL(1, n1->get_connections_out()->size()); + BOOST_CHECK_EQUAL(1, n2->get_connections_in()->size()); + BOOST_CHECK_THROW(network.add_connection_simple(2,0,0,0), std::out_of_range); + BOOST_CHECK_THROW(network.add_connection_simple(0,2,0,0), std::out_of_range); + BOOST_CHECK_THROW(network.add_connection_simple(0,1,-1,0), std::out_of_range); + + + } + + BOOST_AUTO_TEST_CASE(NeuralNetwork_add_connection_general_test){ + NeuralNetwork network; + Neuron* n1 = new NeuronLinear(1,1); + Neuron* n2 = new NeuronLinear(2,2); + network.add_neuron(n1); + network.add_neuron(n2); + std::function<double(double *, int*, int)> f= [](double * weight_array, int * index_array, int n_params){ + double a = weight_array[0]; + double b = weight_array[1]; + return (a + 1.5 * b); + }; + int para[5]={0,1,2,3,4}; + double w_array[5] = {1,2,3,4,5}; + + network.add_connection_general(0,1, &f, para, w_array, 5); + BOOST_CHECK_EQUAL(1, n1->get_connections_out()->size()); + BOOST_CHECK_EQUAL(1, n2->get_connections_in()->size()); + BOOST_CHECK_THROW(network.add_connection_general(2,1, &f, para, w_array, 5), std::out_of_range); + BOOST_CHECK_THROW(network.add_connection_general(0,2, &f, para, w_array, 5), std::out_of_range); + } +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/NeuronBinary_test.cpp b/src/tests/NeuronBinary_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95ebc0de4c9ba64e49d0d16d3a179bc50e4b449f --- /dev/null +++ b/src/tests/NeuronBinary_test.cpp @@ -0,0 +1,34 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + +#include "../Neuron/NeuronBinary.h" +/** + * Boost testing suite for testing NeuronBinary.h + * doesn't test inherited methods + */ +BOOST_AUTO_TEST_SUITE(neuronBinary_test) + BOOST_AUTO_TEST_CASE(neuronLinear_construction__test) { + NeuronBinary neuron(1.745); + BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(0), 1.745); + } + + BOOST_AUTO_TEST_CASE(neuronLinear_activate__test) { + NeuronBinary neuron(1.75); + neuron.activate(); + BOOST_CHECK_EQUAL(0.0, neuron.get_state()); + neuron.set_potential(1.76); + neuron.activate(); + BOOST_CHECK_EQUAL(1.0, neuron.get_state()); + + } + + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/NeuronLinear_test.cpp b/src/tests/NeuronLinear_test.cpp index f8b974042dd8e8f8f6cf66d8c63450cc87d71f17..b2d76a919d1c8af6a0467eb714c5fe8fe37257c0 100644 --- a/src/tests/NeuronLinear_test.cpp +++ b/src/tests/NeuronLinear_test.cpp @@ -4,25 +4,35 @@ * @author David Vojtek * @date 2018 */ -#define BOOST_TEST_DYN_LINK -#define BOOST_TEST_MODULE neuronLinear_test + +#define BOOST_TEST_NO_MAIN + #include <boost/test/unit_test.hpp> -#include "../Neuron/NeuronLinear.h" +#include "../Neuron/NeuronLinear.h" /** * Boost testing suite for testing NeuronLinear.h - * doesn't test inherit methods + * doesn't test inherited methods */ BOOST_AUTO_TEST_SUITE(neuronLinear_test) BOOST_AUTO_TEST_CASE(neuronLinear_construction__test) { NeuronLinear neuron(1.745, 784.4547); - BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(0), 1.745); - BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(0), 784.4547); - - - }; + BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(1), 784.4547); + } + BOOST_AUTO_TEST_CASE(neuronLinear_activate__test) { + NeuronLinear neuron(5.0, 1.0); + neuron.activate(); + BOOST_CHECK_EQUAL(5.0, neuron.get_state()); + } + BOOST_AUTO_TEST_CASE(neuronLinear_derivative_test) { + NeuronLinear neuron(5.0, 3.0); + BOOST_CHECK_EQUAL(3.0, neuron.activation_function_get_derivative()); + BOOST_CHECK_EQUAL(1.0, neuron.activation_function_get_partial_derivative(0)); + BOOST_CHECK_EQUAL(0.0, neuron.activation_function_get_partial_derivative(1)); + BOOST_CHECK_EQUAL(0.0, neuron.activation_function_get_partial_derivative(10000)); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/tests/NeuronLogistic_test.cpp b/src/tests/NeuronLogistic_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d2659291314a3f406fa8a883736591b8b373592 --- /dev/null +++ b/src/tests/NeuronLogistic_test.cpp @@ -0,0 +1,41 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + +#include "../Neuron/NeuronLogistic.h" +/** + * Boost testing suite for testing NeuronLogistic.h + * doesn't test inherited methods + */ + + +BOOST_AUTO_TEST_SUITE(neuronLogistic_test) + BOOST_AUTO_TEST_CASE(neuronLogistic_construction__test) { + NeuronLogistic neuron(1.745, 784.4547); + BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(0), 1.745); + BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(1), 784.4547); + } + + BOOST_AUTO_TEST_CASE(neuronLogistic_activate__test) { + NeuronLogistic neuron(3.0, 2.0); + neuron.activate(); + BOOST_CHECK_CLOSE(0.0022492134466, neuron.get_state(), 0.00001); + } + BOOST_AUTO_TEST_CASE(neuronLogistic_derivative_test){ + NeuronLogistic neuron(3.0, 2.0); + BOOST_CHECK_CLOSE(0.0042850850699, neuron.activation_function_get_derivative(), 0.00001); + BOOST_CHECK_CLOSE(-0.0068569236644, neuron.activation_function_get_partial_derivative(0), 0.00001); + BOOST_CHECK_CLOSE(-0.0042850850699, neuron.activation_function_get_partial_derivative(1), 0.00001); + BOOST_CHECK_EQUAL(0.0, neuron.activation_function_get_partial_derivative(10000)); + +} + + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/NeuronTanh.cpp b/src/tests/NeuronTanh.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9998504696ce389a8bc1ed1aa3c7f949feb356de --- /dev/null +++ b/src/tests/NeuronTanh.cpp @@ -0,0 +1,39 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + +#include "../Neuron/NeuronTanh.h" +/** + * Boost testing suite for testing NeuronTanh.h + * doesn't test inherited methods + */ +BOOST_AUTO_TEST_SUITE(neuronTanh_test) + + BOOST_AUTO_TEST_CASE(neuronTanh_construction__test) { + NeuronTanh neuron(1.745); + BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(0), 1.745); + } + + BOOST_AUTO_TEST_CASE(neuronTanh_activate__test) { + NeuronTanh neuron(2.0); + neuron.activate(); + BOOST_CHECK_CLOSE(-0.96402758007581, neuron.get_state(), 0.00001); + } + BOOST_AUTO_TEST_CASE(neuronTanh_derivative_test){ + NeuronTanh neuron(2.0); + BOOST_CHECK_CLOSE(-0.0706508248531644, neuron.activation_function_get_derivative(), 0.00001); + BOOST_CHECK_CLOSE(-0.0706508248531644, neuron.activation_function_get_partial_derivative(0), 0.00001); + BOOST_CHECK_EQUAL(0.0, neuron.activation_function_get_partial_derivative(10000)); + + } + + + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/ParticleSwarm_test.cpp b/src/tests/ParticleSwarm_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b778788b0162849b57d5d81619c9254da07c72a7 --- /dev/null +++ b/src/tests/ParticleSwarm_test.cpp @@ -0,0 +1,37 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + + +#include "../LearningMethods/ParticleSwarm.h" +/** + * Boost testing suite for testing ParticleSwarm.h + */ + +double test_particle_swarm_neural_net_error_function(double *weights){ + + + } + +BOOST_AUTO_TEST_SUITE(ParticleSwarm_test) + + /** + * TODO + */ + BOOST_AUTO_TEST_CASE(ParticleSwarm_construction_test){ + double domain_bound = 5; + double fun= 1; + int tmp=5; + double (*F)(double*) = &test_particle_swarm_neural_net_error_function; + + //BOOST_CHECK_NO_THROW(ParticleSwarm swarm(F, 2, &domain_bound, 0, 1, 1, 0, 20)); + } + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/Particle_test.cpp b/src/tests/Particle_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfc57aab7a33b3728eca06da84c8ddbfdfab79ed --- /dev/null +++ b/src/tests/Particle_test.cpp @@ -0,0 +1,33 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> + + +#include "../LearningMethods/ParticleSwarm.h" +/** + * Boost testing suite for testing ParticleSwarm.h + */ + +double test_particle_function(double *weights){ + + +} + +BOOST_AUTO_TEST_SUITE(Particle_test) + BOOST_AUTO_TEST_CASE(Particle_construction_test){ + double domain_bound[5] = {1,2,3,4,5}; + double (*F)(double*) = &test_particle_function; + Particle particle(1, &domain_bound[0], F); + // particle.get_coordinate(); +} + + + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/neuron_test.cpp b/src/tests/neuron_test.cpp index 01b93c8eaefd8289946ca25e2572f833fe6679cd..d092c5708faf2f3fa9c6c45b560fe3543086deb0 100644 --- a/src/tests/neuron_test.cpp +++ b/src/tests/neuron_test.cpp @@ -5,10 +5,10 @@ * @date 2018 */ -#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_NO_MAIN -#define BOOST_TEST_MODULE neuron_test #include <boost/test/unit_test.hpp> + #include "../Neuron/Neuron.h" #include "../Neuron/NeuronLinear.h" /** @@ -22,21 +22,21 @@ BOOST_AUTO_TEST_SUITE(neuron_test) BOOST_AUTO_TEST_CASE(neuron_saturation_test) { NeuronLinear neuron(0, 0); - BOOST_CHECK_EQUAL(neuron.is_saturated_in(), false); - BOOST_CHECK_EQUAL(neuron.is_saturated_out(), false); - neuron.set_saturation_in(123); - neuron.set_saturation_out(132); BOOST_CHECK_EQUAL(neuron.is_saturated_in(), true); BOOST_CHECK_EQUAL(neuron.is_saturated_out(), true); - neuron.adjust_saturation_in(-123); - neuron.adjust_saturation_out(-123); + neuron.set_saturation_in(123); + neuron.set_saturation_out(123); BOOST_CHECK_EQUAL(neuron.is_saturated_in(), false); BOOST_CHECK_EQUAL(neuron.is_saturated_out(), false); + neuron.adjust_saturation_in(-123); + neuron.adjust_saturation_out(-123); + BOOST_CHECK_EQUAL(neuron.is_saturated_in(), true); + BOOST_CHECK_EQUAL(neuron.is_saturated_out(), true); }; /** * Test of potencial methods */ -BOOST_AUTO_TEST_CASE(neuron_potencial_test) { +BOOST_AUTO_TEST_CASE(neuron_potential_test) { NeuronLinear neuron(0, 0); BOOST_CHECK_EQUAL(neuron.get_potential(), 0); @@ -60,10 +60,7 @@ BOOST_AUTO_TEST_CASE(neuron_state_test) BOOST_CHECK_EQUAL(neuron.get_state(),(size_t)0); neuron.set_state(1.123456789); BOOST_CHECK_EQUAL(neuron.get_state(), 1.123456789); - neuron.set_state(-0.123456789); - BOOST_CHECK_EQUAL(neuron.get_state(), 1); - BOOST_CHECK_EQUAL(neuron.get_state(), 1.0); - BOOST_CHECK_EQUAL(neuron.get_state(), (size_t)1); + }; @@ -74,29 +71,27 @@ BOOST_AUTO_TEST_CASE(neuron_activation_function_test) { NeuronLinear neuron(0,0); - BOOST_CHECK_EQUAL(neuron.activation_function_get_n_parameters(), 0); - BOOST_CHECK_THROW(neuron.activation_function_get_parameter(0), std::out_of_range); + BOOST_CHECK_EQUAL(neuron.activation_function_get_n_parameters(), 2); + BOOST_CHECK_THROW(neuron.activation_function_get_parameter(5), std::out_of_range); neuron.activation_function_set_parameter(0,41.154); BOOST_CHECK_EQUAL(neuron.activation_function_get_parameter(0), 41.154); - BOOST_CHECK_EQUAL(neuron.activation_function_get_n_parameters(), 1); + BOOST_CHECK_EQUAL(neuron.activation_function_get_n_parameters(), 2); }; -/** - * TODO - */ -BOOST_AUTO_TEST_CASE(neuron_connection_in_test){ - NeuronLinear neuron(0,0); - // ConnectionWeight weight = new ConnectionWeight() - // Connection con = new Connection( neuron, neuron , ) - - BOOST_CHECK_THROW(neuron.get_connections_in(), std::nullptr_t); -} -/** - * TODO - */ -BOOST_AUTO_TEST_CASE(neuron_destructor_test){ +BOOST_AUTO_TEST_CASE(neuron_connection_test){ + Neuron* neuron1 = new NeuronLinear(1,1); + Neuron* neuron2 = new NeuronLinear(2,2); + Connection con(neuron1, neuron2, new ConnectionWeight()); + neuron1->add_connection_out(&con); + neuron2->add_connection_in(&con); + BOOST_CHECK_EQUAL(1, neuron1->get_connections_out()->size()); + BOOST_CHECK_EQUAL(1, neuron2->get_connections_in()->size()); + BOOST_CHECK_EQUAL(&con, neuron1->get_connections_out()->at(0)); + BOOST_CHECK_EQUAL(&con, neuron2->get_connections_in()->at(0)); } + BOOST_AUTO_TEST_SUITE_END() +