00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef NETWORK_H
00018 #define NETWORK_H
00019
00020 using namespace std;
00021
00022 #include <amygdala/types.h>
00023
00024 #if GCC_VERSION >= 30000
00025 #include <ext/hash_map>
00026 #else
00027 #include <hash_map>
00028 #endif
00029 #include <string>
00030 #include <queue>
00031 #include <amygdala/mpspikeinput.h>
00032
00033
00034 enum NEvent { NOACTION, RMSPIKE, SPIKE, INPUTSPIKE, RESPIKE };
00035 class Neuron;
00036 class FunctionLookup;
00037 class Layer;
00038 class SpikeInput;
00039 class Synapse;
00040
00041
00063 class Network {
00064 public:
00065
00066 friend class Neuron;
00067 friend void MpSpikeInput::ReadInputBuffer();
00068
00069 Network();
00070 virtual ~Network();
00071
00078 void ScheduleNEvent(NEvent eventType,
00079 AmTimeInt eventTime,
00080 Neuron* reqNrn);
00087 void ScheduleNEvent(NEvent eventType,
00088 AmTimeInt eventTime,
00089 AmIdInt reqNrnId);
00090
00096 Neuron* AddNeuron(NeuronRole nRole, AmIdInt nId);
00097
00104 Neuron* AddNeuron(NeuronRole nRole, Neuron* nrn);
00105
00113 bool ConnectNeurons(AmIdInt preSynapticNeuron,
00114 AmIdInt postSynapticNeuron,
00115 float weight,
00116 AmTimeInt delay=0);
00117
00125 bool ConnectNeurons(Neuron* preSynapticNeuron,
00126 Neuron* postSynapticNeuron,
00127 float weight,
00128 AmTimeInt delay=0);
00129
00138 void Run(AmTimeInt maxRunTime);
00139
00141 int Size() { return net.size(); }
00142
00144 int LayerCount() { return layers.size(); }
00145
00148 typedef hash_map<AmIdInt, Neuron*>::const_iterator const_iterator;
00149
00151 const_iterator begin() const { return net.begin(); }
00152
00154 const_iterator end() const { return net.end(); }
00155
00158 typedef hash_map<unsigned int, Layer*>::iterator layer_iterator;
00159
00161 layer_iterator layer_begin() { return layers.begin(); }
00162
00164 layer_iterator layer_end() { return layers.end(); }
00165
00169 void AddLayer(Layer* newLayer);
00170
00172 AmIdInt MaxNeuronId() { return (nextNeuronId - 1); }
00173
00175 bool Layered() { return isLayered; }
00176
00181 static void SetTimeStepSize(AmTimeInt stepSize);
00182
00184 static AmTimeInt TimeStepSize() { return simStepSize; }
00185
00187 static AmTimeInt SimTime() { return simTime; }
00188
00192 Neuron* GetNeuron(AmIdInt nId) { return net[nId]; }
00193
00195 SpikeInput* GetSpikeInput() { return spikeInput; }
00196
00204 void SetSpikeInput(SpikeInput* sIn);
00205
00207 bool StreamingInput() { return streamingInput; }
00208
00213 void StreamingInput(bool streaming) { streamingInput = streaming; }
00214
00216 static void ResetSimTime();
00217
00220 void SetTrainingMode(bool tMode);
00221
00223 bool GetTrainingMode() const { return trainingMode; }
00224
00226 Layer * GetLayer(AmIdInt layerId);
00227
00232 void EnableSpikeBatching() { spikeDelaysOn = true; }
00233
00234 protected:
00244 void ScheduleSpikeDelay(vector<Synapse*>& axon);
00245
00249 virtual void IncrementSimTime();
00250
00251 int pspLSize;
00252 int pspLRes;
00253 static AmTimeInt simStepSize;
00254
00255 int netSize;
00256 bool streamingInput;
00257
00258 AmIdInt nextNeuronId;
00259 unsigned int nextLayerId;
00260
00261 FunctionLookup* functionRef;
00262 SpikeInput* spikeInput;
00263
00264 hash_map<AmIdInt, Neuron*> net;
00265
00266 hash_map<unsigned int, Layer*> layers;
00267
00272 struct SpikeRequest {
00273 AmTimeInt spikeTime;
00274 AmTimeInt requestTime;
00275 unsigned int requestOrder;
00276 Neuron* requestor;
00277
00278 bool operator<(const SpikeRequest& sr) const
00279 {
00280 if(spikeTime != sr.spikeTime) {
00281 return spikeTime>sr.spikeTime;
00282 }
00283 else if(requestTime != sr.requestTime) {
00284 return requestTime>sr.requestTime;
00285 }
00286 else {
00287 return requestOrder<sr.requestOrder;
00288 }
00289 }
00290 };
00291
00292 priority_queue<SpikeRequest> eventQ;
00293 priority_queue<SpikeRequest> inputQ;
00294 vector< vector<Synapse*> > delayedSpikeQ;
00295 AmTimeInt currSpikeDelayOffset;
00296 AmTimeInt maxOffset;
00297 bool spikeDelaysOn;
00298 AmTimeInt maxSpikeDelay;
00299 Synapse* maxSpikeDelaySyn;
00300 static AmTimeInt simTime;
00301 static unsigned int runCount;
00302 unsigned int spikeBatchCount;
00303
00304 inline void IncrementDelayOffset();
00305
00306 private:
00310 void SendDelayedSpikes();
00311
00313 void SetDefaults();
00314
00316 void InitializeDelayedSpikeQ();
00317
00318 unsigned int eventRequestCount;
00319 bool isLayered;
00320 bool trainingMode;
00321 };
00322
00323 #endif