Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #define SEEDFILE "./seeds.txt"
- #define SURVIVOR 4
- #define CHILDREN 7
- #define DEFLIVES 10
- #define SIGMOID(x) tanh(x)
- #define DSGMOID(x) (1 - x*x)
- #define RAND rand() / RAND_MAX
- void getError(double r, double t, double ox, double oy, double *err) {
- err[0] = (r * cos(t)) - ox;
- err[1] = (r * sin(t)) - oy;
- }
- struct Network {
- int layers;
- int *neuNums;
- double *synDefs;
- double **neurons;
- double ***synapses;
- double rate;
- };
- void constructNetwork(struct Network *net) {
- int l, i, o, nl;
- net->neurons = malloc(sizeof(*net->neurons) * net->layers);
- net->synapses = malloc(sizeof(*net->synapses) * (net->layers - 1));
- for (l = 0, nl = 1; nl < net->layers; l++, nl++) {
- net->neurons[l] = malloc(sizeof(*net->neurons[l]) * net->neuNums[l]);
- net->synapses[l] = malloc(sizeof(*net->synapses[l]) * net->neuNums[l]);
- for (i = 0; i < net->neuNums[l]; i++) {
- net->synapses[l][i] = malloc(sizeof(*net->synapses[l][i]) * net->neuNums[nl]);
- for (o = 0; o < net->neuNums[nl]; o++) {
- net->synapses[l][i][o] = net->synDefs[l] * RAND;
- }
- }
- }
- net->neurons[l] = malloc(sizeof(*net->neurons[l]) * net->neuNums[l]);
- }
- void fireNeurons(struct Network *net) {
- int i, o, inpLay, outLay;
- for (inpLay = 0, outLay = 1; outLay < net->layers; inpLay++, outLay++) {
- for (o = 0; o < net->neuNums[outLay]; o++) {
- net->neurons[outLay][o] = 0;
- for (i = 0; i < net->neuNums[inpLay]; i++) {
- net->neurons[outLay][o] += net->synapses[inpLay][i][o] * net->neurons[inpLay][i];
- }
- net->neurons[outLay][o] = SIGMOID(net->neurons[outLay][o]);
- }
- }
- }
- void adjustSynapses(struct Network *net, double *err) {
- int inpLay, outLay, i, o;
- double tmpErr;
- double *nxtErr;
- for (outLay = net->layers - 1, inpLay = outLay - 1; inpLay >= 0; inpLay--, outLay--) {
- nxtErr = malloc(sizeof(*nxtErr) * net->neuNums[inpLay]);
- for (i = 0; i < net->neuNums[inpLay]; i++) {
- tmpErr = 0;
- for (o = 0; o < net->neuNums[outLay]; o++) {
- tmpErr += err[o] * net->synapses[inpLay][i][o];
- net->synapses[inpLay][i][o] += net->rate * err[o] * net->neurons[inpLay][i];
- }
- nxtErr[i] = DSGMOID(net->neurons[inpLay][i]) * tmpErr;
- }
- free(err);
- err = nxtErr;
- nxtErr = 0;
- }
- free(err);
- }
- void trainSilent(struct Network *net, unsigned int lim) {
- double *err;
- int outLay;
- outLay = net->layers - 1;
- net->neurons[0][0] = 1;
- while (lim--) {
- net->neurons[0][1] = 2 * M_PI * RAND;
- fireNeurons(net);
- err = malloc(sizeof(*err) * 2);
- getError(net->neurons[0][0], net->neurons[0][1], net->neurons[outLay][0], net->neurons[outLay][1], err);
- adjustSynapses(net, err);
- }
- }
- double trainReturnError(struct Network *net, unsigned int lim) {
- double c = 0;
- int outLay;
- unsigned int t;
- double *err;
- net->neurons[0][0] = 1;
- c = 0;
- outLay = net->layers - 1;
- t = lim;
- while (lim--) {
- net->neurons[0][1] = 2 * M_PI * RAND;
- fireNeurons(net);
- err = malloc(sizeof(*err) * 2);
- getError(net->neurons[0][0], net->neurons[0][1], net->neurons[outLay][0], net->neurons[outLay][1], err);
- c += hypot(err[0], err[1]);
- adjustSynapses(net, err);
- }
- return c / t;
- }
- int main() {
- double r;
- int i = 0;
- struct Network net;
- printf("Here we go!\n");
- srand(time(NULL));
- net.layers = 3;
- net.neuNums = malloc(sizeof(*net.neuNums) * 3);
- net.neuNums[0] = 2;
- net.neuNums[1] = 15;
- net.neuNums[2] = 2;
- net.synDefs = malloc(sizeof(*net.synDefs) * 2);
- net.synDefs[0] = 0.1;
- net.synDefs[1] = 0.1;
- net.rate = 0.1;
- constructNetwork(&net);
- printf("Network successfully constructed\n");
- for (i = 1; i <= 200; i++) {
- r = trainReturnError(&net, 5000);
- }
- printf("%d: %f\n", i, r);
- printf("Finished!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement