home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / AI-90-10.ZIP / NEURAL.HPP < prev    next >
C/C++ Source or Header  |  1990-10-01  |  3KB  |  101 lines

  1. //Neural.hpp
  2. //(c) 1990, Miller Freeman Publications
  3. //Released to the public domain
  4. //Disclaimer of warranty: If you use this and something bad happens, you can
  5. //have my sympathy but not my money
  6. //Larry O'Brien
  7. //V. 3.0
  8.   
  9. #ifndef NEURAL_HPP
  10. #define NEURAL_HPP
  11.   
  12. #define OUTLAYER (nLayers - 1)
  13.   
  14. //Portability defines.  These are the functions which should give you
  15. //some trouble if you're trying to port the code to another compiler
  16. //You could replace them other compiler-specific code or, for maximum
  17. //portability, ANSI escape sequences.  Or, leave DISPLAY_YES undefined
  18. //and they will be stripped out.  The network will run and tell you only
  19. //the total error (for all patterns).  Boring, but portable
  20. #define DISPLAY_YES
  21. #define CURSOR_GOTO gotoxy
  22. #define SET_TEXT_ATTR textcolor
  23. #define CLEAR_SCREEN clrscr
  24. #define DOS_PRINT cprintf
  25.   
  26. const int M_FACTOR = 5;    //This scales the input to the neuron
  27. const int MAX_LAYERS = 3;
  28. const int MAX_NEURONS = 40;
  29. const int ALLOC_ERR = 5;
  30.   
  31. enum bool{false,true};
  32.   
  33. struct Neuron{
  34.     Neuron(void);
  35.     void setZero(void);
  36.     inline void transfer(void);
  37.     inline float derivTransfer(float);
  38.     void sumOfErrors(float);
  39.     float input;
  40.     float output;
  41.     float error;
  42. };
  43.   
  44. struct Connection{
  45.     void set(Neuron*, Neuron*);
  46.     void setRandom(float, float, float);
  47.     void displaySelf(void);
  48.     inline void feedForward(void);
  49.     float weight;
  50.     float learningConstant;
  51.     float samadCoefficient;
  52.     float momentum;
  53.     float delta;
  54.     Neuron* n1;
  55.     Neuron* n2;
  56.     void adjust(void);
  57.     inline int firstNeuronIs(Neuron* n);
  58.     inline int secondNeuronIs(Neuron* n);
  59. };
  60.   
  61. struct Pattern{
  62.     int getMem(int, int);
  63.     float* in;
  64.     int* out;
  65. };
  66.   
  67. class Network{
  68.     Neuron*  neuron[MAX_LAYERS][MAX_NEURONS];
  69.     Connection*  cnxn;
  70.     int   nLayers;
  71.     int nCnxns, nNeurons;
  72.     int*  nInLayer;
  73.     int nPatterns;
  74.     int atPattern;
  75.     int inWidth, inDepth, outWidth, outDepth;
  76.     int iteration;
  77.     Pattern*  pat;
  78.     float acceptableError;
  79.     float thisPatternError;
  80.     float totalError;
  81.     void adjustWeights(void);
  82.     void calcOutputError(void);
  83.     void calcMiddleError(void);
  84. public:
  85.     Network(void);
  86.     Network(int, int*);
  87.     bool trained(void);
  88.     bool trainingFile(char*);
  89.     void displayDiff(void);
  90.     void display(void);
  91.     void displayTotalError(void);
  92.     void displayPerformance(unsigned long);
  93.     void forwardPass(void);
  94.     void backwardProp(void);
  95.     void allForward(void);
  96.     bool atEpochEnd(void);
  97.     void zeroTotalError(void);
  98. };
  99.   
  100. #endif
  101.