Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

topology.cc

Go to the documentation of this file.
00001 
00025 #include "inanna/annetwork.h"
00026 
00027 ANNTopology::~ANNTopology () {
00028 }
00029 
00030 
00031 
00033 //          _   |   | |   | |                           o                    //
00034 //         / \  |\  | |\  | |      ___         ___          _                //
00035 //        /   \ | \ | | \ | |      ___| \   | /   ) |/\ | |/ \   ___         //
00036 //        |---| |  \| |  \| |     (   |  \  | |---  |   | |   | (   \        //
00037 //        |   | |   | |   | |____  \__|   \_/  \__  |   | |   |  ---/        //
00038 //                                       \_/                     __/         //
00040 
00041 ANNLayering::~ANNLayering () {
00042 }
00043 
00054 void ANNLayering::make (const char* description) {
00055     if (!description)
00056         return;
00057     String desc = description;
00058     
00059     Array<String> sizes_tmp;
00060     desc.split (sizes_tmp, '-');
00061     ASSERTWITH (sizes_tmp.size()>=1, "Layering description must have at least 1 layer");
00062 
00063     // Move the string array to self
00064     mLayers.make (sizes_tmp.size());
00065     for (int i=0; i<sizes_tmp.size(); i++)
00066         mLayers[i] = sizes_tmp[i].toInt();
00067 }
00068 
00075 int ANNLayering::layerIndex (int layer) const {
00076     if (layer<0)
00077         layer = mLayers.size()+layer;
00078 
00079     ASSERT (layer>=0 && layer<mLayers.size());
00080     
00081     int index = 0;
00082     for (int k=0; k<layer; k++)
00083         index += mLayers[k];
00084 
00085     return index;
00086 }
00087 
00099 void ANNLayering::getPos (int i, int& layerno, int& layerpos) const {
00100     layerpos=i;
00101     for (layerno=0; layerno<mLayers.size() && layerpos>=mLayers[layerno];
00102          layerpos-=mLayers[layerno++]);
00103     ASSERTWITH (layerno<mLayers.size(), format ("Index %d doesn't map to layering", i));
00104 } 
00105 
00108 String ANNLayering::toString () const {
00109     // Make an array containing the layer sizes
00110     Array<String> layers_arr (layers());
00111     for (int i=0; i<layers(); i++)
00112         layers_arr[i] = String ((*this)[i]);
00113 
00114     // Join the layer sizes into a layered network description string
00115     String layers_str;
00116     layers_str.join (layers_arr, '-');
00117 
00118     return layers_str;
00119 }
00120 
00121 
00122 
00124 // |                                     | -----                |                  //
00125 // |      ___         ___       ___      |   |         --       |                  //
00126 // |      ___| \   | /   ) |/\ /   )  ---|   |    __  |  )  __  |  __   ___  \   | //
00127 // |     (   |  \  | |---  |   |---  (   |   |   /  \ |--  /  \ | /  \ (   \  \  | //
00128 // |____  \__|   \_/  \__  |    \__   ---|   |   \__/ |    \__/ | \__/  ---/   \_/ //
00129 //              \_/                                                     __/   \_/  //
00131 
00132 LayeredTopology::~LayeredTopology () {
00133 }
00134 
00135 void LayeredTopology::build (ANNetwork& network) const {
00136     const ANNLayering& mLayering = dynamic_cast<const ANNLayering&>(*this);
00137     int inputs=mLayering[0];
00138     int outputlimit=mLayering.layerIndex (-1);
00139 
00140     // network.makeNeurons (mLayering.totalUnits());
00141 
00142     // Make the unit array
00143     for (int i=0; i<mLayering.totalUnits(); i++)
00144         // If unit template is given, use it
00145         if (network.getUnitPrototype())
00146             network.add (network.getUnitPrototype()->clone());
00147         else // Ordinary neuron
00148             network.add (new Neuron());
00149 
00150     // Set the unit attributes according to their position in the network
00151     for (int i=0; i<network.size(); i++) {
00152         // Set the unit's type
00153         if (i<inputs)
00154             network[i].setType (Neuron::INPUTUNIT);
00155         else
00156             if (i<outputlimit)
00157                 network[i].setType (Neuron::HIDDENUNIT);
00158         else
00159             network[i].setType (Neuron::OUTPUTUNIT);
00160 
00161         // Set unit's coordinates
00162 
00163         // Calculate unit's layer and offset
00164         int layerno, layerpos;
00165         mLayering.getPos (i, layerno, layerpos);
00166 
00167         // Move the unit to it's proper location
00168         network[i].moveTo (layerno*8, layerpos, 0);
00169     }
00170 }
00171 

Generated on Thu Feb 10 20:06:45 2005 for Inanna by doxygen1.2.18