home *** CD-ROM | disk | FTP | other *** search
- // Xor1Temp.cpp
- // Automatically generated from breadboard by NeuroSolutions.
-
- #include "NSLib.h"
-
- // This section of code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- FILE *m_pInFileStream;
- FILE *m_pOutFileStream;
- // (End)
-
- void saveAllWeights(char *weightsFilePath);
- void file35Access(NSFloat *, int, int);
- void dataWriterAccess(NSFloat *, int, int);
- BOOL networkStopped=FALSE;
- unsigned short int weightFileVersion=100;
-
- // Component Construction
- Axon axon1;
- FunctionIO file35;
- FullSynapse fullSynapse2;
- TanhAxon tanhAxon6;
- FullSynapse fullSynapse10;
- TanhAxon tanhAxon14;
- FunctionIO dataWriter;
-
- int main() {
- // This section of code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- char m_sInFilePath[512];
- char m_sOutFilePath[512];
-
- m_pInFileStream = NULL;
- while (!m_pInFileStream) {
- printf("Enter the name of the input file:\n");
- scanf("%s",m_sInFilePath);
- m_pInFileStream = fopen(m_sInFilePath,"r");
- if (!m_pInFileStream)
- printf("Error reading file: %s\n",m_sInFilePath);
- }
- m_pOutFileStream = NULL;
- while (!m_pOutFileStream) {
- printf("Enter the name of the output file:\n");
- scanf("%s",m_sOutFilePath);
- m_pOutFileStream = fopen(m_sOutFilePath,"w");
- if (!m_pOutFileStream)
- printf("Error writing to file: %s\n",m_sOutFilePath);
- }
- // (End)
-
- srand((unsigned)time(NULL));
-
- // Component Initialization
- axon1.setRows(2);
- file35.setFunction((void*)file35Access);
- file35.setMode(READ);
- file35.setSpatialDimension(2,1);
- tanhAxon6.setRows(4);
- tanhAxon14.setRows(1);
- dataWriter.setFunction((void*)dataWriterAccess);
- dataWriter.setMode(WRITE);
- dataWriter.setSpatialDimension(1,1);
- FILE *loadStream = fopen("Xor.nsw","r");
- if (!loadStream) {
- fprintf(stderr, "Could not open weight file Xor.nsw");
- exit(1);
- }
- weightFileVersion = getWeightFileVersion(loadStream);
-
- // Load Normalization Coefficients of Files
- file35.loadWeights(seekComponent(loadStream, "File", "file35"),weightFileVersion);
-
- // Component Interconnection
- axon1.setPreActivityAccess(&file35);
- tanhAxon14.setActivityAccess(&dataWriter);
- axon1.setNext(&fullSynapse2);
- fullSynapse2.setLast(&axon1);
- fullSynapse2.setNext(&tanhAxon6);
- tanhAxon6.setLast(&fullSynapse2);
- tanhAxon6.setNext(&fullSynapse10);
- fullSynapse10.setLast(&tanhAxon6);
- fullSynapse10.setNext(&tanhAxon14);
- tanhAxon14.setLast(&fullSynapse10);
-
- // Load Axon Weights
- axon1.loadWeights(seekComponent(loadStream, "Axon", "axon1"),weightFileVersion);
- tanhAxon6.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon6"),weightFileVersion);
- tanhAxon14.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon14"),weightFileVersion);
-
- // Load Synapse Weights
- fullSynapse2.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse2"),weightFileVersion);
- fullSynapse10.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse10"),weightFileVersion);
- fclose(loadStream);
-
- // Get Ready to Run Network
-
- // Run Network
- for (unsigned int epoch=0; epoch<100; epoch++) {
- for (unsigned int exemplar=0; exemplar<4; exemplar++) {
- axon1.fire();
- if (networkStopped)
- goto ConcludeFiring;
- }
- }
-
- ConcludeFiring:
- saveAllWeights("Xor1Temp.nsw");
- // This section of code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- fclose(m_pInFileStream);
- fclose(m_pOutFileStream);
- // (End)
-
- return 0;
- }
-
- void saveAllWeights(char *weightsFilePath)
- { FILE *saveStream = fopen(weightsFilePath,"w");
- if (!saveStream) {
- fprintf(stderr, "Could not open weight file %s\n", weightsFilePath);
- exit(1);
- }
- fprintf(saveStream,"#NSWeightFileVersion 135\n\n");
- file35.saveWeights(putComponent(saveStream, "File", "file35"));
- axon1.saveWeights(putComponent(saveStream, "Axon", "axon1"));
- fullSynapse2.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse2"));
- tanhAxon6.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon6"));
- fullSynapse10.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse10"));
- tanhAxon14.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon14"));
- fclose(saveStream);
- }
-
- void file35Access(NSFloat *data, int rows, int cols) {
- // This function is called each time the axon1's Pre-Activity data changes
- // This function's code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- int length,
- i;
-
- length = rows*cols;
- i=0;
- while ((i<length) && (!feof(m_pInFileStream))) {
- fscanf(m_pInFileStream,"%f", data+i);
- i++;
- }
- if (feof(m_pInFileStream))
- networkStopped=TRUE;
- // (End)
- }
-
- void dataWriterAccess(NSFloat *data, int rows, int cols) {
- // This function is called each time the tanhAxon14's Activity data should be changed
- // This function's code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- int j,
- i;
-
- for (i=0; i<rows; i++) {
- for (j=0; j<cols; j++)
- fprintf(m_pOutFileStream, "%f ", data[i*cols + j]);
- fprintf(m_pOutFileStream,"\n");
- }
- // (End)
- }
-
-