00001 #ifndef _NEURON_H 00002 #define _NEURON_H 00003 00004 #include "Link.h" 00005 #include "defines.h" 00006 #include <string> 00007 00008 namespace annie 00009 { 00010 00011 // Some activation functions and their derivatives 00012 00014 real identity(real x); 00016 real didentity(real x); 00018 real sigmoid(real x); 00020 real dsigmoid(real x); 00022 real gaussian(real x); 00024 real dgaussian(real x); 00032 real signum(real x); 00034 real tansig(real x); 00036 real dtansig(real x); 00037 00038 00040 00046 typedef real(*ActivationFunction)(real); 00047 00049 typedef std::vector<Link *> LINKS; 00050 00077 class Neuron 00078 { 00079 protected: 00081 int _label; 00082 00088 mutable bool _outputCacheValid; 00089 00091 mutable real _activationCache; 00093 mutable real _outputCache; 00094 00096 mutable bool _errorCacheValid; 00097 00107 mutable real _errorCache; 00108 00118 std::vector<char *> _classHeirarchy; 00119 00121 LINKS _inputLinks; 00122 public: 00127 LINKS _outputLinks; 00128 protected: 00130 ActivationFunction _activationFunction; 00131 00149 virtual void _recacheOutput() const =0; 00150 00166 virtual void _recacheError() const =0; 00167 00168 public: 00172 Neuron(int label); 00173 00174 Neuron(Neuron &neuron); 00175 00177 virtual ~Neuron(); 00178 00185 virtual real getActivation() const; 00186 00192 virtual real getOutput() const; 00193 00199 virtual real getError() const; 00200 00202 virtual int getLabel() const; 00203 00207 virtual uint getInputCount() const; 00208 00212 virtual uint getOutputCount() const; 00213 00220 virtual void invalidateOutputCache(); 00221 00226 virtual void invalidateErrorCache(); 00227 00234 virtual int getInputs(std::vector<int> &labels, Vector &weights); 00235 00240 void getWeights(Vector &out) const; 00241 00246 virtual void disconnect(Neuron *from); 00247 00253 virtual real getWeight(Neuron *from) const; 00254 00259 virtual operator std::string() const; 00260 00264 virtual const char *getClassName() const=0; 00265 friend class Link; 00266 00281 bool instanceOf(const char *className) const; 00282 00287 friend std::ostream& operator << (std::ostream& s, const Neuron &neuron); 00288 }; 00289 00290 }; //namespace annie 00291 #endif // define _NEURON_H 00292