00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Math Library 00005 * Module : Neuron 00006 * Package : Zeus.ZeusMath.Intel 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef NeuronHPP 00033 #define NeuronHPP 00034 00035 #include <zeusmath/Config/PlatformDefines.hpp> 00036 #include <zeusbase/System/Int.h> 00037 #include <zeusbase/System/Time.h> 00038 #include <zeusbase/System/ZObject.h> 00039 #include <zeusbase/System/ManagedList.hpp> 00040 00041 BEGIN_NAMESPACE_Zeus 00042 00043 class TNeuron; 00044 00045 /*****************************************************************************/ 00048 /*****************************************************************************/ 00049 zeusmath_class TSynapse : public TZObject 00050 { 00051 public: 00052 /*************************************************/ 00055 /*************************************************/ 00056 enum EGrowMethod 00057 { 00058 etAllwaysGrow = 0, //allways grow 00059 etKeepSign = 1 //grow only if the sign does not change 00060 }; 00061 00062 /*************************************************************************/ 00068 /*************************************************************************/ 00069 TSynapse(TNeuron& rPostNeuron, Float fWeight = 0, EGrowMethod eGrowMethod = etAllwaysGrow); 00070 00071 /*************************************************************************/ 00074 /*************************************************************************/ 00075 bool equals(const TSynapse& rSynapse) const 00076 { 00077 return (&(this->m_rPostNeuron) == &(rSynapse.m_rPostNeuron) && 00078 this->m_fWeight == rSynapse.m_fWeight && 00079 this->m_eGrowMethod == rSynapse.m_eGrowMethod); 00080 } 00081 00082 /*************************************************************************/ 00085 /*************************************************************************/ 00086 TNeuron& getPostNeuron() { return m_rPostNeuron; } 00087 00088 /*************************************************************************/ 00091 /*************************************************************************/ 00092 const TNeuron& getPostNeuronConst() const { return m_rPostNeuron; } 00093 00094 /*************************************************************************/ 00097 /*************************************************************************/ 00098 Float getWeight() const { return m_fWeight; } 00099 00100 /*************************************************************************/ 00103 /*************************************************************************/ 00104 EGrowMethod getGrowMethod() const { return m_eGrowMethod; } 00105 00106 /*************************************************************************/ 00109 /*************************************************************************/ 00110 void grow(Float fGrowValue) 00111 { 00112 switch(m_eGrowMethod) 00113 { 00114 case etKeepSign : 00115 if ((fGrowValue / m_fWeight) > -1.0) 00116 { 00117 m_fWeight += fGrowValue; 00118 } 00119 break; 00120 00121 case etAllwaysGrow: //fall through 00122 default: 00123 m_fWeight += fGrowValue; 00124 break; 00125 } 00126 } 00127 00128 /*************************************************************************/ 00131 /*************************************************************************/ 00132 void setWeight(Float fWeight) { m_fWeight = fWeight; } 00133 00134 /*************************************************************************/ 00137 /*************************************************************************/ 00138 void setGrowMethod(EGrowMethod eMethod) { m_eGrowMethod = eMethod; } 00139 00140 /*************************************************************************/ 00143 /*************************************************************************/ 00144 bool operator==(const TSynapse& rSynapse) const 00145 { 00146 return equals(rSynapse); 00147 } 00148 00149 /*************************************************************************/ 00152 /*************************************************************************/ 00153 bool operator!=(const TSynapse& rSynapse) const 00154 { 00155 return !equals(rSynapse); 00156 } 00157 00158 protected: 00159 /*************************************************************************/ 00162 /*************************************************************************/ 00163 virtual ~TSynapse() 00164 { 00165 //m_rPostNeuron.decreaseConnected(); 00166 } 00167 00168 private: 00170 TNeuron& m_rPostNeuron; 00172 Float m_fWeight; 00174 EGrowMethod m_eGrowMethod; 00175 00176 //Hide this methods 00177 TSynapse(const TSynapse& rSynapse); 00178 TSynapse& operator=(const TSynapse& rSynapse); 00179 }; 00180 00181 typedef TManagedList<TSynapse> TAxon; 00182 00183 /**************************************************************************/ 00186 /**************************************************************************/ 00187 zeusmath_class TNeuron : public TZObject 00188 { 00189 public: 00190 /***********************************************************************/ 00193 /***********************************************************************/ 00194 TNeuron(Uint uiID, Float fThreshold, Float fPotential) 00195 : m_uiID(uiID), 00196 m_fThreshold(fThreshold), 00197 m_fPotential(fPotential) 00198 { 00199 m_bNeuronState = false; 00200 m_iConnected = 0; 00201 } 00202 00203 /***********************************************************************/ 00206 /***********************************************************************/ 00207 TNeuron() 00208 { 00209 m_bNeuronState = false; 00210 m_fPotential = 0; 00211 m_fThreshold = 0; 00212 m_iConnected = 0; 00213 m_uiID = ++m_uiStaticNeuronIDCounter; 00214 } 00215 00216 /***********************************************************************/ 00219 /***********************************************************************/ 00220 TNeuron(const TNeuron& rNeuron) 00221 : m_Axon(rNeuron.m_Axon) 00222 { 00223 m_bNeuronState = rNeuron.m_bNeuronState; 00224 m_fPotential = rNeuron.m_fPotential; 00225 m_fThreshold = rNeuron.m_fThreshold; 00226 m_iConnected = rNeuron.m_iConnected; 00227 m_uiID = rNeuron.m_uiID; 00228 } 00229 00230 /***********************************************************************/ 00233 /***********************************************************************/ 00234 TNeuron& operator=(const TNeuron& rNeuron) 00235 { 00236 m_Axon = rNeuron.m_Axon; 00237 m_bNeuronState = rNeuron.m_bNeuronState; 00238 m_fPotential = rNeuron.m_fPotential; 00239 m_fThreshold = rNeuron.m_fThreshold; 00240 m_iConnected = rNeuron.m_iConnected; 00241 m_uiID = rNeuron.m_uiID; 00242 return *this; 00243 } 00244 00245 /***********************************************************************/ 00248 /***********************************************************************/ 00249 virtual ~TNeuron() {} 00250 00251 /***********************************************************************/ 00254 /***********************************************************************/ 00255 void connect(TNeuron& rPostNeuron, Float fWeight = 0, TSynapse::EGrowMethod eGrowMethod = TSynapse::etAllwaysGrow) 00256 { 00257 m_Axon.add(new TSynapse(rPostNeuron, fWeight, eGrowMethod), false); 00258 } 00259 00260 /***********************************************************************/ 00263 /***********************************************************************/ 00264 bool equals(const TNeuron& rNeuron) const 00265 { 00266 return (this->m_fPotential == rNeuron.m_fPotential && 00267 this->m_fThreshold == rNeuron.m_fThreshold && 00268 this->m_iConnected == rNeuron.m_iConnected && 00269 this->m_Axon == rNeuron.m_Axon && 00270 this->m_uiID == rNeuron.m_uiID); 00271 } 00272 00273 /***********************************************************************/ 00276 /***********************************************************************/ 00277 Int getAxonSize() const 00278 { 00279 return m_Axon.getCount(); 00280 } 00281 00282 /***********************************************************************/ 00285 /***********************************************************************/ 00286 Int getInputSize() const 00287 { 00288 return m_iConnected; 00289 } 00290 00291 /***********************************************************************/ 00294 /***********************************************************************/ 00295 Uint getID() const 00296 { 00297 return m_uiID; 00298 } 00299 00300 /***********************************************************************/ 00303 /***********************************************************************/ 00304 Float getPotential() const 00305 { 00306 return m_fPotential; 00307 } 00308 00309 /***********************************************************************/ 00312 /***********************************************************************/ 00313 Float getThreshold() const 00314 { 00315 return m_fThreshold; 00316 } 00317 00318 /***********************************************************************/ 00321 /***********************************************************************/ 00322 bool isActive() const 00323 { 00324 return m_bNeuronState; 00325 } 00326 00327 /***********************************************************************/ 00330 /***********************************************************************/ 00331 void updateActivity() 00332 { 00333 m_bNeuronState = (m_fPotential > m_fThreshold); 00334 } 00335 00336 /***********************************************************************/ 00339 /***********************************************************************/ 00340 void raisePotential(Float fValue) 00341 { 00342 m_fPotential += fValue; 00343 } 00344 00345 /***********************************************************************/ 00348 /***********************************************************************/ 00349 void resetPotential() 00350 { 00351 m_fPotential = 0; 00352 } 00353 00354 /***********************************************************************/ 00357 /***********************************************************************/ 00358 void setThreshold(Float fValue) 00359 { 00360 m_fThreshold = fValue; 00361 } 00362 00363 /***********************************************************************/ 00366 /***********************************************************************/ 00367 void setActivity(bool bAct) 00368 { 00369 m_bNeuronState = bAct; 00370 } 00371 00372 /***********************************************************************/ 00375 /***********************************************************************/ 00376 void decreaseConnected() 00377 { 00378 --m_iConnected; 00379 } 00380 00381 /***********************************************************************/ 00384 /***********************************************************************/ 00385 void increaseConnected() 00386 { 00387 ++m_iConnected; 00388 } 00389 00390 /***********************************************************************/ 00393 /***********************************************************************/ 00394 TSynapse* getSynapse(Int iIndex) { return m_Axon[iIndex]; } 00395 00396 /***********************************************************************/ 00399 /***********************************************************************/ 00400 TSynapse* operator[](Int iIndex) { return m_Axon[iIndex]; } 00401 00402 /***********************************************************************/ 00405 /***********************************************************************/ 00406 bool operator==(const TNeuron& rNeuron) const 00407 { 00408 return equals(rNeuron); 00409 } 00410 00411 /***********************************************************************/ 00414 /***********************************************************************/ 00415 bool operator!=(const TNeuron& rNeuron) const 00416 { 00417 return !equals(rNeuron); 00418 } 00419 00420 00421 protected: 00422 static Uint m_uiStaticNeuronIDCounter; 00423 00424 private: 00426 bool m_bNeuronState; 00428 Float m_fPotential; 00430 Float m_fThreshold; 00432 TAxon m_Axon; 00434 Int m_iConnected; 00436 Uint m_uiID; 00437 }; 00438 00439 00440 //-------------------------------------------------------------------------------- 00441 /*************************************************************************/ 00447 /*************************************************************************/ 00448 inline TSynapse::TSynapse(TNeuron& rPostNeuron, Float fWeight /*= 0*/, EGrowMethod eGrowMethod /*= etAllwaysGrow*/) 00449 : m_rPostNeuron(rPostNeuron), 00450 m_fWeight(fWeight), 00451 m_eGrowMethod(eGrowMethod) 00452 { 00453 m_rPostNeuron.increaseConnected(); 00454 } 00455 00456 00457 00458 END_NAMESPACE_Zeus 00459 00460 00461 //--------------------------------------------------------------------------- 00462 #endif