Commit 17b46d1b authored by Martin Beseda's avatar Martin Beseda

Trying to merge

parents 7ce82bef ea4ebfb2
image: debian:latest
before_script:
- echo "deb http://ftp.us.debian.org/debian unstable main contrib non-free" >> /etc/apt/sources.list.d/unstable.list
- apt-get update
- apt-get install -y -t unstable gcc-5
- apt-get install -y -qq make cmake libboost-test-dev
- ./build.sh
run_tests:
script: "./run_tests.sh"
image: ubuntu:latest
before_script:
- apt-get update
- apt-get install -y -qq make cmake libboost-test-dev g++-5
- ./build.sh
run_tests:
script: "./run_tests.sh"
......@@ -34,7 +34,33 @@ target_link_libraries(neuron_test boost_unit_test neuron)
add_executable(linear_neuron_test tests/NeuronLinear_test.cpp)
target_link_libraries(linear_neuron_test boost_unit_test neuron)
add_executable(test_cases main.cpp)
add_executable(binary_neuron_test tests/NeuronBinary_test.cpp)
target_link_libraries(binary_neuron_test boost_unit_test neuron)
add_executable(logistic_neuron_test tests/NeuronLogistic_test.cpp)
target_link_libraries(logistic_neuron_test boost_unit_test neuron)
add_executable(tanh_neuron_test tests/NeuronTanh.cpp)
target_link_libraries(tanh_neuron_test boost_unit_test neuron)
add_executable(connection_weight_test tests/ConnectionWeight_test.cpp)
target_link_libraries(connection_weight_test boost_unit_test neuron)
add_executable(connection_test tests/Connection_test.cpp)
target_link_libraries(connection_test boost_unit_test neuron)
add_executable(neural_network_test tests/NeuralNetwork_test.cpp)
target_link_libraries(neural_network_test boost_unit_test neuron)
add_executable(connection_weight_identity_test tests/ConnectionWeightIdentity_test.cpp)
target_link_libraries(connection_weight_identity_test boost_unit_test neuron)
add_executable(particle_swarm_test tests/ParticleSwarm_test.cpp)
target_link_libraries(particle_swarm_test boost_unit_test neuron particle_swarm)
add_executable(particle_test tests/Particle_test.cpp)
target_link_libraries(particle_test boost_unit_test neuron particle_swarm)
add_executable(test_cases main.cpp)
target_link_libraries(test_cases neuron particle_swarm boost_serialization)
......@@ -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;
......@@ -300,7 +307,7 @@ double* ParticleSwarm::get_centroid_coordinates() {
return coords;
}
double ParticleSwarm::get_euclidean_distance(double* a, double* b, int n) {
double ParticleSwarm::get_euclidean_distance(double* a, double* b, unsigned int n) {
double dist = 0;
for(unsigned int i = 0; i < n; i++) {
if((a[i]-b[i]) * (a[i]-b[i]) > 1000) {
......
......@@ -124,7 +124,7 @@ protected:
* @param n
* @return
*/
double get_euclidean_distance(double* a, double* b, int n);
double get_euclidean_distance(double* a, double* b, unsigned int n);
public:
......
......@@ -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
}
......@@ -75,8 +75,14 @@ public:
*
* @param values
*/
void set_weights(double *values);
/**
*
* @return vector of weights
*/
std::vector<double> get_weights();
/**
*
* @param values
......
......@@ -89,7 +89,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];
}
......
......@@ -41,17 +41,17 @@ protected:
/**
* Number of parameters of the activation function
*/
int n_activation_function_parameters = 0;
unsigned int n_activation_function_parameters = 0;
/**
* Keeps track of the number of saturated INCOMING connections
*/
int n_saturated_connections_in = 0;
unsigned int n_saturated_connections_in = 0;
/**
* Keeps track of the number of saturated OUTGOING connections
*/
int n_saturated_connections_out = 0;
unsigned int n_saturated_connections_out = 0;
/**
* A pointer to a vector containing pointers to incoming connections
......
......@@ -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);
......
......@@ -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);
......
......@@ -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)
*/
......
/**
* 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()
/**
* 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
/**
* 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
/**
* 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
/**
* 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
......@@ -5,23 +5,35 @@
* @date 2018
*/
#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(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()
/**
* 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
/**
* 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
/**
* 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){
return 0;
}
BOOST_AUTO_TEST_SUITE(ParticleSwarm_test)
/**
* TODO