00001 
00025 
00026 
00027 
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     
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     
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         
00079         
00080         
00081         (*net)[h+mInputs].enable(hidexists[h]);
00082     }
00083 
00084 
00085     bool input_exists;
00086     bool w_exists;
00087     
00088     
00089     for (int i=0; i < mInputs; i++) {
00090         
00091         
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         
00100         if (input_exists) {
00101             
00102             
00103             for (int h=0; h<mMaxHidden; h++) {
00104                 w_exists = hidexists[h];
00105                 
00106                 
00107                 if (mPruneWeights) {
00108                     w_exists = static_cast<const BinaryGene&> (
00109                         (*this)[(CONSTR)format ("W%d-%d", i, h)]).getvalue();
00110                 }
00111                 
00112                 
00113                 if (w_exists)
00114                     net->connect (i, h+mInputs);
00115             }
00116         }
00117     }
00118 
00119     
00120     
00121     
00122 
00123     for (int o=0; o<mOutputs; o++) {
00124     
00125         
00126         for (int h=0; h<mMaxHidden; h++) {
00127             w_exists = hidexists[h];
00128             
00129             
00130             if (mPruneWeights) {
00131                 w_exists = static_cast<const BinaryGene&> (
00132                     (*this)[(CONSTR)format ("W%d-%d", o, h)]).getvalue();
00133             }
00134             
00135             
00136             if (w_exists)
00137                 net->connect (h+mInputs, o+mInputs+mMaxHidden);
00138         }
00139     }
00140 
00141     
00142     
00143 
00144     net->init (0.5);
00145     msg.host.set ("brainplan", net);
00146 
00147     return true;
00148 }
00149