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

nolfi.cc

Go to the documentation of this file.
00001 
00002 #include <magic/mgdev-eps.h>
00003 #include <magic/mclass.h>
00004 #include <magic/mlsystem.h>
00005 #include <magic/mturtle.h>
00006 
00007 #include <inanna/annetwork.h>
00008 #include <inanna/initializer.h>
00009 #include <nhp/individual.h>
00010 
00011 #include "annalee/nolfi.h"
00012 #include "annalee/nolfinet.h"
00013 
00014 impl_dynamic (NolfiEncoding, {ANNEncoding});
00015 
00016 
00017 
00019 //                                                                           //
00020 //       |   |      |     o -----                      | o                   //
00021 //       |\  |      |  __   |       _    ___           |     _               //
00022 //       | \ |  __  | /   | |---  |/ \  |   \  __   ---| | |/ \   ___        //
00023 //       |  \| /  \ | +-- | |     |   | |     /  \ (   | | |   | (   \       //
00024 //       |   | \__/ | |   | |____ |   |  \__/ \__/  ---| | |   |  ---/       //
00025 //                    |                                           __/        //
00027 
00028 NolfiEncoding::NolfiEncoding (const GeneticID& name, const StringMap& params) : ANNEncoding (name, params) {
00029     /*if (isnull(params["NolfiEncoding.types"])) {
00030         mTypes = (mInputs>mOutputs)? mInputs:mOutputs;
00031         if (mTypes<16)
00032             mTypes = 16;
00033     } else
00034     */
00035     mTypes = params["NolfiEncoding.types"].toInt ();
00036 
00037     mXSize      = getOrDefault (params, "NolfiEncoding.xSize", String(9)).toInt ();
00038     mYSize      = getOrDefault (params, "NolfiEncoding.ySize", String(22)).toInt ();
00039     mAxonScale  = getOrDefault (params, "NolfiEncoding.axonScale", String(0.5)).toDouble ();
00040     mTipRadius  = getOrDefault (params, "NolfiEncoding.tipRadius", String(0.5)).toDouble ();
00041     mMaxHidden  = getOrDefault (params, "NolfiEncoding.neurons", String(0.5)).toInt ();
00042 
00043     ASSERTWITH (mTipRadius>=0.5 || params["NolfiEncoding.tipRadius"]=="auto-network"
00044                 || params["NolfiEncoding.tipRadius"]=="auto-cell",
00045                 format ("NolfiEncoding.tipRadius must be a real-valued radius >=0.5, "
00046                         "\042auto-genome\042, or \042auto-cell\042; "
00047                         "not \042%s\042", (CONSTR) params["NolfiEncoding.tipRadius"]));
00048 }
00049 
00050 NolfiEncoding::NolfiEncoding (const NolfiEncoding& other) : ANNEncoding (other) {
00051     mTypes      = other.mTypes;
00052     mXSize      = other.mXSize;
00053     mYSize      = other.mYSize;
00054     mAxonScale  = other.mAxonScale;
00055     mTipRadius  = other.mTipRadius;
00056 }
00057 
00058 void NolfiEncoding::copy (const Genstruct& o) {
00059     ANNEncoding::copy (o);
00060     const NolfiEncoding& other = static_cast<const NolfiEncoding&>(o);
00061     mTypes      = other.mTypes;
00062     mXSize      = other.mXSize;
00063     mYSize      = other.mYSize;
00064     mAxonScale  = other.mAxonScale;
00065     mTipRadius  = other.mTipRadius;
00066 }
00067 
00068 void NolfiEncoding::addPrivateGenes (Gentainer& g, const StringMap& params) {
00069     Gentainer::addPrivateGenes (g, params);
00070     
00071     //StringMap params;
00072     //params.set("graycoding","0");
00073     for (int i=0; i<mMaxHidden; i++)
00074         NolfiCell::addGenesTo (*this, i, mTypes, mXSize, mYSize, params);
00075 
00076     if (params["NolfiEncoding.tipRadius"] == "auto-network")
00077         add (&(new BitFloatGene ("tipr", 1, 10, 8, params))->hide());
00078 }
00079 
00080 bool NolfiEncoding::execute (const GeneticMsg& msg) const {
00081 
00082     // Read the cells from the genome
00083     NolfiNet nnet (mInputs, mMaxHidden, mOutputs, mXSize, mYSize, mTipRadius, mAxonScale);
00084     nnet.decodeFrom (*this);
00085 
00086     // Build the network
00087     ANNetwork* net = nnet.growNet ();
00088 
00089     //TRACE1 ("%s", (CONSTR) (net->getLayering().toString()));
00090     
00091     // Add the plan to the host
00092     if (net) {
00093         net->setInitializer (new GaussianInitializer ());
00094 
00095         // Take pictures only if this is a picture-taking recreation
00096         bool takePics = int(dynamic_cast<const TakeBrainPicsMsg*>(&msg));
00097 
00098         if (takePics) {
00099             msg.host.set ("brainpic1", new String (nnet.drawEPS()));
00100             msg.host.set ("brainpic2", new String (net->drawEPS()));
00101         }
00102         net->cleanup (true, mPrunePassthroughs);
00103         if (takePics) {
00104             net->drawFeedForward();
00105             msg.host.set ("brainpic3", new String (net->drawEPS()));
00106             delete net; // The net was created only for taking babypics
00107         } else {
00108             // Place the brain description into host
00109             msg.host.set ("brainplan", net);
00110         }
00111     }
00112 
00113     return true;
00114 }
00115 

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