Advertisement
mixster

Untitled

Jul 24th, 2011
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #define SEEDFILE "./seeds.txt"
  7. #define SURVIVOR 4
  8. #define CHILDREN 7
  9. #define DEFLIVES 10
  10.  
  11. #define SIGMOID(x) tanh(x)
  12. #define DSGMOID(x) (1 - x*x)
  13. #define RAND rand() / RAND_MAX
  14.  
  15. void getError(double r, double t, double ox, double oy, double *err) {
  16.   err[0] = (r * cos(t)) - ox;
  17.   err[1] = (r * sin(t)) - oy;
  18. }
  19.  
  20.  
  21. struct Network {
  22.   int layers;
  23.   int *neuNums;
  24.   double *synDefs;
  25.   double **neurons;
  26.   double ***synapses;
  27.   double rate;
  28. };
  29.  
  30. void constructNetwork(struct Network *net) {
  31.   int l, i, o, nl;
  32.  
  33.   net->neurons = malloc(sizeof(*net->neurons) * net->layers);
  34.   net->synapses = malloc(sizeof(*net->synapses) * (net->layers - 1));
  35.  
  36.   for (l = 0, nl = 1; nl < net->layers; l++, nl++) {
  37.     net->neurons[l] = malloc(sizeof(*net->neurons[l]) * net->neuNums[l]);
  38.     net->synapses[l] = malloc(sizeof(*net->synapses[l]) * net->neuNums[l]);
  39.    
  40.     for (i = 0; i < net->neuNums[l]; i++) {
  41.       net->synapses[l][i] = malloc(sizeof(*net->synapses[l][i]) * net->neuNums[nl]);
  42.      
  43.       for (o = 0; o < net->neuNums[nl]; o++) {
  44.     net->synapses[l][i][o] = net->synDefs[l] * RAND;
  45.       }
  46.     }
  47.   }
  48.   net->neurons[l] = malloc(sizeof(*net->neurons[l]) * net->neuNums[l]);
  49. }
  50.  
  51. void fireNeurons(struct Network *net) {
  52.   int i, o, inpLay, outLay;
  53.   for (inpLay = 0, outLay = 1; outLay < net->layers; inpLay++, outLay++) {
  54.     for (o = 0; o < net->neuNums[outLay]; o++) {
  55.       net->neurons[outLay][o] = 0;
  56.       for (i = 0; i < net->neuNums[inpLay]; i++) {
  57.     net->neurons[outLay][o] += net->synapses[inpLay][i][o] * net->neurons[inpLay][i];
  58.       }
  59.       net->neurons[outLay][o] = SIGMOID(net->neurons[outLay][o]);
  60.     }
  61.   }
  62. }
  63.  
  64. void adjustSynapses(struct Network *net, double *err) {
  65.   int inpLay, outLay, i, o;
  66.   double tmpErr;
  67.   double *nxtErr;
  68.  
  69.   for (outLay = net->layers - 1, inpLay = outLay - 1; inpLay >= 0; inpLay--, outLay--) {
  70.     nxtErr = malloc(sizeof(*nxtErr) * net->neuNums[inpLay]);
  71.     for (i = 0; i < net->neuNums[inpLay]; i++) {
  72.       tmpErr = 0;
  73.       for (o = 0; o < net->neuNums[outLay]; o++) {
  74.     tmpErr += err[o] * net->synapses[inpLay][i][o];
  75.     net->synapses[inpLay][i][o] += net->rate * err[o] * net->neurons[inpLay][i];
  76.       }
  77.       nxtErr[i] = DSGMOID(net->neurons[inpLay][i]) * tmpErr;
  78.     }
  79.    
  80.     free(err);
  81.     err = nxtErr;
  82.     nxtErr = 0;
  83.   }
  84.   free(err);
  85. }
  86.  
  87. void trainSilent(struct Network *net, unsigned int lim) {
  88.   double *err;
  89.   int outLay;
  90.  
  91.   outLay = net->layers - 1;  
  92.   net->neurons[0][0] = 1;
  93.   while (lim--) {
  94.     net->neurons[0][1] = 2 * M_PI * RAND;
  95.     fireNeurons(net);
  96.     err = malloc(sizeof(*err) * 2);
  97.     getError(net->neurons[0][0], net->neurons[0][1], net->neurons[outLay][0], net->neurons[outLay][1], err);
  98.     adjustSynapses(net, err);
  99.   }
  100. }
  101.  
  102. double trainReturnError(struct Network *net, unsigned int lim) {
  103.   double c = 0;
  104.   int outLay;
  105.   unsigned int t;
  106.   double *err;
  107.  
  108.   net->neurons[0][0] = 1;
  109.   c = 0;
  110.   outLay = net->layers - 1;
  111.   t = lim;
  112.  
  113.   while (lim--) {
  114.     net->neurons[0][1] = 2 * M_PI * RAND;
  115.     fireNeurons(net);
  116.     err = malloc(sizeof(*err) * 2);
  117.     getError(net->neurons[0][0], net->neurons[0][1], net->neurons[outLay][0], net->neurons[outLay][1], err);
  118.     c += hypot(err[0], err[1]);
  119.     adjustSynapses(net, err);
  120.   }
  121.  
  122.   return c / t;
  123. }
  124.  
  125. int main() {
  126.   double r;
  127.   int i = 0;
  128.   struct Network net;
  129.   printf("Here we go!\n");
  130.  
  131.   srand(time(NULL));
  132.  
  133.   net.layers = 3;
  134.   net.neuNums = malloc(sizeof(*net.neuNums) * 3);
  135.   net.neuNums[0] = 2;
  136.   net.neuNums[1] = 15;
  137.   net.neuNums[2] = 2;
  138.   net.synDefs = malloc(sizeof(*net.synDefs) * 2);
  139.   net.synDefs[0] = 0.1;
  140.   net.synDefs[1] = 0.1;
  141.   net.rate = 0.1;
  142.   constructNetwork(&net);
  143.  
  144.   printf("Network successfully constructed\n");
  145.  
  146.   for (i = 1; i <= 200; i++) {
  147.     r = trainReturnError(&net, 5000);
  148.   }
  149.  
  150.   printf("%d: %f\n", i, r);
  151.   printf("Finished!\n");
  152.   return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement