00001 # include <axon.h>
00002 # include <neuron.h>
00003 # include <qglviewer.h>
00004 # include <qgl.h>
00005 # include <network.h>
00006 # include <inn.h>
00007 # include <interface.h>
00008 # include <randomc.h>
00009
00010 using namespace qglviewer ;
00011
00012 extern Network * network ;
00013 extern Inn * inn ;
00014 extern TRandomMersenne *rg ;
00015
00016 Axon::Axon(int _id, Neuron *_in, Neuron *_out) {
00017 id = _id;
00018 in = _in;
00019 out = _out;
00020
00021 float value = rg->Random();
00022 value /= 10.0 ;
00023 int sign = rg->IRandom(0, 10) ;
00024 if( sign>5 ) value *= -1 ;
00025 weight = value ;
00026 lastWeight = 0.0 ;
00027
00028 frame_ = new ManipulatedFrame() ;
00029 }
00030
00031 int Axon::getId() {
00032 return id;
00033 }
00034
00035
00036 double Axon::getWeight(void) { return weight ; }
00037 void Axon::setWeight( double weight ) { this->weight = weight ; }
00038
00039 Neuron* Axon::getNeuronIn() {
00040 return in;
00041 }
00042
00043 Neuron* Axon::getNeuronOut() {
00044 return out;
00045 }
00046
00047 void Axon::draw(int selected, const bool names) {
00048 if(network->displayaxons()) {
00049 float v = (1.0 / (1 + exp(-1.0 * weight)));
00050 if( selected != id ) glColor4f( 1.0, 1.0-v, 0.0, 0.4 ) ;
00051 else glColor4f( 1.0, 0.0, 1.0, 0.5 ) ;
00052 frame_->setPosition( in->frame()->position() ) ;
00053 drawAxon( in->frame()->position(), out->frame()->position(), names ) ;
00054 }
00055 }
00056
00057 void Axon::drawAxon( Vec from, Vec to, const bool names ) {
00058 if( names ) glPushName(id);
00059 glPushMatrix() ;
00060 glLineWidth( 1.5 ) ;
00061 glDisable(GL_LIGHTING) ;
00062 glBegin(GL_LINES) ;
00063 glVertex3f( from.x, from.y, from.z ) ;
00064 glVertex3f( to.x, to.y, to.z ) ;
00065 glEnd() ;
00066 if( network->displayvalues() ) {
00067 float midx, midy, midz ;
00068 midx = (from.x+to.x)/2 ;
00069 midy = (from.y+to.y)/2 ;
00070 midz = (from.z+to.z)/2 - 1.5*network->neuronsize() ;
00071 glColor3ub( 200, 200, 200 ) ;
00072 QString value ; value.setNum(weight, 'f', 3) ;
00073 float x, y, z ;
00074 inn->viewer->camera()->getPosition( x, y, z ) ;
00075 inn->viewer->drawText( midx, midy, midz, value, QFont( "Arial", 30/((int)z+1) ) );
00076 }
00077 glEnable(GL_LIGHTING) ;
00078 glPopMatrix() ;
00079 if( names ) glPopName() ;
00080 }
00081
00082 void Axon::forwardPropagation() {
00083 out->incOutValueTemp(in->getOutValue() * weight);
00084 }
00085
00086 void Axon::gradientPropagation() {
00087 in->incGradientValueTemp(out->getGradientValue() * weight);
00088 }
00089
00090 void Axon::changeWeight(double convergenceCoeff) {
00091 weight += convergenceCoeff * in->getOutValue() * out->getGradientValue();
00092
00093
00094 }
00095
00096 float Axon::rad2deg( float rad ) {
00097 return rad*(180/3.14159) ;
00098 }
00099
00100 float Axon::deg2rad( float deg ) {
00101 return deg*( 3.14159/180 ) ;
00102 }