Main Page | Namespace List | Data Structures | File List | Data Fields | Globals

layer.cpp

Go to the documentation of this file.
00001 # include <layer.h>
00002 # include <network.h>
00003 # include <qglviewer.h>
00004 # include <inn.h>
00005 # include <interface.h>
00006 # include <qfont.h>
00007 
00008 extern Network * network ;
00009 extern Inn * inn ;
00010 
00011 Layer::Layer(int _id) {
00012    id = _id;
00013    neurons = new list<Neuron*>();
00014    frame_ = new ManipulatedFrame() ;
00015 }
00016 
00017 int Layer::getId() {
00018         return id;
00019 }
00020 
00021 list<Neuron*>* Layer::getNeurons() {
00022         return neurons;
00023 }
00024 
00025 int Layer::getNum() {
00026         return network->getLayerNumFromLayerId( id ) ;
00027 }
00028 
00029 Neuron* Layer::getNeuronFromId(int id_neur) {
00030         list<Neuron*>::iterator iter;
00031         for (iter = neurons->begin(); iter != neurons->end(); iter++)
00032                 if ((*iter)->getId() == id_neur) return (*iter);
00033         return NULL;
00034 }
00035 
00036 void Layer::draw( int selected, const bool names ) {    
00037         if(network->displaylayers()) {
00038                 Vec position = frame_->position() ;
00039                 position.y = (float)getNum() ;
00040                 frame_->setPosition( position ) ;
00041                 if( selected != id ) glColor4f( 0.3, 0.3, 1.0, 0.3 ) ;
00042                 else glColor4f( 1.0, 0.0, 1.0, 0.3 ) ;
00043                 drawLayer( names ) ;
00044         }
00045 }
00046 
00047 void Layer::drawNeurons( int selected, const bool names ) {
00048         list<Neuron*>::iterator iter;
00049         for (iter = neurons->begin(); iter != neurons->end(); iter++)
00050                 (*iter)->draw( selected, names ) ;
00051 }
00052 
00053 void Layer::drawLayer( const bool names ) {
00054         glEnable(GL_BLEND) ;
00055         if( names ) glPushName(id) ;
00056                 glPushMatrix() ;
00057                         glMultMatrixd(frame_->matrix());
00058                         glRotatef( 90, 1.0, 0.0, 0.0 ) ; 
00059                         GLUquadricObj * qobj = gluNewQuadric() ;
00060                         gluQuadricDrawStyle( qobj, GLU_FILL ) ;
00061                         gluQuadricNormals( qobj, GLU_SMOOTH ) ;
00062                         gluDisk(qobj, 0.0, 2.5, network->shininess(), network->shininess()) ;
00063                         glColor4f( 1.0, 1.0, 0.0, 0.4 ) ;
00064                         glutSolidTorus( 0.05, 2.5, network->shininess(), network->shininess()) ;
00065                         glColor3ub( 200, 200, 200 ) ;
00066                 glPopMatrix() ;
00067         if( names ) glPopName() ;
00068 }
00069 
00070 ManipulatedFrame * Layer::frame( int id ) {
00071         if( id == this->id ) return frame_ ;
00072         list<Neuron*>::iterator iter;
00073         for (iter = neurons->begin(); iter != neurons->end(); iter++)
00074         if ((*iter)->getId() == id) return (*iter)->frame() ;
00075     return NULL ;
00076 }
00077 
00078 int Layer::nbNeurons() {
00079         return neurons->size();
00080 }
00081 
00082 void Layer::addNeuron(int id) {
00083         /* Pour le wizard */
00084         Neuron *n = new Neuron(id);
00085         neurons->push_back(n);
00086         updateNeuronsPos() ;
00087 }
00088 
00089 void Layer::addNeuron(int id, float x, float z) {
00090         /* Pour le reste */
00091         Neuron *n = new Neuron(id);
00092         n->setX(x);
00093         n->setZ(z);
00094         neurons->push_back(n);
00095         //updateNeuronsPos();
00096 }
00097 
00098 void Layer::removeNeuron(int id) {
00099         bool found = false;
00100         list<Neuron*>::iterator iter = neurons->begin();
00101         while (iter != neurons->end() && !found) {
00102                 if((*iter)->getId() == id) found = true;
00103                 if (!found) iter++;
00104         }
00105         if (iter != neurons->end()) {
00106                 (*iter)->removeAxons();
00107                 neurons->erase(iter);
00108                 delete (*iter);
00109         }
00110 }
00111 
00112 void Layer::removeNeurons() {
00113         list<Neuron*>::iterator iter;
00114         for (iter = neurons->begin(); iter != neurons->end(); iter++) {
00115                 (*iter)->removeAxons();
00116                 delete (*iter);
00117         }
00118         neurons->clear();
00119 }
00120 
00121 bool Layer::existNeuron(int id) {
00122         list<Neuron*>::iterator iter;
00123         for (iter = neurons->begin(); iter != neurons->end(); iter++)
00124                 if ((*iter)->getId() == id) return true;
00125         return false;
00126 }
00127 
00128 void Layer::setNeuronsLinePos( int n, list<Neuron*>::iterator *iter, float z ) {
00129         int i;
00130         if (n==1) {
00131                 Vec position ;
00132                 position.x = 0.0;
00133                 position.y = (float)this->getNum();
00134                 position.z = z;
00135                 (*(*iter))->frame()->setPosition( position );
00136         } else if( n>1 && (n%2)==0 ) {
00137                 int nbL = n / 2;
00138                 for (i = nbL; i > 0; i--) {
00139                         Vec position ;
00140                 position.x = -((i*2)-1)*(2*network->neuronsize());
00141                 position.y = (float)this->getNum() ;
00142                 position.z = z ;
00143                 (*(*iter))->frame()->setPosition( position ) ;
00144                 (*iter)++;
00145                 }
00146                 for (i = 1; i <= nbL; i++) {
00147                         Vec position ;
00148                 position.x = ((i*2)-1)*(2*network->neuronsize());
00149                 position.y = (float)this->getNum() ;
00150                 position.z = z ;
00151                 (*(*iter))->frame()->setPosition( position ) ;
00152                 (*iter)++;
00153                 }
00154         } else if( n>1 && (n%2)!=0 ) {
00155                 int nbL = n / 2;
00156                 for (i = nbL; i > 0; i--) {
00157                         Vec position ;
00158                 position.x = (-i*2)*(2*network->neuronsize());
00159                 position.y = (float)this->getNum() ;
00160                 position.z = z ;
00161                 (*(*iter))->frame()->setPosition( position ) ;
00162                 (*iter)++;
00163                 }
00164                 for (i = 0; i <= nbL; i++) {
00165                         Vec position ;
00166                 position.x = (i*2)*(2*network->neuronsize());
00167                 position.y = (float)this->getNum() ;
00168                 position.z = z ;
00169                 (*(*iter))->frame()->setPosition( position ) ;
00170                 (*iter)++;
00171                 }
00172         }
00173 }
00174 
00175 void Layer::updateNeuronsPos( void ) {
00176         int i;
00177         int n = neurons->size() ;
00178         int nbNeuronsPerLine = (int)ceil(sqrt((double)n));
00179         int nbFullLines = n / nbNeuronsPerLine;
00180         int nbNeuronsOnLastLine = n - (nbFullLines * nbNeuronsPerLine);
00181         int nbLines;
00182         if (nbNeuronsOnLastLine == 0)
00183                 nbLines = nbFullLines; 
00184         else nbLines = nbFullLines + 1;
00185         list<Neuron*>::iterator iter = neurons->begin();
00186         int countLines = 0;
00187         
00188         if (nbLines == 1) {
00189                 setNeuronsLinePos(n, &iter, 0.0);
00190         } else if (nbLines>1 && (nbLines%2)==0) {
00191                 int nbF = nbLines / 2;
00192                 for (i = nbF; i > 0; i--) {
00193                         countLines++;
00194                 double z = ((i*2)-1)*(2*network->neuronsize());
00195                 setNeuronsLinePos(nbNeuronsPerLine, &iter, z);
00196                 }
00197                 for (i = 1; i <= nbF; i++) {
00198                         countLines++;
00199                         double z = -((i*2)-1)*(2*network->neuronsize());
00200                         if (nbNeuronsOnLastLine != 0 && countLines == nbLines)
00201                                 setNeuronsLinePos(nbNeuronsOnLastLine, &iter, z);
00202                 else setNeuronsLinePos(nbNeuronsPerLine, &iter, z);
00203                 }
00204         } else if( nbLines>1 && (nbLines%2)!=0 ) {
00205                 int nbF = nbLines / 2;
00206                 for (i = nbF; i > 0; i--) {
00207                         countLines++;
00208                         double z = (i*2)*(2*network->neuronsize());
00209                 setNeuronsLinePos(nbNeuronsPerLine, &iter, z);
00210                 }
00211                 for (i = 0; i <= nbF; i++) {
00212                         countLines++;
00213                         double z = (-i*2)*(2*network->neuronsize());
00214                 if (nbNeuronsOnLastLine != 0 && countLines == nbLines)
00215                         setNeuronsLinePos(nbNeuronsOnLastLine, &iter, z);
00216                 else setNeuronsLinePos(nbNeuronsPerLine, &iter, z);
00217                 }
00218         }
00219 }
00220 
00221 void Layer::clearAxons() {
00222         list<Neuron*>::iterator iter;
00223         for (iter = neurons->begin(); iter != neurons->end(); iter++)
00224                 (*iter)->removeAxons();
00225 }
00226 
00227 void Layer::resetAllValues() {
00228         list<Neuron*>::iterator iter;
00229         for (iter = neurons->begin(); iter != neurons->end(); iter++) {
00230                 (*iter)->setOutValue(0.0);
00231                 (*iter)->setGradientValue(0.0);
00232                 (*iter)->setOutValueTemp(0.0);
00233                 (*iter)->setGradientValueTemp(0.0);
00234         }
00235 }
00236 
00237 void Layer::resetTempValues() {
00238         list<Neuron*>::iterator iter;
00239         for (iter = neurons->begin(); iter != neurons->end(); iter++) {
00240                 (*iter)->setOutValueTemp(0.0);
00241                 (*iter)->setGradientValueTemp(0.0);
00242         }
00243 }
00244 
00245 void Layer::forwardPropagation() {
00246         list<Neuron*>::iterator iter;
00247         for (iter = neurons->begin(); iter != neurons->end(); iter++)
00248                 (*iter)->forwardPropagation();
00249 }
00250 
00251 void Layer::gradientBackPropagation() {
00252         list<Neuron*>::iterator iter;
00253         for (iter = neurons->begin(); iter != neurons->end(); iter++)
00254                 (*iter)->gradientBackPropagation();
00255 }
00256 
00257 void Layer::description() {
00258         cout << "    Layer id =  " << id << " / Nb neurons = " << neurons->size() << endl;
00259 }

Generated on Fri Dec 3 14:57:50 2004 for INN by doxygen 1.3.6