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
00038
00039
00040
00041
00042
00044
00058 MillerEncoding::MillerEncoding (
00059 const GeneticID& name,
00060 const StringMap& params)
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
00090 int totalUnits = mInputs+mMaxHidden+mOutputs;
00091 for (int i=0; i<totalUnits-mOutputs; i++) {
00092
00093
00094
00095 if (i>=mInputs || mPruneInputs)
00096 add (new BinaryGene (format ("E%d", i)));
00097
00098
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
00108 double pc=0.5;
00109 do {
00110 pc = mPcAverage+gaussrnd(mPcVariance);
00111 } while (pc<0.0 || pc>1.0);
00112
00113
00114 for (int i=0; i<size(); i++)
00115 if (BinaryGene* gene = dynamic_cast<BinaryGene*> (&(*this)[i]))
00116 gene->setInitP(pc);
00117
00118
00119 Gentainer::init ();
00120 }
00121
00124 bool MillerEncoding::execute (const GeneticMsg& msg) const {
00125
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
00132
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
00139 for (int i=0; i<totalUnits-mOutputs; i++) {
00140 if (i>=mInputs || mPruneInputs)
00141
00142 cmatrix.get(i,i) =
00143 static_cast<const BinaryGene&> ((*this)[(CONSTR)format ("E%d", i)]).getvalue();
00144 }
00145
00146
00147
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
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
00166 for (int i=totalUnits-mOutputs; i<totalUnits; i++)
00167 cmatrix.get(i,i) = 1;
00168
00169
00170
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
00180
00181
00182
00183 String desc;
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
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;
00207 } else {
00208
00209 msg.host.set ("brainplan", net);
00210 }
00211 }
00212
00213 return true;
00214 }
00215
00216