00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef MODELNEURON_LAYER_H_DEFINED
00038 #define MODELNEURON_LAYER_H_DEFINED
00039
00040 #include "ModelNeuron/SimLayer.H"
00041 #include "ModelNeuron/Location.H"
00042 #include "ModelNeuron/Weights.H"
00043 #include "ModelNeuron/SimUnit.H"
00044 #include "ModelNeuron/NeuralSimUtils.H"
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template<class T, class W>
00055 class Layer : public SimLayer
00056 {
00057 public:
00058
00059 Layer(const SimTime& timestep, const uint width, const uint height,
00060 const std::string& name = "", const std::string& units = "");
00061
00062
00063 Layer(const double& param1, const SimTime& timestep, const uint width, const uint height,
00064 const std::string& name = "", const std::string& units = "");
00065
00066
00067 Layer(const double& param1, const double& param2,
00068 const bool doSubCenter, const BorderPolicy bp,
00069 const SimTime& timestep, const uint width, const uint height,
00070 const std::string& name = "", const std::string& units = "");
00071
00072
00073 Layer(const double& param1, const double& param2,
00074 const double& param3, const bool doSubCenter,
00075 const BorderPolicy bp,
00076 const SimTime& timestep, const uint width, const uint height,
00077 const std::string& name = "", const std::string& units = "");
00078
00079
00080 Layer(const double& param1, const double& param2,
00081 const double& param3, const double& param4,
00082 const bool subCenter, const BorderPolicy bp,
00083 const SimTime& timestep, const uint width, const uint height,
00084 const std::string& name = "", const std::string& units = "");
00085
00086
00087 Layer(const Layer& nlc);
00088
00089
00090 Layer& operator=(const Layer& nlc);
00091
00092
00093 virtual ~Layer();
00094
00095
00096 Image<double> getDisplayOutput(const int pos = -1) const;
00097
00098
00099 void getSimUnit(const Location& loc, std::vector<const SimUnit*>& units);
00100
00101
00102 void editSimUnit(const Location& loc, std::vector<SimUnit*>& units);
00103
00104
00105 const uint numSimUnits() const;
00106
00107
00108
00109
00110 const SimUnit& getSimUnit(const uint pos) const;
00111
00112
00113 SimUnit& editSimUnit(const uint pos);
00114
00115
00116 void setModule(const SimUnit& nsm);
00117
00118
00119 void setModule(const SimUnit& nsm, const Location& p);
00120
00121
00122 void setTime(const SimTime& time, const bool recursive = true);
00123
00124
00125
00126 void setWeights(const W& weights);
00127
00128
00129 Layer<T,W>* clone () const;
00130
00131 private:
00132
00133 void doIntegrate(const SimTime& dt, const Image<double>& inpe, const Image<double>& inpi);
00134
00135
00136 void doInit();
00137
00138
00139 const bool isFull(const Location& pos = Location(-1));
00140
00141
00142 void setModule(const SimUnit& nsm, nsu::Int2Type<false>);
00143 void setModule(const SimUnit& nsm, const Location& pos, nsu::Int2Type<false>);
00144
00145
00146 void setModule(const SimUnit& nsm, nsu::Int2Type<true>);
00147 void setModule(const SimUnit& nsm, const Location& pos, nsu::Int2Type<true>);
00148
00149 template <class U> struct non_pointer_type { typedef U Type; };
00150 template <class U> struct non_pointer_type<U*> { typedef U Type; };
00151
00152
00153 typedef typename nsu::vector<T>::iterator iterator;
00154 typedef typename nsu::vector<T>::const_iterator const_iterator;
00155
00156
00157 typedef typename Image<double>::iterator d_iterator;
00158 typedef typename Image<double>::const_iterator const_d_iterator;
00159
00160
00161 typedef typename non_pointer_type<T>::Type cast_type;
00162
00163 nsu::vector<T> itsL;
00164 W itsW;
00165
00166 std::vector<bool> itsFilled;
00167 bool itsFull;
00168
00169 Location itsProbe;
00170 };
00171
00172
00173
00174
00175 template<class T, class W>
00176 Layer<T,W>::Layer(const SimTime& timestep, const uint width, const uint height,
00177 const std::string& name, const std::string& units) :
00178 SimLayer(timestep, width, height, name, units),
00179 itsL(), itsW(), itsFilled(width * height, false),
00180 itsFull(false), itsProbe() { itsProbe.setHyperCubeDims(width, height); }
00181
00182
00183 template<class T, class W>
00184 Layer<T,W>::Layer(const double& param1, const SimTime& timestep,
00185 const uint width, const uint height,
00186 const std::string& name, const std::string& units)
00187 : SimLayer(timestep, width, height, name, units),
00188 itsL(), itsW(param1), itsFilled(width * height, false), itsFull(false), itsProbe()
00189 { itsProbe.setHyperCubeDims(width, height); }
00190
00191
00192 template<class T, class W>
00193 Layer<T,W>::Layer(const double& param1, const double& param2,
00194 const bool doSubCenter, const BorderPolicy bp,
00195 const SimTime& timestep,
00196 const uint width, const uint height,
00197 const std::string& name, const std::string& units)
00198 : SimLayer(timestep, width, height, name, units),
00199 itsL(), itsW(param1,param2,doSubCenter,bp),
00200 itsFilled(width * height, false), itsFull(false), itsProbe()
00201 { itsProbe.setHyperCubeDims(width, height); }
00202
00203
00204 template<class T, class W>
00205 Layer<T,W>::Layer(const double& param1, const double& param2,
00206 const double& param3, const bool doSubCenter,
00207 const BorderPolicy bp, const SimTime& timestep,
00208 const uint width, const uint height,
00209 const std::string& name, const std::string& units)
00210 : SimLayer(timestep, width, height, name, units),
00211 itsL(), itsW(param1,param2,param3,doSubCenter,bp),
00212 itsFilled(width * height, false), itsFull(false), itsProbe()
00213 { itsProbe.setHyperCubeDims(width, height); }
00214
00215
00216 template<class T, class W>
00217 Layer<T,W>::Layer(const double& param1, const double& param2,
00218 const double& param3, const double& param4,
00219 const bool doSubCenter,
00220 const BorderPolicy bp,const SimTime& timestep,
00221 const uint width, const uint height,
00222 const std::string& name,
00223 const std::string& units) :
00224 SimLayer(timestep, width, height, name, units),
00225 itsL(), itsW(param1,param2,param3,param4,doSubCenter,bp),
00226 itsFilled(width * height, false), itsFull(false), itsProbe()
00227 { itsProbe.setHyperCubeDims(width, height); }
00228
00229
00230 template<class T, class W>
00231 Layer<T,W>::Layer(const Layer<T,W>& rhs) :
00232 SimLayer(rhs), itsL(rhs.itsL), itsW(rhs.itsW),
00233 itsFilled(rhs.itsFilled), itsFull(rhs.itsFull), itsProbe(rhs.itsProbe)
00234 { }
00235
00236
00237 template<class T, class W>
00238 Layer<T,W>& Layer<T,W>::operator=(const Layer<T,W>& rhs)
00239 {
00240 if (this != &rhs)
00241 {
00242 SimLayer::operator=(rhs);
00243 itsL = rhs.itsL;
00244 itsW = rhs.itsW;
00245 itsFilled = rhs.itsFilled;
00246 itsFull = rhs.itsFull;
00247 itsProbe = rhs.itsProbe;
00248 }
00249 return *this;
00250 }
00251
00252
00253 template<class T, class W>
00254 Layer<T,W>::~Layer()
00255 { }
00256
00257
00258 template<class T, class W>
00259 Image<double> Layer<T,W>::getDisplayOutput(const int pos) const
00260 {
00261 ASSERT(pos < 1);
00262 Image<double> out(getOutDims(), NO_INIT);
00263 if (itsFull)
00264 {
00265 d_iterator outiter(out.beginw());
00266 const_iterator nsmptr(itsL.begin()), end(itsL.end());
00267
00268 while (nsmptr != end)
00269 *outiter++ = (nsmptr++)->getDisplayOutput();
00270 }
00271 return out;
00272 }
00273
00274
00275 template<class T, class W>
00276 void Layer<T,W>::getSimUnit(const Location& loc, std::vector<const SimUnit*>& units)
00277 {
00278
00279 itsProbe.setLocation(loc);
00280 const std::vector<int>& locs = itsProbe.getLinearPos();
00281
00282
00283 std::vector<int>::const_iterator iter(locs.begin());
00284 while (iter != locs.end())
00285 units.push_back(&getSimUnit(*iter++));
00286 }
00287
00288
00289 template<class T, class W>
00290 void Layer<T,W>::editSimUnit(const Location& loc, std::vector<SimUnit*>& units)
00291 {
00292
00293 itsProbe.setLocation(loc);
00294 const std::vector<int>& locs = itsProbe.getLinearPos();
00295
00296 std::vector<int>::const_iterator iter(locs.begin());
00297 while (iter != locs.end())
00298 units.push_back(&editSimUnit(*iter++));
00299 }
00300
00301
00302 template<class T, class W>
00303 const uint Layer<T,W>::numSimUnits() const
00304 {
00305 return getOutSize();
00306 }
00307
00308
00309 template<class T, class W>
00310 const SimUnit& Layer<T,W>::getSimUnit(const uint pos) const
00311 {
00312 if (pos > itsL.size())
00313 if (itsL.isNull(pos))
00314 LFATAL("That unit has not yet been assigned");
00315 return itsL[pos];
00316 }
00317
00318
00319 template<class T, class W>
00320 SimUnit& Layer<T,W>::editSimUnit(const uint pos)
00321 {
00322 if (pos > itsL.size())
00323 if (itsL.isNull(pos))
00324 LFATAL("That unit has not yet been assigned");
00325 return itsL[pos];
00326 }
00327
00328 template<class T, class W>
00329 void Layer<T,W>::setModule(const SimUnit& mod)
00330 {
00331 setModule(mod, nsu::Int2Type<nsu::TypeTraits<T>::isPointer >());
00332 }
00333
00334
00335
00336 template<class T, class W>
00337 void Layer<T,W>::setModule(const SimUnit& mod, const Location& pos)
00338 {
00339 setModule(mod, pos, nsu::Int2Type<nsu::TypeTraits<T>::isPointer >());
00340 }
00341
00342
00343 template<class T, class W>
00344 void Layer<T,W>::setWeights(const W& weights)
00345 {
00346 itsW = weights;
00347 }
00348
00349
00350 template<class T, class W>
00351 void Layer<T,W>::setTime(const SimTime& time, const bool recursive)
00352 {
00353
00354 SimLayer::setTime(time, false);
00355
00356 if (recursive)
00357 {
00358 iterator nsmptr(itsL.begin()), end(itsL.end());
00359 std::vector<bool>::const_iterator fptr(itsFilled.begin());
00360
00361 while (nsmptr != end)
00362 {
00363 if (*fptr)
00364 nsmptr->setTime(time, true);
00365
00366 ++fptr; ++nsmptr;
00367 }
00368 }
00369 }
00370
00371
00372 template<class T, class W>
00373 void Layer<T,W>::doIntegrate(const SimTime& dt, const Image<double>& inpe, const Image<double>& inpi)
00374 {
00375 if (itsFull && itsW.initialized())
00376 {
00377
00378 const Image<double> neighborhood = itsW.compute( itsOutput );
00379 const_d_iterator i_n(neighborhood.begin());
00380
00381
00382 iterator i_l(itsL.begin()), end(itsL.end());
00383
00384
00385 d_iterator i_out(itsOutput.beginw());
00386
00387
00388 const_d_iterator i_exc(inpe.begin()), i_inh(inpi.begin());
00389
00390
00391 while (i_l != end)
00392 {
00393
00394
00395 i_l->input(*i_n++);
00396 i_l->inputExc(*i_exc++);
00397 i_l->inputInh(*i_inh++);
00398 i_l->evolve(getTime());
00399 *i_out++ = (i_l++)->getOutput();
00400 }
00401 }
00402 else
00403 { LFATAL("All positions in the layer have not been filled with modules"
00404 " or the weights have not yet been setup");
00405 }
00406 }
00407
00408
00409 template<class T, class W>
00410 void Layer<T,W>::doInit()
00411 {
00412 for (uint i = 0; i < itsL.size(); ++i)
00413 if (~itsL.isNull(i))
00414 itsL[i].initialize();
00415 }
00416
00417
00418 template<class T, class W>
00419 Layer<T,W>* Layer<T,W>::clone () const
00420 { return new Layer<T,W>(*this); };
00421
00422
00423 template<class T, class W>
00424 void Layer<T,W>::setModule(const SimUnit& nsm, nsu::Int2Type<false>)
00425 {
00426 this->setUnits(nsm.getUnits());
00427 const cast_type temp = dynamic_cast<const cast_type&>(nsm);
00428 itsL.clear();
00429 for (uint i = 0; i < getOutSize(); ++i)
00430 itsL.push_back(temp);
00431 itsFull = true;
00432 itsFilled = std::vector<bool>(itsFilled.size(), true);
00433 }
00434
00435
00436 template<class T, class W>
00437 void Layer<T,W>::setModule(const SimUnit& nsm, nsu::Int2Type<true>)
00438 {
00439 this->setUnits(nsm.getUnits());
00440 itsL.clear();
00441 for (uint i = 0; i < getOutSize(); ++i)
00442 itsL.push_back(nsm);
00443 itsFull = true;
00444 itsFilled = std::vector<bool>(itsFilled.size(), true);
00445 }
00446
00447
00448 template<class T, class W>
00449 void Layer<T,W>::setModule(const SimUnit& nsm, const Location& pos, nsu::Int2Type<false>)
00450 {
00451 const cast_type temp = dynamic_cast<const cast_type&>(nsm);
00452 Location p = pos;
00453 const std::vector<int>& locs = p.getLinearPos(getOutWidth(), getOutHeight());
00454 std::vector<int>::const_iterator iter(locs.begin());
00455 while (iter != locs.end())
00456 {
00457 if (*iter < (int)itsL.size())
00458 itsL.set_at(*iter, temp);
00459 else
00460 for (int i = itsL.size(); i <= *iter; ++i)
00461 itsL.push_back(temp);
00462
00463 itsFilled[*iter++] = true;
00464 }
00465 itsFull = isFull();
00466 }
00467
00468
00469 template<class T, class W>
00470 void Layer<T,W>::setModule(const SimUnit& nsm, const Location& pos, nsu::Int2Type<true>)
00471 {
00472 Location p = pos;
00473 const std::vector<int>& locs = p.getLinearPos(getOutWidth(), getOutHeight());
00474 std::vector<int>::const_iterator iter(locs.begin());
00475 while (iter != locs.end())
00476 {
00477 if (*iter < (int)itsL.size())
00478 itsL.set_at(*iter, nsm);
00479 else
00480 for (int i = (int)itsL.size(); i <= *iter; ++i)
00481 itsL.push_back(nsm);
00482
00483 itsFilled[*iter++] = true;
00484 }
00485 itsFull = isFull();
00486 }
00487
00488
00489 template<class T, class W>
00490 const bool Layer<T,W>::isFull(const Location& pos)
00491 {
00492 std::vector<bool>::const_iterator i(itsFilled.begin()), end(itsFilled.end());
00493 while (i != end)
00494 if (*i++ == false)
00495 return false;
00496
00497 return true;
00498 }
00499
00500 #endif
00501
00502
00503
00504
00505
00506