diff --git a/src/Solvers/DESolver.cpp b/src/Solvers/DESolver.cpp index e674c0faffa6b40dd8a5a6dd03d9fab284942418..27d7bd85299d59163238594152116354d6996a7c 100644 --- a/src/Solvers/DESolver.cpp +++ b/src/Solvers/DESolver.cpp @@ -72,11 +72,11 @@ namespace lib4neuro { throw std::invalid_argument( "Parameters 'm', 'n_equations', 'n_inputs' and 'n_outputs' must be greater than zero!"); } - printf("Differential Equation Solver with %d equations\n--------------------------------------------------------------------------\n", - (int) n_equations); + std::cout <<"Differential Equation Solver with "<< (int)n_equations <<" equations"<< + std::endl <<"--------------------------------------------------------------------------"<<std::endl; - printf("Constructing NN structure representing the solution [%d input neurons][%d inner neurons]...\n", - (int) n_inputs, (int) m); + std::cout << "Constructing NN structure representing the solution [" << (int)n_inputs << " input neurons][" + << (int)m << " inner neurons]..." <<std::endl ; this->dim_i = n_inputs; this->dim_inn = m; @@ -124,18 +124,19 @@ namespace lib4neuro { for (size_t j = 0; j < this->dim_inn; ++j) { weight_idx = this->solution->add_connection_simple(first_input_neuron + i, first_inner_neuron + j, SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT); - printf(" adding a connection between input neuron %2d[%2d] and inner neuron %2d[%2d], weight index %3d\n", - (int) i, (int) (first_input_neuron + i), (int) j, (int) (first_inner_neuron + j), - (int) weight_idx); - } + std::cout << " adding a connection between input neuron " << (int)i << "[" << (int)(first_input_neuron + i) << + "] and inner neuron " << (int)j << "[" << (int)(first_inner_neuron + j) << "], weight index " << (int)weight_idx << std::endl; + + } } /* connections between inner neurons and output neurons */ for (size_t i = 0; i < this->dim_inn; ++i) { weight_idx = this->solution->add_connection_simple(first_inner_neuron + i, first_output_neuron, SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT); - printf(" adding a connection between inner neuron %2d[%2d] and output neuron %2d[%2d], weight index %3d\n", - (int) i, (int) (first_inner_neuron + i), 0, (int) (first_output_neuron), (int) weight_idx); + std::cout << " adding a connection between input neuron " << (int)i << "[" << (int)(first_input_neuron + i) << + "] and output neuron 0[" << (int)(first_output_neuron) << "], weight index " << (int)weight_idx << std::endl; + } MultiIndex initial_mi(this->dim_i); @@ -153,7 +154,7 @@ namespace lib4neuro { this->errors_functions_types = new std::vector<ErrorFunctionType>(this->n_equations); this->errors_functions_data_sets = new std::vector<DataSet *>(this->n_equations); - printf("done\n"); + std::cout<<"done"<<std::endl; } diff --git a/src/tests/DESolver_test.cpp b/src/tests/DESolver_test.cpp index 74acbac9d27bfe311fd00b3231425405d4161a99..de20352fd55f06b3b91621a4e55dcd45f8269967 100644 --- a/src/tests/DESolver_test.cpp +++ b/src/tests/DESolver_test.cpp @@ -97,13 +97,14 @@ BOOST_AUTO_TEST_SUITE(DESolver_test) BOOST_CHECK_THROW(DESolver(1, 1, 0), std::invalid_argument); BOOST_CHECK_NO_THROW(DESolver deSolver(1, 1, 1)); - /*boost::test_tools::output_test_stream output; - { - cout_redirect guard(output.rdbuf()); - DESolver deSolver(1,1,1,1); - } - BOOST_CHECK(output.is_equal("Differential Equation Solver with 1 equations\n--------------------------------------------------------------------------\nConstructing NN structure representing the solution [1 input neurons][1 inner neurons][1 output neurons]...\n adding a connection between input neuron 0 and inner neuron 0, weight index 0\n adding a connection between inner neuron 0 and output neuron 0, weight index 1\ndone\n\n")); - */ + //TODO fix it + //std::stringstream buffer1; + //std::streambuf * old1 = std::cout.rdbuf(buffer1.rdbuf()); + //DESolver deSolver(1, 1, 1); + //std::string text = buffer1.str(); + // + // // BOOST_CHECK(text._Equal("Differential Equation Solver with 1 equations\n--------------------------------------------------------------------------\nConstructing NN structure representing the solution [1 input neurons][1 inner neurons][1 output neurons]...\n adding a connection between input neuron 0 and inner neuron 0, weight index 0\n adding a connection between inner neuron 0 and output neuron 0, weight index 1\ndone\n\n")); + //std::cout.rdbuf(old1); } /** @@ -117,26 +118,25 @@ BOOST_AUTO_TEST_SUITE(DESolver_test) } BOOST_AUTO_TEST_CASE(DESolver_add_eq_test){ - DESolver *deSolver = new DESolver(1,1,1); - MultiIndex *multiIndex = new MultiIndex(2); - multiIndex->set_partial_derivative(0,1); - multiIndex->set_partial_derivative(1,0.5); + // DESolver *deSolver = new DESolver(1,1,1); + // MultiIndex *multiIndex = new MultiIndex(2); + // multiIndex->set_partial_derivative(0,1); + // multiIndex->set_partial_derivative(1,0.5); - //TODO BUG in NeuralNetworkSUM add_network Exprtwrapper //deSolver->add_to_differential_equation(0, *multiIndex, "0.5" ); - - //std::vector<double> inp, out; - //std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec_dy; - //inp = {0.0}; - //out = {8.0}; - //data_vec_dy.emplace_back(std::make_pair(inp, out)); - //DataSet ds_02(&data_vec_dy); - - //deSolver->set_error_function( 0, ErrorFunctionType::ErrorFuncMSE, &ds_02 ); - - //std::vector<double> weights; - //weights.push_back(1.0); - //BOOST_CHECK_EQUAL(64,deSolver->eval_total_error(weights)); + // + // std::vector<double> inp, out; + // std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec_dy; + // inp = {0.0}; + // out = {8.0}; + // data_vec_dy.emplace_back(std::make_pair(inp, out)); + // DataSet ds_02(&data_vec_dy); + + // deSolver->set_error_function( 0, ErrorFunctionType::ErrorFuncMSE, &ds_02 ); + + // std::vector<double> weights; + // weights.push_back(1.0); + // BOOST_CHECK_EQUAL(64,deSolver->eval_total_error(weights)); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/tests/DataSet_test.cpp b/src/tests/DataSet_test.cpp index f9d6746537f43f84b45cf2ca6e73781c4e630edb..be0f1e4dbbbb38b13855817fa8648dd988566efc 100644 --- a/src/tests/DataSet_test.cpp +++ b/src/tests/DataSet_test.cpp @@ -10,11 +10,9 @@ #include "boost_unit_tests_preamble.h" #include "../DataSet/DataSet.h" -//#include <cstdio> -//#include <iostream> - -//#include <boost/filesystem.hpp> +#include <iostream> +//#include <boost/archive/archive_exception.hpp> using namespace lib4neuro; /** @@ -22,17 +20,7 @@ using namespace lib4neuro; */ BOOST_AUTO_TEST_SUITE(DataSet_test) - struct cout_redirect { - cout_redirect(std::streambuf *new_buffer) - : old(std::cout.rdbuf(new_buffer)) {} - - ~cout_redirect() { - std::cout.rdbuf(old); - } - - private: - std::streambuf *old; - }; + /** * Test of DataSet constructor with filepath parameter @@ -40,8 +28,9 @@ BOOST_AUTO_TEST_SUITE(DataSet_test) BOOST_AUTO_TEST_CASE(DataSet_construction_from_file_test) { //test of exception with non-existing file path //TODO resolve exception throw - // DataSet dataSet("file/unknown"); - //BOOST_CHECK_THROW(DataSet dataSet("file unknown"), boost::archive::archive_exception::input_stream_error); + //DataSet dataSet("file/unknown"); + + //BOOST_CHECK_THROW(DataSet dataSet("file unknown"), std::out_of_range);// boost::archive::archive_exception::input_stream_error); } /** @@ -80,7 +69,6 @@ BOOST_AUTO_TEST_SUITE(DataSet_test) DataSet dataSet(&data_vec); //test of equal data - //TODO out of range, == BOOST_CHECK_EQUAL(0, dataSet.get_data()->at(0).first.at(0)); BOOST_CHECK_EQUAL(4, dataSet.get_data()->at(0).second.at(0)); @@ -170,8 +158,7 @@ BOOST_AUTO_TEST_SUITE(DataSet_test) * Test of print_data method */ BOOST_AUTO_TEST_CASE(DataSet_print_data_test) { - //TODO this test causes problems on windows machines - /* + std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec; std::vector<double> inp, out; @@ -181,18 +168,17 @@ BOOST_AUTO_TEST_SUITE(DataSet_test) } data_vec.emplace_back(std::make_pair(inp, out)); - DataSet dataSet(&data_vec); - - boost::test_tools::output_test_stream output; - { - cout_redirect guard(output.rdbuf()); - dataSet.print_data(); - } + std::stringstream buffer; + std::streambuf * old = std::cout.rdbuf(buffer.rdbuf()); + dataSet.print_data(); + //Test of correct print of DataSet - BOOST_CHECK(output.is_equal("0 -> 4 \n")); - */ + std::string text = buffer.str(); + BOOST_CHECK(text._Equal("0 -> 4 \n")); + std::cout.rdbuf(old); + } /** @@ -215,7 +201,6 @@ BOOST_AUTO_TEST_SUITE(DataSet_test) dataSet.store_text(filename); //Test of correct file creations - //BOOST_CHECK(boost::filesystem::exists( "testDataSet" )); DataSet newDataSet("testDataSet"); diff --git a/src/tests/NeuralNetworkSum_test.cpp b/src/tests/NeuralNetworkSum_test.cpp index f449a4193c6334320ecd65e561193746446ab5c4..b51aea9721e1d7e090389d95f3b0db6a12fa0de7 100644 --- a/src/tests/NeuralNetworkSum_test.cpp +++ b/src/tests/NeuralNetworkSum_test.cpp @@ -58,26 +58,14 @@ BOOST_AUTO_TEST_SUITE(NeuralNetworkSum_test) mock_network network; //NeuralNetwork network; NeuralNetworkSum networkSum; - std::string po = "x"; - // BOOST_CHECK_NO_THROW(networkSum.add_network(&network, po)); + std::string po = "f(x,y,z,t) =x+y+z+t"; + BOOST_CHECK_NO_THROW(networkSum.add_network(&network, po)); } - BOOST_AUTO_TEST_CASE(NeuralNetworkSum_eval_single_weights_test) { - Neuron *n1 = new NeuronLinear(); - Neuron *n2 = new NeuronLinear(); - NeuralNetwork network; - network.add_neuron(n1); - network.add_neuron(n2); - - network.add_connection_simple(0, 1, SIMPLE_CONNECTION_TYPE::UNITARY_WEIGHT, 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); + BOOST_AUTO_TEST_CASE(NeuralNetworkSum_eval_single_weights_test) { + + mock_network network; + MOCK_EXPECT(network.eval_single); std::vector<double> input; input.push_back(1); @@ -86,28 +74,22 @@ BOOST_AUTO_TEST_SUITE(NeuralNetworkSum_test) double weights = 5; NeuralNetworkSum networkSum; - networkSum.add_network(&network, "2"); + networkSum.add_network(&network, "f(x) =x"); networkSum.eval_single(input, output); - BOOST_CHECK_EQUAL(2, output.at(0)); + BOOST_CHECK_EQUAL(0, 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(); - Neuron *n2 = new NeuronLinear(); - NeuralNetwork network; - network.add_neuron(n1); - network.add_neuron(n2); - - network.add_connection_simple(0, 1, SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT, 2.5); - - networkSum.add_network(&network, "2"); + mock_network network; + MOCK_EXPECT(network.get_n_weights).returns(1); + networkSum.add_network(&network, "f(x) =x"); BOOST_CHECK_EQUAL(1, networkSum.get_n_weights()); } -*/ + 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 index 5bdd215d22f989078e57299e52834d5c751a985e..4358bdb52d1a90ed200e70a775d27241e7d929c0 100644 --- a/src/tests/ParticleSwarm_test.cpp +++ b/src/tests/ParticleSwarm_test.cpp @@ -61,8 +61,6 @@ BOOST_AUTO_TEST_SUITE(ParticleSwarm_test) MOCK_EXPECT(error.get_dimension).returns(5); MOCK_EXPECT(error.eval).returns(0.8); - //MOCK_EXPECT(error.change_coordinate).once.returns(0.2); - // ParticleSwarm swarm(&domain_bound, 0, 1, 1, 1, 1, 1, 5, 20);