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

puredirect.cc

Go to the documentation of this file.
00001 
00025 #include <magic/mclass.h>
00026 #include <nhp/individual.h>
00027 #include <inanna/annetwork.h>
00028 #include <inanna/initializer.h>
00029 #include "annalee/anngenes.h"
00030 #include "annalee/miller.h"
00031 
00032 impl_dynamic (MillerEncoding, {ANNEncoding});
00033 
00034 
00036 //                                                                           //
00037 //    ----                  ___   o                     -----                //
00038 //    |   )            ___  |  \         ___   ___   |  |       _    ___     //
00039 //    |---  |   | |/\ /   ) |   | | |/\ /   ) |   \ -+- |---  |/ \  |   \    //
00040 //    |     |   | |   |---  |   | | |   |---  |      |  |     |   | |        //
00041 //    |      \__! |    \__  |__/  | |    \__   \__/   \ |____ |   |  \__/    //
00042 //                                                                           //
00044 
00058 MillerEncoding::MillerEncoding (
00059     const GeneticID& name,   //< Name of the gene
00060     const StringMap& params) //< Parameters
00061         : ANNEncoding (name, params)
00062 {
00063     mPruneInputs    = params["MillerEncoding.pruneInputs"].toInt ();
00064     mPcVariance     = params["MillerEncoding.pcVariance"].toDouble ();
00065     mPcAverage      = params["MillerEncoding.pcAverage"].toDouble ();
00066 }
00067 
00068 MillerEncoding::MillerEncoding (const MillerEncoding& other) : ANNEncoding (other) {
00069     mPruneInputs = other.mPruneInputs;
00070     mPcVariance  = other.mPcVariance;
00071     mPcAverage   = other.mPcAverage;
00072 }
00073 
00076 void MillerEncoding::copy (const Genstruct& o) {
00077     ANNEncoding::copy (o);
00078     const MillerEncoding& other = static_cast<const MillerEncoding&>(o);
00079     mPruneInputs = other.mPruneInputs;
00080     mPcVariance = other.mPcVariance;
00081     mPcAverage = other.mPcAverage;
00082 }
00083 
00086 void MillerEncoding::addPrivateGenes (Gentainer& g, const StringMap& params) {
00087     Gentainer::addPrivateGenes (g, params);
00088 
00089     // Create genes for the units
00090     int totalUnits = mInputs+mMaxHidden+mOutputs;
00091     for (int i=0; i<totalUnits-mOutputs; i++) {
00092 
00093         // Existance of a neuron. Not encoded for input units if input
00094         // pruning is not enabled, nor output units which always exist
00095         if (i>=mInputs || mPruneInputs)
00096             add (new BinaryGene (format ("E%d", i)));
00097         
00098         // Connect input units and hidden units to all successive neurons
00099         for (int j=i+1; j<totalUnits; j++)
00100                 add (&(new BinaryGene (format ("W%d-%d", i, j)))->hide());
00101     }
00102 }
00103 
00106 void MillerEncoding::init () {
00107     // Find random p_c (connection probability) for the genome
00108     double pc=0.5;
00109     do {
00110         pc = mPcAverage+gaussrnd(mPcVariance);
00111     } while (pc<0.0 || pc>1.0); // Not before it's in a range of a probability value
00112 
00113     // Change the pc of all binary genes we own
00114     for (int i=0; i<size(); i++)
00115         if (BinaryGene* gene = dynamic_cast<BinaryGene*> (&(*this)[i]))
00116             gene->setInitP(pc);
00117 
00118     // Let the superclass implement the initialization
00119     Gentainer::init ();
00120 }
00121 
00124 bool MillerEncoding::execute (const GeneticMsg& msg) const {
00125     // Take pictures only if this is a picture-taking recreation
00126     bool takePics = int(dynamic_cast<const TakeBrainPicsMsg*>(&msg));
00127 
00128     ANNetwork* net = new ANNetwork (format ("%d-%d-%d", mInputs, mMaxHidden, mOutputs));
00129     int totalUnits = mInputs+mMaxHidden+mOutputs;
00130     
00131     // Create and zero the connection matrix. Using this matrix is
00132     // useful mostly just for informative purposes (in logging)
00133     PackTable<int> cmatrix (totalUnits,totalUnits);
00134     for (int i=0; i<cmatrix.rows; i++)
00135         for (int j=0; j<cmatrix.rows; j++)
00136             cmatrix.get(i,j) = 0;
00137 
00138     // Go trough each input and hidden unit and check if it exists
00139     for (int i=0; i<totalUnits-mOutputs; i++) {
00140         if (i>=mInputs || mPruneInputs)
00141             // Enable or disable the unit from the network
00142             cmatrix.get(i,i) =
00143                 static_cast<const BinaryGene&> ((*this)[(CONSTR)format ("E%d", i)]).getvalue();
00144     }
00145 
00146 
00147     // Fill the connection matrix
00148     for (int i=0; i<totalUnits-mOutputs; i++)
00149         for (int j=i+1; j<totalUnits; j++)
00150             if (j>=mInputs)
00151                 if (static_cast<const BinaryGene&> (
00152                     (*this)[(CONSTR)format ("W%d-%d", i, j)]).getvalue())
00153                     cmatrix.get(i,j) = 1;
00154 
00155     // Calculate some statistics (probability of connection)
00156     int conns=0, totconns=0;
00157     for (int i=0; i<totalUnits-mOutputs; i++)
00158         for (int j=(i>=mInputs)?i+1:mInputs; j<totalUnits; j++) {
00159             totconns++;
00160             conns += cmatrix.get(i,j);
00161         }
00162     double pConn = double(conns) / double(totconns);
00163     msg.host.set ("pConn", new String (format ("%f", pConn)));
00164 
00165     // Enable outputs
00166     for (int i=totalUnits-mOutputs; i<totalUnits; i++)
00167         cmatrix.get(i,i) = 1;
00168     
00169     // Enable and connect according to the connection matrix (if both
00170     // source and target units exist and also the connection)
00171     for (int i=0; i<cmatrix.rows; i++) {
00172         (*net)[i].enable(cmatrix.get(i,i));
00173         if (cmatrix.get(i,i))
00174             for (int j=i+1; j<cmatrix.rows; j++)
00175                 if (cmatrix.get(j,j) && cmatrix.get(i,j))
00176                     net->connect (i,j);
00177     }
00178 
00179     //for (int i=totalUnits-mOutputs; i<totalUnits; i++)
00180     //  (*net)[i].setTFunc (FreeNeuron::LINEAR_TF);
00181 
00182     // "Print" network connectivity matrix
00183     String desc; // Printed connection matrix
00184     if (takePics) {
00185         desc.reserve(totalUnits*(totalUnits+1)+10);
00186         for (int i=0; i<cmatrix.rows; i++) {
00187             for (int j=0; j<cmatrix.cols; j++)
00188                 desc += cmatrix.get(i,j)? '1':'0';
00189             desc += '\n';
00190         }
00191     }
00192     
00193     if (net) {
00194         net->setInitializer (new GaussianInitializer (0.5));
00195 
00196         // Take some nice photos
00197         net->cleanup ();
00198         if (takePics)
00199             msg.host.set ("brainpic1", new String (net->drawEPS()));
00200         net->cleanup (true, mPrunePassthroughs);
00201         if (takePics) {
00202             msg.host.set ("brainpic2", new String (net->drawEPS()));
00203             net->drawFeedForward();
00204             msg.host.set ("brainpic3", new String (net->drawEPS()));
00205             msg.host.set ("braindesc1", new String (desc));
00206             delete net; // The net was created only for taking babypics
00207         } else {
00208             // Place the brain description into host
00209             msg.host.set ("brainplan", net);
00210         }
00211     }
00212 
00213     return true;
00214 }
00215 
00216 

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