Commit 18281563 authored by David Vojtek's avatar David Vojtek

new NNSum unit test + fix of other tests

parent 0adddfc7
......@@ -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)
......@@ -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//////////////////////////////
......
......@@ -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);
......
......@@ -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();
......
/**
* 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
/**
* 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
......@@ -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
......@@ -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));
}
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment