main.cpp 6.32 KB
Newer Older
1 2 3 4 5 6 7
/**
 * DESCRIPTION OF THE FILE
 *
 * @author Michal Kravčenko
 * @date 14.6.18 -
 */

8
#include <iostream>
9
#include <vector>
10
#include <boost/archive/text_oarchive.hpp>
11 12 13
#include "Network/NeuralNetwork.h"
#include "Neuron/NeuronLinear.h"

14 15
#include "LearningMethods/ParticleSwarm.h"

16 17
//TODO rewrite "tests" to separate examples

18
//TODO prepsat tak, aby neuronova sit managovala destruktory vsech potrebnych objektu (kvuli serializaci)
19 20 21 22
/**
 * Test of simple neural network
 * Network should evaluate the function f(x) = x + 1
 */
23
void test1( ){
24 25 26
    std::vector<double> in(1);
    std::vector<double> out(1);

27 28 29 30 31 32 33 34 35 36
    NeuralNetwork net;
    NeuronLinear* u1 = new NeuronLinear(1.0, 1.0); //f(x) = x + 1.0
    NeuronLinear* u2 = new NeuronLinear(0.0, 1.0); //f(x) = x
    int idx1 = net.add_neuron(u1);
    int idx2 = net.add_neuron(u2);
////////////////////// SIMPLE EDGE WEIGHT ////////////////////////////////////////
//    net.add_connection_simple(idx1, idx2, -1, 1.0);
////////////////////// END SIMPLE EDGE WEIGHT ////////////////////////////////////////

/////////////////////////BEGIN OF COMPLEX EDGE WEIGHT//////////////////////////////
Michal Kravcenko's avatar
Michal Kravcenko committed
37
   std::function<double(double *, size_t*, size_t)> weight_function = [](double * weight_array, size_t * index_array, size_t n_params){
38
        //w(x, y) = x + y
39 40 41
        double a = weight_array[index_array[0]];
        double b = weight_array[index_array[1]];
//        printf("eval: %f, %f\n",  a, b);
42 43
        return (a + 0.0 * b);
    };
Michal Kravcenko's avatar
Michal Kravcenko committed
44
    size_t  weight_indices [2] = {0, 1};
45 46 47
    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//////////////////////////////
48 49 50 51 52 53 54
    for(int i = 0; i < 20; ++i){
        in[0] = 0.05 * i;
        net.eval_single(in, out);

        printf("x = %3.2f, f(x) = %3.2f, expected output = %3.2f\n", in[0], out[0], in[0] + 1.0);
    }

55 56 57
    //clean-up phase
    delete u1;
    delete u2;
58 59
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/**
 * Test of DataSet serialization
 */
void test3() {
    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 ds(&data_vec);

    ds.print_data();

    ds.store_text("stored_data.4ds");

    DataSet ds2("stored_data.4ds");

    ds2.print_data();
}

85 86 87 88 89 90 91 92
double particle_swarm_test_function(double *x){
//    return x[0] * x[1] - x[0] * x[0] + x[1] * x[2];
    return x[0] * x[0];
}

NeuralNetwork net;
std::vector<std::vector<double>*> *train_data_input;
std::vector<std::vector<double>*> *train_data_output;
93

94 95 96 97 98
double test_particle_swarm_neural_net_error_function(double *weights){

    net.copy_weights(weights);

    unsigned int dim_out = train_data_output->at(0)->size();
99
//    unsigned int dim_in = train_data_input->at(0)->size();
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    double error = 0.0, val;

    std::vector<double> output( dim_out );
    for(unsigned int i = 0; i < train_data_input->size(); ++i){

        net.eval_single(*train_data_input->at(i), output);

        for(unsigned int j = 0; j < dim_out; ++j){
            val = output[j] - train_data_output->at(i)->at(j);
            error += val * val;
        }

    }

115
    /*
116 117 118 119 120
    printf("INPUT: ");
    for(unsigned int i = 0; i < dim_in; ++i){
        printf("%f ", weights[i]);
    }
    printf(", ERROR: %f\n", 0.5 * error);
121
    */
122 123 124 125 126 127
    return 0.5 * error;
}

//TODO proper memory management
void test_particle_swarm_neural_net(){

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
//    unsigned int dim_in = 2;
//    unsigned int dim_out = 1;
//
//    /* TRAIN DATA DEFINITION */
//    train_data_input = new std::vector<std::vector<double>*>();
//    train_data_output = new std::vector<std::vector<double>*>();
//
//    std::vector<double> *input_01 = new std::vector<double>(dim_in);
//    std::vector<double> *input_02 = new std::vector<double>(dim_in);
//
//    std::vector<double> *output_01 = new std::vector<double>(dim_out);
//    std::vector<double> *output_02 = new std::vector<double>(dim_out);
//
//    (*input_01)[0] = 0.0;
//    (*input_01)[1] = 1.0;
//    (*output_01)[0] = 0.5;
//
//    (*input_02)[0] = 1.0;
//    (*input_02)[1] = 0.5;
//    (*output_02)[0] = 0.75;
//
//    train_data_input->push_back(input_01);
//    train_data_output->push_back(output_01);
//
//    train_data_input->push_back(input_02);
//    train_data_output->push_back(output_02);
//    /* NETWORK DEFINITION */
//
//
//    NeuronLinear* i1 = new NeuronLinear(0.0, 1.0); //f(x) = x
//    NeuronLinear* i2 = new NeuronLinear(0.0, 1.0); //f(x) = x
//
////    NeuronLogistic* o1 = new NeuronLogistic(1.0, 0.0); //f(x) = (1 + e^(-x + 0.0))^(1.0)
//    NeuronLinear* o1 = new NeuronLinear(1.0, 2.0); //f(x) = 2x + 1
//
//    int idx1 = net.add_neuron(i1);
//    int idx2 = net.add_neuron(i2);
//    int idx3 = net.add_neuron(o1);
//
//    net.add_connection_simple(idx1, idx3, -1, 1.0);
//    net.add_connection_simple(idx2, idx3, -1, 1.0);
//
//    /* PARTICLE SETUP */
//    double (*F)(double*) = &test_particle_swarm_neural_net_error_function;
//
//    unsigned int n_edges = 2;
//    unsigned int dim = n_edges, max_iters = 2000;
//
//
//    double domain_bounds [4] = {-800.0, 800.0, -800.0, 800.0};
//
//    double c1 = 0.5, c2 = 1.5, w = 0.8;
//
//    unsigned int n_particles = 10;
//
//    ParticleSwarm swarm_01(F, dim, domain_bounds, c1, c2, w, n_particles, max_iters);
//
//    swarm_01.optimize(0.5, 0.02);
//
//    /* CLEANUP PHASE */
//    for( std::vector<double> *input: *train_data_input){
//        delete input;
//    }
//    for( std::vector<double> *output: *train_data_output){
//        delete output;
//    }
//
//    delete train_data_output;
//    delete train_data_input;
197

198
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
//
//void test_particle_swarm(){
//    double (*F)(double*) = &particle_swarm_test_function;
//
//    unsigned int dim = 3, max_iters = 100;
//
////    double domain_bounds [2] = {2.0, 3.0};
//    double domain_bounds [6] = {-3.0, 3.0, 2.0, 5.0, 1.0, 15.0};
//
//    double c1 = 0.5, c2 = 1.5, w = 1.0;
//
//    unsigned int n_particles = 1000;
//
//    double accuracy = 1e-6;
//
//    ParticleSwarm swarm_01(F, dim, domain_bounds, c1, c2, w, n_particles, max_iters);
//
//    swarm_01.optimize(0.5, accuracy);
//}
218

219 220
int main(int argc, char** argv){

221 222 223
//    test1();

//    test_particle_swarm();
224

225
    test_particle_swarm_neural_net();
226 227

//    test2();
228

229
//    test3();
230

231
    return 0;
Martin Beseda's avatar
Martin Beseda committed
232
}