/** * Example of a neural network with reused edge weights * The system of equations associated with the net in this example is not regular * minimizes the function: ((2y+0.5)^2 + (2x+1)^2 + (2x + y + 0.25)^2 + (2x+1)^2 + 1 + (4.5x + 0.37)^2 ) /3 * minimum [0.705493164] at (x, y) = (-1133/6290, -11193/62900) = (-0.180127186, -0.177949126) */ // // Created by Michal on 7/17/18. // #include #include "../include/4neuro.h" int main() { /* TRAIN DATA DEFINITION */ std::vector, std::vector>> data_vec; std::vector inp, out; inp = {0, 1, 0}; out = {0.5, 0}; data_vec.emplace_back(std::make_pair(inp, out)); inp = {1, 0.5, 0}; out = {0.75, 0}; data_vec.emplace_back(std::make_pair(inp, out)); inp = {0, 0, 1.25}; out = {0, 0.63}; data_vec.emplace_back(std::make_pair(inp, out)); DataSet ds(&data_vec); /* NETWORK DEFINITION */ NeuralNetwork net; /* Input neurons */ NeuronLinear *i1 = new NeuronLinear(0.0, 1.0); //f(x) = x NeuronLinear *i2 = new NeuronLinear(0.0, 1.0); //f(x) = x NeuronLinear *i3 = new NeuronLinear(1, 1); //f(x) = x + 1 /* Output neurons */ NeuronLinear *o1 = new NeuronLinear(1.0, 2.0); //f(x) = 2x + 1 NeuronLinear *o2 = new NeuronLinear(1, 2); //f(x) = 2x + 1 /* Adding neurons to the nets */ int idx1 = net.add_neuron(i1); int idx2 = net.add_neuron(i2); int idx3 = net.add_neuron(o1); int idx4 = net.add_neuron(i3); int idx5 = net.add_neuron(o2); /* Adding connections */ //net.add_connection_simple(idx1, idx3, -1, 1.0); //net.add_connection_simple(idx2, idx3, -1, 1.0); net.add_connection_simple(idx1, idx3); // weight index 0 net.add_connection_simple(idx2, idx3); // weight index 1 net.add_connection_simple(idx4, idx5, 0); // AGAIN weight index 0 - same weight! net.randomize_weights(); /* specification of the input/output neurons */ std::vector net_input_neurons_indices(3); std::vector net_output_neurons_indices(2); net_input_neurons_indices[0] = idx1; net_input_neurons_indices[1] = idx2; net_input_neurons_indices[2] = idx4; net_output_neurons_indices[0] = idx3; net_output_neurons_indices[1] = idx5; net.specify_input_neurons(net_input_neurons_indices); net.specify_output_neurons(net_output_neurons_indices); /* COMPLEX ERROR FUNCTION SPECIFICATION */ MSE mse(&net, &ds); // double weights[2] = {-0.18012411, -0.17793740}; // double weights[2] = {1, 1}; // printf("evaluation of error at point (%f, %f) => %f\n", weights[0], weights[1], mse.eval(weights)); /* TRAINING METHOD SETUP */ unsigned int max_iters = 5000; //must encapsulate each of the partial error functions 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 = 100; ParticleSwarm swarm_01(&mse, domain_bounds, c1, c2, w, n_particles, max_iters); swarm_01.optimize(0.5, 0.02, 0.9); printf("evaluation of error: %f\n", mse.eval()); return 0; }