Commit 69085220 authored by David Vojtek's avatar David Vojtek

some small changes

parent a2f929a9
......@@ -78,4 +78,4 @@ endif ()
IF (Boost_FOUND)
message("Boost DIR: " ${Boost_INCLUDE_DIR})
include_directories(${Boost_INCLUDE_DIR})
endif()
\ No newline at end of file
endif()
......@@ -64,7 +64,7 @@ add_executable(connection_weight_identity_test tests/ConnectionWeightIdentity_te
target_link_libraries(connection_weight_identity_test boost_unit_test 4neuro)
add_executable(dataset_test tests/DataSet_test.cpp)
target_link_libraries(dataset_test boost_unit_test data_set)
target_link_libraries(dataset_test boost_unit_test 4neuro)
add_executable(particle_swarm_test tests/ParticleSwarm_test.cpp)
target_link_libraries(particle_swarm_test boost_unit_test 4neuro)
......
......@@ -78,7 +78,7 @@ void NeuralNetwork::add_connection_simple(int n1_idx, int n2_idx, int weight_idx
void NeuralNetwork::add_connection_general(int n1_idx, int n2_idx, std::function<double(double *, int*, int)> *f,
int* weight_indices, double* weight_values, int n_weights) {
ConnectionWeight *con_weight_u1u2 = new ConnectionWeight(n_weights, this->connection_weights, f);
ConnectionWeight *con_weight_u1u2 = new ConnectionWeight(n_weights, this->connection_weights);
//we analyze weights
int weight_idx = 0;
double weight_value = 0.0;
......
......@@ -190,9 +190,9 @@ void test_particle_swarm_neural_net(){
unsigned int n_particles = 10;
ParticleSwarm swarm_01(F, dim, domain_bounds, c1, c2, w, n_particles, max_iters);
// ParticleSwarm swarm_01(F, dim, domain_bounds, c1, c2, w, n_particles, max_iters);
swarm_01.optimize(0.5, 0.02);
// swarm_01.optimize(0.5, 0.02);
/* CLEANUP PHASE */
for( std::vector<double> *input: *train_data_input){
......@@ -221,9 +221,9 @@ void test_particle_swarm(){
double accuracy = 1e-6;
ParticleSwarm swarm_01(F, dim, domain_bounds, c1, c2, w, n_particles, max_iters);
//ParticleSwarm swarm_01(F, dim, domain_bounds, c1, c2, w, n_particles, max_iters);
swarm_01.optimize(0.5, accuracy);
//swarm_01.optimize(0.5, accuracy);
}
int main(int argc, char** argv){
......
......@@ -20,10 +20,9 @@ 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;
//Tests of no exception when, create new instance
BOOST_CHECK_NO_THROW( ConnectionWeight conn(2, w_array, f ));
BOOST_CHECK_NO_THROW( ConnectionWeight conn(2, w_array ));
BOOST_CHECK_NO_THROW( ConnectionWeight conn);
}
......@@ -32,13 +31,9 @@ BOOST_AUTO_TEST_SUITE(ConnectionWeight_test)
*/
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 );
ConnectionWeight connbad(7, &w_array, &f );
ConnectionWeight conn(5, &w_array );
ConnectionWeight connbad(7, &w_array);
int para[5]={0,1,2,3,4};
//Test of no exception when call SetParamIndices method on instance created with with correct parameters
......@@ -56,12 +51,8 @@ BOOST_AUTO_TEST_SUITE(ConnectionWeight_test)
*/
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 );
ConnectionWeight conn(5, &w_array );
int para[5]={4,8,2,3,4};
conn.SetParamIndices(para);
......@@ -75,12 +66,8 @@ BOOST_AUTO_TEST_SUITE(ConnectionWeight_test)
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 );
ConnectionWeight conn(5, &w_array);
int para[5]={0,1,2,3,4};
conn.SetParamIndices(para);
conn.adjust_weights(w_array2);
......@@ -95,12 +82,7 @@ BOOST_AUTO_TEST_SUITE(ConnectionWeight_test)
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 );
ConnectionWeight conn(5, &w_array );
int para[5]={0,1,2,3,4};
conn.SetParamIndices(para);
conn.set_weights(w_array2);
......
......@@ -28,14 +28,8 @@ BOOST_AUTO_TEST_SUITE(Connection_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 );
ConnectionWeight* conn = new ConnectionWeight(2, &w_array );
int para[2]={0,1};
conn->SetParamIndices(para);
Connection connection(neuron1, neuron2, conn);
......@@ -61,7 +55,7 @@ BOOST_AUTO_TEST_SUITE(Connection_test)
};
ConnectionWeight* conn = new ConnectionWeight(2, &w_array, &f );
ConnectionWeight* conn = new ConnectionWeight(2, &w_array );
int para[2]={0,1};
conn->SetParamIndices(para);
Connection connection(neuron1, neuron2, conn);
......
......@@ -25,14 +25,38 @@ BOOST_AUTO_TEST_SUITE(ParticleSwarm_test)
BOOST_AUTO_TEST_CASE(ParticleSwarm_construction_test){
double domain_bound = 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));
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
DataSet dataSet(&data_vec);
ErrorFunction *error = new MSE(&network, &dataSet);
BOOST_CHECK_NO_THROW(ParticleSwarm swarm(error, &domain_bound, 0, 1, 1, 0, 20));
}
BOOST_AUTO_TEST_CASE(ParticleSwarm_optimalize_test){
double domain_bound = 5;
double (*F)(double*) = &test_particle_swarm_neural_net_error_function;
ParticleSwarm swarm(F, 2, &domain_bound, 0, 1, 1, 0, 20);
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
DataSet dataSet(&data_vec);
ErrorFunction *error = new MSE(&network, &dataSet);
ParticleSwarm swarm(error,&domain_bound, 0, 1, 1, 0, 20);
BOOST_CHECK_THROW(swarm.optimize(-1,1,1), std::invalid_argument) ;
BOOST_CHECK_THROW(swarm.optimize(1,-1,1), std::invalid_argument) ;
BOOST_CHECK_THROW(swarm.optimize(1,1,-1), std::invalid_argument) ;
......
......@@ -8,48 +8,93 @@
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>
#include "../LearningMethods/ParticleSwarm.h"
/**
* Boost testing suite for testing ParticleSwarm.h
* TODO
*/
double test_particle_function(double *weights){
return 0;
}
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;
BOOST_CHECK_NO_THROW(Particle particle(1, &domain_bound[0], F));
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
DataSet dataSet(&data_vec);
ErrorFunction *error = new MSE(&network, &dataSet);
BOOST_CHECK_NO_THROW(Particle particle(error, &domain_bound[0]));
// particle.get_coordinate();
}
BOOST_AUTO_TEST_CASE(Particle_get_coordinate_test){
double domain_bound[5] = {1,2,3,4,5};
double (*F)(double*) = &test_particle_function;
Particle particle1(1, &domain_bound[0], F);
Particle particle2(1, &domain_bound[0], F);
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
DataSet dataSet(&data_vec);
ErrorFunction *error = new MSE(&network, &dataSet);
Particle particle1(error, &domain_bound[0]);
Particle particle2(error, &domain_bound[0]);
BOOST_CHECK(*particle1.get_coordinate() != *particle2.get_coordinate());
}
BOOST_AUTO_TEST_CASE(particle_change_coordiante_test) {
double domain_bound[5] = {1, 2, 3, 4, 5};
double (*F)(double *) = &test_particle_function;
Particle particle(1, &domain_bound[0], F);
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
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};
double (*F)(double *) = &test_particle_function;
Particle particle(1, &domain_bound[0], F);
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
DataSet dataSet(&data_vec);
ErrorFunction *error = new MSE(&network, &dataSet);
Particle particle(error, &domain_bound[0]);
BOOST_CHECK_EQUAL(particle.get_optimal_value(),0);
}
......
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