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

direct.cc

Go to the documentation of this file.
00001 
00025 
00026 //                                                                          //
00027 //  ___   o                     -----                      | o              //
00028 //  |  \         ___   ___   |  |       _    ___           |     _          //
00029 //  |   | | |/\ /   ) |   \ -+- |---  |/ \  |   \  __   ---| | |/ \   ___   //
00030 //  |   | | |   |---  |      |  |     |   | |     /  \ (   | | |   | (   \  //
00031 //  |__/  | |    \__   \__/   \ |____ |   |  \__/ \__/  ---| | |   |  ---/  //
00032 //                                                                    __/   //
00034 
00035 DirectEncoding::DirectEncoding (const GeneticID& name, const StringMap& params) : ANNEncoding (name, params) {
00036     mPruneInputs = isnull(params["prins"])? true : int(params["prins"]);
00037     mPruneWeights = isnull(params["prwts"])? false : int(params["prwts"]);
00038 }
00039 
00040 DirectEncoding::DirectEncoding (const DirectEncoding& other) : ANNEncoding (other) {
00041     mPruneInputs = other.mPruneInputs;
00042     mPruneWeights = other.mPruneWeights;
00043 }
00044 
00045 void DirectEncoding::copy (const Genstruct& o) {
00046     ANNEncoding::copy (o);
00047     const DirectEncoding& other = static_cast<const DirectEncoding&>(o);
00048     mPruneInputs = other.mPruneInputs;
00049     mPruneWeights = other.mPruneWeights;
00050 }
00051 
00052 void DirectEncoding::addPrivateGenes (Gentainer& g, const StringMap& params) {
00053     Gentainer::addPrivateGenes (g, params);
00054 
00055     if (mPruneInputs)
00056         for (int i=0; i<mInputs; i++)
00057             add (new BinaryGene (format ("R%d", i), 1.0));
00058     
00059     if (mPruneWeights) {
00060         for (int i=0; i<mInputs; i++)
00061             for (int j=0; j<mMaxHidden; j++)
00062                 add (new BinaryGene (format ("W%d-%d", i, j), 1.0));
00063     }
00064         
00065     // Prune hidden
00066     for (int i=0; i<mMaxHidden; i++)
00067         add (new BinaryGene (format ("H%d", i), 1.0));
00068 }
00069 
00070 bool DirectEncoding::execute (const GeneticMsg& msg) const {
00071     FreeNetwork* net = new FreeNetwork (format ("%d-%d-%d", mInputs, mMaxHidden, mOutputs));
00072     
00073     // Go trough each hidden unit and check if it exists
00074     bool hidexists [mMaxHidden];
00075     for (int h=0; h<mMaxHidden; h++) {
00076         hidexists[h] = static_cast<const BinaryGene&> (
00077             (*this)[(CONSTR)format ("H%d", h)]).getvalue();
00078         // TRACE2 ("%d=%d", h, int(hidexists[h]));
00079         
00080         // Enable or disable it from the network
00081         (*net)[h+mInputs].enable(hidexists[h]);
00082     }
00083 
00084 
00085     bool input_exists;
00086     bool w_exists;
00087     
00088     // Connect each input to every hidden neuron
00089     for (int i=0; i < mInputs; i++) {
00090         
00091         // See if this input unit "exists" (if input pruning is enabled)
00092         input_exists = true;
00093         if (mPruneInputs)
00094             input_exists = static_cast<const BinaryGene&> (
00095                 (*this)[(CONSTR)format ("R%d", i)]).getvalue();
00096 
00097         (*net)[i].enable (input_exists);
00098         
00099         // If it exists...
00100         if (input_exists) {
00101             
00102             // Go trough each (existing) hidden unit
00103             for (int h=0; h<mMaxHidden; h++) {
00104                 w_exists = hidexists[h];
00105                 
00106                 // If the hidden unit exists
00107                 if (mPruneWeights) {
00108                     w_exists = static_cast<const BinaryGene&> (
00109                         (*this)[(CONSTR)format ("W%d-%d", i, h)]).getvalue();
00110                 }
00111                 
00112                 // Create the connection if it exists
00113                 if (w_exists)
00114                     net->connect (i, h+mInputs);
00115             }
00116         }
00117     }
00118 
00119     //
00120     // Connect hiddens to outputs
00121     //
00122 
00123     for (int o=0; o<mOutputs; o++) {
00124     
00125         // Go trough each (existing) hidden unit
00126         for (int h=0; h<mMaxHidden; h++) {
00127             w_exists = hidexists[h];
00128             
00129             // If the hidden unit exists
00130             if (mPruneWeights) {
00131                 w_exists = static_cast<const BinaryGene&> (
00132                     (*this)[(CONSTR)format ("W%d-%d", o, h)]).getvalue();
00133             }
00134             
00135             // Create the connection if it exists
00136             if (w_exists)
00137                 net->connect (h+mInputs, o+mInputs+mMaxHidden);
00138         }
00139     }
00140 
00141     // OStream out2;
00142     // *net >> out2;
00143 
00144     net->init (0.5);
00145     msg.host.set ("brainplan", net);
00146 
00147     return true;
00148 }
00149 

Generated on Thu Feb 10 20:21:26 2005 for Annalee by doxygen1.2.18