Skip to content
Snippets Groups Projects
seminar.cpp 4.05 KiB
/**
 * DESCRIPTION OF THE FILE
 *
 * @author Michal Kravčenko
 * @date 10.9.18 -
 */

#include <random>
#include <iostream>
#include <fstream>

#include "4neuro.h"
#include "../Solvers/DESolver.h"

int main() {

    std::cout << std::endl << "Running lib4neuro Moldyn Seminar example" << std::endl;
    std::cout << "********************************************************************************************************************************************" <<std::endl;


    l4n::NeuralNetwork XOR;
    unsigned  int i1 = XOR.add_neuron( new l4n::NeuronLinear( ), l4n::BIAS_TYPE::NO_BIAS );
    unsigned  int i2 = XOR.add_neuron( new l4n::NeuronLinear( ), l4n::BIAS_TYPE::NO_BIAS );

    unsigned  int h1 = XOR.add_neuron( new l4n::NeuronLogistic( ) );
    unsigned  int h2 = XOR.add_neuron( new l4n::NeuronLogistic( ) );

    unsigned  int o1 = XOR.add_neuron( new l4n::NeuronLinear( ), l4n::BIAS_TYPE::NO_BIAS );

    XOR.add_connection_simple( i1, h1 );
    XOR.add_connection_simple( i2, h1 );

    XOR.add_connection_simple( i1, h2 );
    XOR.add_connection_simple( i2, h2 );

    XOR.add_connection_simple( h1, o1 );
    XOR.add_connection_simple( h2, o1 );

    /* TRAIN DATA DEFINITION */
    std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
    std::vector<double> inp, out;

    inp = {0, 0};
    out = {0};
    data_vec.emplace_back(std::make_pair(inp, out));

    inp = {0, 1};
    out = {1};
    data_vec.emplace_back(std::make_pair(inp, out));

    inp = {1, 0};
    out = {1};
    data_vec.emplace_back(std::make_pair(inp, out));

    inp = {1, 1};
    out = {0};
    data_vec.emplace_back(std::make_pair(inp, out));

    l4n::DataSet ds(&data_vec);

    /* specification of the input/output neurons */
    std::vector<size_t> net_input_neurons_indices(2);
    std::vector<size_t> net_output_neurons_indices(1);
    net_input_neurons_indices[0] = i1;
    net_input_neurons_indices[1] = i2;

    net_output_neurons_indices[0] = o1;

    XOR.specify_input_neurons(net_input_neurons_indices);
    XOR.specify_output_neurons(net_output_neurons_indices);


    /* ERROR FUNCTION SPECIFICATION */
    l4n::MSE mse(&XOR, &ds);



    /* TRAINING METHOD SETUP */
    std::vector<double> domain_bounds(2 * (XOR.get_n_weights() + XOR.get_n_biases()));

    for(size_t i = 0; i < domain_bounds.size() / 2; ++i){
        domain_bounds[2 * i] = -10;
        domain_bounds[2 * i + 1] = 10;
    }

    double c1 = 1.7;
    double c2 = 1.7;
    double w = 0.7;
    size_t n_particles = 50;
    size_t iter_max = 1000;

    /* if the maximal velocity from the previous step is less than 'gamma' times the current maximal velocity, then one
     * terminating criterion is met */
    double gamma = 0.5;

    /* if 'delta' times 'n' particles are in the centroid neighborhood given by the radius 'epsilon', then the second
     * terminating criterion is met ('n' is the total number of particles) */
    double epsilon = 0.02;
    double delta = 0.7;

    l4n::ParticleSwarm swarm_01(
            &domain_bounds,
            c1,
            c2,
            w,
            gamma,
            epsilon,
            delta,
            n_particles,
            iter_max
    );
    swarm_01.optimize( mse );

    std::vector<double> *parameters = swarm_01.get_parameters( );
    XOR.copy_parameter_space(parameters);

    /* ERROR CALCULATION */
    double error = 0.0;
    inp = {0, 0};
    XOR.eval_single( inp, out );
    error += (0 - out[0]) * (0 - out[0]);
    std::cout << "x = (0,   0), expected output: 0, real output: " << out[0] << std::endl;

    inp = {0, 1};
    XOR.eval_single( inp, out );
    error += (1 - out[0]) * (1 - out[0]);
    std::cout << "x = (0,   1), expected output: 1, real output: " << out[0] << std::endl;

    inp = {1, 0};
    XOR.eval_single( inp, out );
    error += (1 - out[0]) * (1 - out[0]);
    std::cout << "x = (1,   0), expected output: 1, real output: " << out[0] << std::endl;

    inp = {1, 1};
    XOR.eval_single( inp, out );
    error += (0 - out[0]) * (0 - out[0]);
    std::cout << "x = (1,   1), expected output: 0, real output: " << out[0] << std::endl;


    return 0;
}