diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 192861a2391ca16138d560532233c7d6dcf7910b..83b069287ac4371f57a579a43cfeb83d24ad6662 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -84,5 +84,8 @@ target_link_libraries(particle_swarm_test boost_unit_test 4neuro) add_executable(particle_test tests/Particle_test.cpp) target_link_libraries(particle_test boost_unit_test 4neuro) +add_executable(NeuralNetworkSum_test tests/NeuralNetworkSum_test.cpp) +target_link_libraries(NeuralNetworkSum_test boost_unit_test 4neuro) + diff --git a/src/main.cpp b/src/main.cpp index 501274bfa9a20e6feeb48810661adbb9afa5c053..0b7edeb7f5a5710f43b178a45b115b29df367482 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,14 +44,14 @@ void test1( ){ ////////////////////// END SIMPLE EDGE WEIGHT //////////////////////////////////////// /////////////////////////BEGIN OF COMPLEX EDGE WEIGHT////////////////////////////// - std::function<double(double *, int*, int)> weight_function = [](double * weight_array, int * index_array, int n_params){ + std::function<double(double *, size_t *, size_t)> weight_function = [](double * weight_array, size_t * index_array, size_t n_params){ //w(x, y) = x + y double a = weight_array[index_array[0]]; double b = weight_array[index_array[1]]; // printf("eval: %f, %f\n", a, b); return (a + 0.0 * b); }; - int weight_indices [2] = {0, -1}; + size_t weight_indices [2] = {0, 1}; double weight_values [2] = {1.0, 5.0}; net.add_connection_general(idx1, idx2, &weight_function, weight_indices, weight_values, 2); /////////////////////////END OF COMPLEX EDGE WEIGHT////////////////////////////// diff --git a/src/tests/ConnectionWeight_test.cpp b/src/tests/ConnectionWeight_test.cpp index d7c5dd062c72ef851d92dd3d4be619b3f75c72ec..c54060fb634b0e92227a7c1617908f8e92a2ac50 100644 --- a/src/tests/ConnectionWeight_test.cpp +++ b/src/tests/ConnectionWeight_test.cpp @@ -84,6 +84,7 @@ BOOST_AUTO_TEST_SUITE(ConnectionWeight_test) std::vector<double> w_array= {2,3,4,5,6} ; double w_array2[5] = {1,2,3,4,5}; ConnectionWeight conn(5, &w_array ); + conn.eval(); int para[5]={0,1,2,3,4}; conn.SetParamIndices(para); conn.set_weights(w_array2); diff --git a/src/tests/Connection_test.cpp b/src/tests/Connection_test.cpp index d0e069083896037720bc26c81d17d12f037a5423..027c696c88e37e52175937f74bf15fd80dcc55a3 100644 --- a/src/tests/Connection_test.cpp +++ b/src/tests/Connection_test.cpp @@ -25,15 +25,12 @@ 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} ; + Neuron *neuron1 = new NeuronLinear(2, 3); + Neuron *neuron2 = new NeuronLinear(4, 5); + std::vector<double> w_array = {2, 3, 4, 5, 6}; - ConnectionWeight* conn = new ConnectionWeight(2, &w_array ); - int para[2]={0,1}; - conn->SetParamIndices(para); + ConnectionWeight *conn = new ConnectionWeight(2, &w_array); Connection connection(neuron1, neuron2, conn); - //Test of correct input neuron BOOST_CHECK_EQUAL(neuron1, connection.get_neuron_in()); //Test of correct output neuron @@ -45,19 +42,18 @@ BOOST_AUTO_TEST_SUITE(Connection_test) */ 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){ + 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 ); - int para[2]={0,1}; - conn->SetParamIndices(para); + ConnectionWeight *conn = new ConnectionWeight(2, &w_array); + Connection connection(neuron1, neuron2, conn); neuron1->activate(); diff --git a/src/tests/Graph_Elemetary_Functions_Function_Cosine_test.cpp b/src/tests/Graph_Elemetary_Functions_Function_Cosine_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32f70c8211dc3df5156cd0025bc165313eada768 --- /dev/null +++ b/src/tests/Graph_Elemetary_Functions_Function_Cosine_test.cpp @@ -0,0 +1,25 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> +#include "../Graph/ElementaryFunctions/FunctionCosine.h" + +/** + * Boost testing suite for testing FunctionCosine.h + * doesn't test inherited methods + */ +BOOST_AUTO_TEST_SUITE(FunctionCosine_test) + + BOOST_AUTO_TEST_CASE(FunctionCosine_Construction_Test) + { + FunctionCosine functionCosine(1, 2); + // BOOST_CHECK_NO_THROW(); + } + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/NeuralNetworkSum_test.cpp b/src/tests/NeuralNetworkSum_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..889f984cbfbbe9fd2159fea25397a1d6291576dd --- /dev/null +++ b/src/tests/NeuralNetworkSum_test.cpp @@ -0,0 +1,82 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author David Vojtek + * @date 2018 + */ + +#define BOOST_TEST_NO_MAIN + +#include <boost/test/unit_test.hpp> +#include "../Network/NeuralNetworkSum.h" +#include "../Neuron/NeuronLinear.h" + +/** + * Boost testing suite for testing NeuralNetworkSum.h + */ +BOOST_AUTO_TEST_SUITE(NeuralNetworkSum_test) + + /** + * Test of creating new instance of NeuralNetworkSum + */ + BOOST_AUTO_TEST_CASE(NeuralNetworkSum_constuction_test) { + //Test of none exception raise when creating new instance of NeuralNewtwork + BOOST_CHECK_NO_THROW(NeuralNetworkSum networkSum); + } + + BOOST_AUTO_TEST_CASE(NeuralNetworkSum_add_network_test) { + NeuralNetwork network; + NeuralNetworkSum networkSum; + + BOOST_CHECK_NO_THROW(networkSum.add_network(&network, 5)); + } + + BOOST_AUTO_TEST_CASE(NeuralNetworkSum_eval_single_weights_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, -1, 2.5); + + std::vector<size_t> output_neuron_indices(1); + output_neuron_indices[0] = (size_t) 1; + network.specify_output_neurons(output_neuron_indices); + + std::vector<size_t> input_neuron_indices(1); + input_neuron_indices[0] = (size_t) 0; + network.specify_input_neurons(input_neuron_indices); + + std::vector<double> input; + input.push_back(1); + std::vector<double> output; + output.push_back(1); + + double weights = 5; + NeuralNetworkSum networkSum; + networkSum.add_network(&network, 2); + + networkSum.eval_single(input, output, &weights); + BOOST_CHECK_EQUAL(44, output.at(0)); + } + + BOOST_AUTO_TEST_CASE(NeuralNetworkSum_get_weights_test) { + NeuralNetworkSum networkSum; + BOOST_CHECK_EQUAL(0, networkSum.get_n_weights()); + + 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, -1, 2.5); + + networkSum.add_network(&network, 2); + + BOOST_CHECK_EQUAL(1, networkSum.get_n_weights()); + } + + +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 index 7f5f672f086803c394c484ba10d3d7ae610c5296..95e6ebe4c73e78bc89227733ba47ccf022faa45c 100644 --- a/src/tests/NeuralNetwork_test.cpp +++ b/src/tests/NeuralNetwork_test.cpp @@ -65,12 +65,12 @@ BOOST_AUTO_TEST_SUITE(NeuralNetwork_test) 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) { + std::function<double(double *, size_t *, size_t)> f = [](double *weight_array, size_t *index_array, size_t 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}; + size_t 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); @@ -208,9 +208,9 @@ BOOST_AUTO_TEST_SUITE(NeuralNetwork_test) output.push_back(1); double weights = 5; - network.get_n_weights(); network.eval_single(input, output, &weights); BOOST_CHECK_EQUAL(22, output.at(0)); } + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/tests/NeuronLogistic_test.cpp b/src/tests/NeuronLogistic_test.cpp index 4edab726dee04646407b9f3c724cfb46a576bc50..14c150d52c83b0b620ee982c67c4957ea482feef 100644 --- a/src/tests/NeuronLogistic_test.cpp +++ b/src/tests/NeuronLogistic_test.cpp @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_SUITE(neuronLogistic_test) NeuronLogistic neuron(3.0, 2.0); neuron.activate(); //Test of correct state after activate neuron - BOOST_CHECK_CLOSE(0.0022492134466, neuron.get_state(), 0.00001); + BOOST_CHECK_CLOSE(0.0016937944, neuron.get_state(), 0.00001); } /** @@ -44,10 +44,10 @@ BOOST_AUTO_TEST_SUITE(neuronLogistic_test) NeuronLogistic neuron(3.0, 2.0); //Test of correct output of activation_function_get_derivative method - BOOST_CHECK_CLOSE(0.0042850850699, neuron.activation_function_get_derivative(), 0.00001); + BOOST_CHECK_CLOSE(0.00447566759338, neuron.activation_function_get_derivative(), 0.00001); //Tests of correct outputs of activation_function_get_partial_derivative method - 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_CLOSE(-0.0036025788498, neuron.activation_function_get_partial_derivative(0), 0.00001); + BOOST_CHECK_CLOSE(-0.0044756675933, neuron.activation_function_get_partial_derivative(1), 0.00001); BOOST_CHECK_EQUAL(0.0, neuron.activation_function_get_partial_derivative(10000)); } diff --git a/src/tests/Particle_test.cpp b/src/tests/Particle_test.cpp index 5412c453cbbd0c995ecdd84af437005293efc7d4..243fc92e0cf7f6b932b6601c224486f24687bfbd 100644 --- a/src/tests/Particle_test.cpp +++ b/src/tests/Particle_test.cpp @@ -16,13 +16,10 @@ * Boost testing suite for testing ParticleSwarm.h * TODO */ + BOOST_AUTO_TEST_SUITE(Particle_test) - - -BOOST_AUTO_TEST_SUITE(Particle_test) - - BOOST_AUTO_TEST_CASE(Particle_construction_test){ - double domain_bound[5] = {1,2,3,4,5}; + BOOST_AUTO_TEST_CASE(Particle_construction_test) { + double domain_bound[5] = {1, 2, 3, 4, 5}; Neuron *n1 = new NeuronLinear(1, 1); Neuron *n2 = new NeuronLinear(2, 2); NeuralNetwork network; @@ -53,11 +50,11 @@ BOOST_AUTO_TEST_SUITE(Particle_test) ErrorFunction *error = new MSE(&network, &dataSet); Particle particle(error, &domain_bound[0]); BOOST_CHECK_NO_THROW(Particle particle(error, &domain_bound[0])); - // particle.get_coordinate(); -} + // particle.get_coordinate(); + } - BOOST_AUTO_TEST_CASE(Particle_get_coordinate_test){ - double domain_bound[5] = {1,2,3,4,5}; + BOOST_AUTO_TEST_CASE(Particle_get_coordinate_test) { + double domain_bound[5] = {1, 2, 3, 4, 5}; Neuron *n1 = new NeuronLinear(1, 1); Neuron *n2 = new NeuronLinear(2, 2); NeuralNetwork network; @@ -93,75 +90,81 @@ BOOST_AUTO_TEST_SUITE(Particle_test) } //Random - /* BOOST_AUTO_TEST_CASE(particle_change_coordiante_test) { - double domain_bound[5] = {1,2,3,4,5}; - Neuron *n1 = new NeuronLinear(1, 1); - Neuron *n2 = new NeuronLinear(2, 2); - NeuralNetwork network; - std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec; - std::vector<double> inp, out; - - for (int i = 0; i < 1; i++) { - inp.push_back(i); - out.push_back(i + 4); - } - - data_vec.emplace_back(std::make_pair(inp, out)); - network.add_neuron(n1); - network.add_neuron(n2); - network.add_connection_simple(0, 1, 0, 2.5); - network.randomize_weights(); - - std::vector<size_t> net_input_neurons_indices(1); - std::vector<size_t> net_output_neurons_indices(1); - net_input_neurons_indices[0] = 0; - - net_output_neurons_indices[0] = 1; - - network.specify_input_neurons(net_input_neurons_indices); - network.specify_output_neurons(net_output_neurons_indices); - - DataSet dataSet(&data_vec); - ErrorFunction *error = new MSE(&network, &dataSet); - Particle particle(error, &domain_bound[0]); - particle.change_coordinate(1.0, 2.0, 2.0, &domain_bound[1], 1); - BOOST_CHECK_EQUAL(1.32664, *particle.get_coordinate()); - } - - BOOST_AUTO_TEST_CASE(particle_optimal_value_test){ - double domain_bound[5] = {1,2,3,4,5}; - Neuron *n1 = new NeuronLinear(1, 1); - Neuron *n2 = new NeuronLinear(2, 2); - NeuralNetwork network; - std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec; - std::vector<double> inp, out; - - for (int i = 0; i < 1; i++) { - inp.push_back(i); - out.push_back(i + 4); - } - - data_vec.emplace_back(std::make_pair(inp, out)); - network.add_neuron(n1); - network.add_neuron(n2); - network.add_connection_simple(0, 1, 0, 2.5); - network.randomize_weights(); - - std::vector<size_t> net_input_neurons_indices(1); - std::vector<size_t> net_output_neurons_indices(1); - net_input_neurons_indices[0] = 0; - - net_output_neurons_indices[0] = 1; - - network.specify_input_neurons(net_input_neurons_indices); - network.specify_output_neurons(net_output_neurons_indices); - - DataSet dataSet(&data_vec); - ErrorFunction *error = new MSE(&network, &dataSet); - Particle particle(error, &domain_bound[0]); - BOOST_CHECK_CLOSE(1.789708839, particle.get_optimal_value(), 0.00001 ); - - }*/ + //TODO + /* + BOOST_AUTO_TEST_CASE(particle_change_coordiante_test) { + double domain_bound[5] = {1,2,3,4,5}; + Neuron *n1 = new NeuronLinear(1, 1); + Neuron *n2 = new NeuronLinear(2, 2); + NeuralNetwork network; + std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec; + std::vector<double> inp, out; + + for (int i = 0; i < 1; i++) { + inp.push_back(i); + out.push_back(i + 4); + } + + data_vec.emplace_back(std::make_pair(inp, out)); + network.add_neuron(n1); + network.add_neuron(n2); + network.add_connection_simple(0, 1, 0, 2.5); + network.randomize_weights(); + + std::vector<size_t> net_input_neurons_indices(1); + std::vector<size_t> net_output_neurons_indices(1); + net_input_neurons_indices[0] = 0; + + net_output_neurons_indices[0] = 1; + + network.specify_input_neurons(net_input_neurons_indices); + network.specify_output_neurons(net_output_neurons_indices); + + + + DataSet dataSet(&data_vec); + ErrorFunction *error = new MSE(&network, &dataSet); + Particle particle(error, &domain_bound[0]); + particle.change_coordinate(1.0, 2.0, 2.0, &domain_bound[1], 1); + + + BOOST_CHECK_EQUAL(1.32664, *particle.get_coordinate()); + } + + BOOST_AUTO_TEST_CASE(particle_optimal_value_test){ + double domain_bound[5] = {1,2,3,4,5}; + Neuron *n1 = new NeuronLinear(1, 1); + Neuron *n2 = new NeuronLinear(2, 2); + NeuralNetwork network; + std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec; + std::vector<double> inp, out; + + for (int i = 0; i < 1; i++) { + inp.push_back(i); + out.push_back(i + 4); + } + + data_vec.emplace_back(std::make_pair(inp, out)); + network.add_neuron(n1); + network.add_neuron(n2); + network.add_connection_simple(0, 1, 0, 2.5); + network.randomize_weights(); + + std::vector<size_t> net_input_neurons_indices(1); + std::vector<size_t> net_output_neurons_indices(1); + net_input_neurons_indices[0] = 0; + + net_output_neurons_indices[0] = 1; + + network.specify_input_neurons(net_input_neurons_indices); + network.specify_output_neurons(net_output_neurons_indices); + + DataSet dataSet(&data_vec); + ErrorFunction *error = new MSE(&network, &dataSet); + Particle particle(error, &domain_bound[0]); + BOOST_CHECK_CLOSE(1.789708839, particle.get_optimal_value(), 0.00001 ); + } + */ BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file