home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / CodeGen_Files / EXAMPLES / XOR2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  5.0 KB  |  166 lines

  1. // Xor1Temp.cpp
  2. // Automatically generated from breadboard by NeuroSolutions.
  3.  
  4. #include "NSLib.h"      
  5.  
  6. // This section of code was written after NeuroSolutions generated the code for the BB
  7. // (Begin)
  8. FILE    *m_pInFileStream;
  9. FILE    *m_pOutFileStream;
  10. // (End)
  11.  
  12. void saveAllWeights(char *weightsFilePath);
  13. void file35Access(NSFloat *, int, int);
  14. void dataWriterAccess(NSFloat *, int, int);
  15. BOOL networkStopped=FALSE;
  16. unsigned short int weightFileVersion=100;
  17.  
  18. // Component Construction
  19.     Axon axon1;
  20.     FunctionIO file35;
  21.     FullSynapse fullSynapse2;
  22.     TanhAxon tanhAxon6;
  23.     FullSynapse fullSynapse10;
  24.     TanhAxon tanhAxon14;
  25.     FunctionIO dataWriter;
  26.  
  27. int main() {
  28.     // This section of code was written after NeuroSolutions generated the code for the BB
  29.     // (Begin)
  30.     char    m_sInFilePath[512];
  31.     char    m_sOutFilePath[512];
  32.  
  33.     m_pInFileStream = NULL;
  34.     while (!m_pInFileStream) {
  35.         printf("Enter the name of the input file:\n");
  36.         scanf("%s",m_sInFilePath);
  37.         m_pInFileStream = fopen(m_sInFilePath,"r");
  38.         if (!m_pInFileStream)
  39.             printf("Error reading file: %s\n",m_sInFilePath);
  40.     }
  41.     m_pOutFileStream = NULL;
  42.     while (!m_pOutFileStream) {
  43.         printf("Enter the name of the output file:\n");
  44.         scanf("%s",m_sOutFilePath);
  45.         m_pOutFileStream = fopen(m_sOutFilePath,"w");
  46.         if (!m_pOutFileStream)
  47.             printf("Error writing to file: %s\n",m_sOutFilePath);
  48.     }
  49.     // (End)
  50.  
  51.     srand((unsigned)time(NULL));
  52.  
  53.     // Component Initialization
  54.     axon1.setRows(2);
  55.     file35.setFunction((void*)file35Access);
  56.     file35.setMode(READ);
  57.     file35.setSpatialDimension(2,1);
  58.     tanhAxon6.setRows(4);
  59.     tanhAxon14.setRows(1);
  60.     dataWriter.setFunction((void*)dataWriterAccess);
  61.     dataWriter.setMode(WRITE);
  62.     dataWriter.setSpatialDimension(1,1);
  63.     FILE *loadStream = fopen("Xor.nsw","r");
  64.     if (!loadStream) {
  65.         fprintf(stderr, "Could not open weight file Xor.nsw");
  66.         exit(1);
  67.     }
  68.     weightFileVersion = getWeightFileVersion(loadStream);
  69.  
  70.     // Load Normalization Coefficients of Files
  71.     file35.loadWeights(seekComponent(loadStream, "File", "file35"),weightFileVersion);
  72.  
  73.     // Component Interconnection
  74.     axon1.setPreActivityAccess(&file35);
  75.     tanhAxon14.setActivityAccess(&dataWriter);
  76.     axon1.setNext(&fullSynapse2);
  77.     fullSynapse2.setLast(&axon1);
  78.     fullSynapse2.setNext(&tanhAxon6);
  79.     tanhAxon6.setLast(&fullSynapse2);
  80.     tanhAxon6.setNext(&fullSynapse10);
  81.     fullSynapse10.setLast(&tanhAxon6);
  82.     fullSynapse10.setNext(&tanhAxon14);
  83.     tanhAxon14.setLast(&fullSynapse10);
  84.  
  85.     // Load Axon Weights
  86.     axon1.loadWeights(seekComponent(loadStream, "Axon", "axon1"),weightFileVersion);
  87.      tanhAxon6.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon6"),weightFileVersion);
  88.      tanhAxon14.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon14"),weightFileVersion);
  89.  
  90.     // Load Synapse Weights
  91.     fullSynapse2.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse2"),weightFileVersion);
  92.     fullSynapse10.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse10"),weightFileVersion);
  93.     fclose(loadStream);
  94.  
  95.     // Get Ready to Run Network
  96.  
  97.     // Run Network
  98.     for (unsigned int epoch=0; epoch<100; epoch++) { 
  99.         for (unsigned int exemplar=0; exemplar<4; exemplar++) { 
  100.             axon1.fire();
  101.             if (networkStopped)
  102.                 goto ConcludeFiring;
  103.         }
  104.     }
  105.     
  106.     ConcludeFiring:
  107.     saveAllWeights("Xor1Temp.nsw");
  108.     // This section of code was written after NeuroSolutions generated the code for the BB
  109.     // (Begin)
  110.     fclose(m_pInFileStream);
  111.     fclose(m_pOutFileStream);
  112.     // (End)
  113.  
  114.     return 0;
  115. }
  116.  
  117. void saveAllWeights(char *weightsFilePath)
  118. {    FILE *saveStream = fopen(weightsFilePath,"w");
  119.     if (!saveStream) {
  120.         fprintf(stderr, "Could not open weight file %s\n", weightsFilePath);
  121.         exit(1);
  122.     }
  123.     fprintf(saveStream,"#NSWeightFileVersion 135\n\n");
  124.     file35.saveWeights(putComponent(saveStream, "File", "file35"));
  125.      axon1.saveWeights(putComponent(saveStream, "Axon", "axon1"));
  126.      fullSynapse2.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse2"));
  127.      tanhAxon6.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon6"));
  128.      fullSynapse10.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse10"));
  129.      tanhAxon14.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon14"));
  130.      fclose(saveStream);
  131. }
  132.  
  133. void file35Access(NSFloat *data, int rows, int cols) {
  134.     // This function is called each time the axon1's Pre-Activity data changes
  135.     // This function's code was written after NeuroSolutions generated the code for the BB
  136.     // (Begin)
  137.     int     length,
  138.         i;
  139.  
  140.     length = rows*cols;
  141.     i=0;
  142.     while ((i<length) && (!feof(m_pInFileStream))) {
  143.         fscanf(m_pInFileStream,"%f", data+i);
  144.         i++;
  145.     }
  146.     if (feof(m_pInFileStream))
  147.         networkStopped=TRUE;
  148.     // (End)
  149. }
  150.  
  151. void dataWriterAccess(NSFloat *data, int rows, int cols) {
  152.     // This function is called each time the tanhAxon14's Activity data should be changed
  153.     // This function's code was written after NeuroSolutions generated the code for the BB
  154.     // (Begin)
  155.     int     j,
  156.         i;
  157.  
  158.     for (i=0; i<rows; i++) {
  159.         for (j=0; j<cols; j++)
  160.             fprintf(m_pOutFileStream, "%f ", data[i*cols + j]);
  161.         fprintf(m_pOutFileStream,"\n");
  162.     }
  163.     // (End)
  164. }
  165.  
  166.