source: cpp/gdk/neuroimpl.h @ 72

Last change on this file since 72 was 72, checked in by Maciej Komosinski, 12 years ago

neural noise parameter in neurons

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1// This file is a part of the Framsticks GDK library.
2// Copyright (C) 2002-2011  Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#ifndef _NEUROIMPL_H_
6#define _NEUROIMPL_H_
7
8#include "model.h"
9#include "param.h"
10#include "framsg.h"
11#ifdef NEURO_SIGNALS
12#include "signals.h"
13#endif
14
15class NeuroImpl;
16extern ParamEntry neuroimpl_tab[];
17#ifdef NEURO_SIGNALS
18extern Param neurosignals_param;
19#endif
20
21class Creature;
22
23class NeuroNetConfig
24{
25public:
26NeuroNetConfig();
27
28Param par;
29double randominit;
30double nnoise;
31double touchrange;
32
33static NeuroNetConfig globalconfig;
34};
35
36#ifdef NEURO_SIGNALS
37class NeuroSignals: public SignalSet
38{
39  protected:
40Creature *cr;
41NeuroImpl *owner;
42Creature *getCreature();
43  public:
44
45NeuroSignals(NeuroImpl *n):owner(n),cr(0) {}
46
47#define STATRICKCLASS NeuroSignals
48PARAMPROCDEF(p_add);
49PARAMPROCDEF(p_get);
50PARAMGETDEF(size);
51PARAMPROCDEF(p_receive);
52PARAMPROCDEF(p_receiveSet);
53PARAMPROCDEF(p_receiveFilter);
54PARAMPROCDEF(p_receiveSingle);
55#undef STATRICKCLASS
56};
57#endif
58
59/// Neuro net implementation
60class NeuroNetImpl
61{
62CallbackNode *cnode;
63Model &mod;
64SList neurons[4];
65NeuroNetConfig& config;
66int isbuilt,errorcount;
67STCALLBACKDEFC(NeuroNetImpl,destroyNN);
68int minorder,maxorder;
69
70  public:
71#ifdef NEURO_SIGNALS
72ChannelSpace *channels;
73#endif
74static int mytags_id;
75static double getStateFromNeuro(Neuro *n);
76int getErrorCount() {return errorcount;}
77NeuroNetImpl(Model& model, NeuroNetConfig& conf = NeuroNetConfig::globalconfig
78#ifdef NEURO_SIGNALS
79, ChannelSpace *ch=0
80#endif
81);
82~NeuroNetImpl();
83void simulateNeuroNet();
84void simulateNeuroPhysics();
85
86static NeuroImpl *getImpl(Neuro* n) {return (NeuroImpl*)n->userdata[mytags_id];}
87};
88
89
90/**
91   Neuro implementation - this object calculates the Neuron's state
92   (Neuro::state) in each simulation step.
93
94SUBCLASSING TUTORIAL
95====================
96
971.Derive your custom neuron from NeuroImpl class. The name must be prefixed with NI_
98
99class NI_MyNeuron: public NeuroImpl
100{ ... };
101
1022.Public parameters
103Create any number of public fields, they will be adjustable from the genotype level.
1043 datatypes are supported: long, double and SString
105
106public:
107  long intParameter;
108  double fpParameter;
109  SString txtParameter;
110
111
1123.Required method: "instantiator".
113It is always the same, just create a new instance of your neuron.
114public:
115  NeuroImpl* makeNew() { return new NI_MyNeuron(); };
116
117
1184.Required method: default constructor
119Set the "paramentries" variable if you need public parameters in your neuron.
120NI_..._tab is created automatically and should be declared as: extern ParamEntry NI_..._tab[];
121At this stage the parameter values are not yet available.
122
123public:
124 NI_MyNeuron() // no parameters!
125 {
126 paramentries=NI_MyNeuron_tab;
127 // you add here: some general initialization
128 }
129
130
1315.Optional method: initialization
132This method is called once before the neuron is actually used in the simulation.
133The parameter values are already initialized (according to the genotype) and the neuron is bound to the creature (i.e. this->neuro is valid).
134Return 0 if the neuron cannot be initialized.
135
136int lateinit()
137 {
138 // you add here: initialization using full neuron context
139 // example: if (!neuro->joint) return 0; //this neuron must be attached to joint
140 return 1;//OK
141 }
142
143
1446.Required method: simulation step
145If it has output: calculate the next neuron state and call setState()
146If it is an effector: do anything else
147
148void go()
149 {
150 // you add here: things called every simulation step
151 }
152
153Note: You can make your neuron fire before or after "regular" neurons by changing its "simorder" property (during initialization). The default value is 1, whereas receptors have simorder=0 and effectors have simorder=2.
154
155
1567.Neuron definition
157In order to incorporate the new neuron into Framsticks you need to provide some additional information (to be added to "f0.def" file).
158
159NEUROCLASS(MyNeuron,MN,This is the name,`Neuron description',-1,1,0)
160NEUROPROP(int,0,0,name of the int,d,,,,intParameter)
161NEUROPROP(fp,0,0,name of the floating point,f,,,,fpParameter)
162NEUROPROP(txt,0,0,name of the text,s,,,,txtParameter)
163ENDNEUROCLASS
164
165NEUROCLASS:
166- MyNeuron: neuron class name (without the NI_ prefix)
167- MN: neuron symbol (used in genotypes)
168- full name and description
169- -1: preferred number of inputs (special case: -1=any)
170- 1: provides output: 1=yes/0=no
171- 0: preferred location: 0=none, 1=part, 2=joint
172
173NEUROPROP:
174- int/fp/txt: parameter names as visible in genotypes and scripting
175- "name of the ...": descriptive name
176- d/f/s: type (int/floating point/string)
177- intParameter/fpParameter/txtParameter: C++ field names
178
179   
180 */
181class NeuroImpl
182{
183protected:
184int simorder;
185int channels;
186SListTempl<double> chstate;
187SListTempl<double> chnewstate;
188Param *fields_param;
189ExtObject *fields_object;
190public:
191static const int ENDDRAWING;
192static const int MAXDRAWINGXY;
193
194enum NeuroImplStats { BeforeInit=0, InitError=1, InitOk=2 };
195NeuroImplStats status;
196/** originating neuron object (from the model) */
197Neuro *neuro;
198NeuroClass *neuroclass;
199/** don't access directly */
200double newstate;
201
202#ifdef NEURO_SIGNALS
203NeuroSignals sigs;
204#endif
205NeuroNetImpl *owner;
206ExtObject sigs_obj;
207
208/** "virtual constructor" - NeuroFactory uses this method to create the proper implementation object.
209    subclasses must return new object here. */
210virtual NeuroImpl* makeNew() {return 0;} //
211/** will be used by readParam() method, if not null  */
212ParamEntry *paramentries; // no extra properties if ==0
213/** read additional properties from "moredata" field of the originating Neuro */
214void readParam();
215/** called when all other neuro objects were already created and "moredata" transferred to
216    object fields.
217    useful for initialization that cannot be performed in the constructor.
218    @return 1=ok  0=failure
219*/
220virtual int lateinit() {return 1;}
221/** calculate 'newstate - implementation dependent */
222virtual void go(){}
223/** for neurons doing some physical actions (called each simulation step when nnspeed!=1.0) */
224virtual void goPhysics(){}
225
226int getSimOrder() {return simorder;}
227virtual int getNeedPhysics() {return 0;}
228
229void setChannelCount(int c);
230int getChannelCount() {return channels;}
231
232int getInputCount() {return neuro->getInputCount();}
233int getInputChannelCount(int i);
234double getInputState(int i,int channel=0);
235double getWeightedInputState(int i,int channel=0);
236double getInputSum(int startwith=0);
237double getWeightedInputSum(int startwith=0);
238double getInputWeight(int i) {return neuro->getInputWeight(i);}
239void setState(double st,int channel);
240void setState(double st) {validateNeuroState(st); newstate=st;}
241double getState(int channel);
242double getState() {return neuro->state;}
243
244virtual int getDrawingCount() {return 0;}
245virtual int* getDrawing(int i) {return 0;}
246
247void commit();
248void validateNeuroState(double& st) {if (st<=-1e10) st=-1e10; else if (st>1e10) st=1e10;}
249
250NeuroImpl():owner(0),neuro(0),newstate(0),paramentries(0),simorder(1),status(BeforeInit),channels(1),fields_param(0),fields_object(0)
251#ifdef NEURO_SIGNALS
252,sigs(this),sigs_obj(&neurosignals_param,&sigs)
253#endif
254 {}
255virtual ~NeuroImpl();
256virtual void createFieldsObject();
257
258/** usually == "newstate" but will obey the "hold state" */
259double getNewState(int channel=0);
260
261/** don't use! */
262void setCurrentState(double st,int channel=0);
263
264bool getPosition(Pt3D &pos);
265Creature* getCreature();
266
267#define STATRICKCLASS NeuroImpl
268PARAMGETDEF(count) {arg1->setInt(getInputCount());}
269PARAMPROCDEF(p_get) {arg2->setDouble(getInputState(arg1->getInt()));}
270PARAMPROCDEF(p_getweight) {arg2->setDouble(getInputWeight(arg1->getInt()));}
271PARAMPROCDEF(p_getw) {arg2->setDouble(getWeightedInputState(arg1->getInt()));}
272PARAMPROCDEF(p_getsum) {arg2->setDouble(getInputSum(arg1->getInt()));}
273PARAMPROCDEF(p_getwsum) {arg2->setDouble(getWeightedInputSum(arg1->getInt()));}
274PARAMGETDEF(sum) {arg1->setDouble(getInputSum(0));}
275PARAMGETDEF(wsum) {arg1->setDouble(getWeightedInputSum(0));}
276PARAMPROCDEF(p_getchancount) {arg2->setInt(getInputChannelCount(arg1->getInt()));}
277PARAMPROCDEF(p_getchan) {arg2->setDouble(getInputState(arg1[1].getInt(),arg1[0].getInt()));}
278PARAMPROCDEF(p_getwchan) {arg2->setDouble(getWeightedInputState(arg1[1].getInt(),arg1[0].getInt()));}
279PARAMGETDEF(state) {arg1->setDouble(getState());}
280PARAMSETDEF(state) {setState(arg1->getDouble()); return 0;}
281PARAMGETDEF(cstate) {arg1->setDouble(neuro->state);}
282PARAMSETDEF(cstate) {setCurrentState(arg1->getDouble()); return 0;}
283PARAMGETDEF(hold) {arg1->setInt((neuro->flags&(Neuro::HoldState))?1:0);}
284PARAMSETDEF(hold) {neuro->flags=(neuro->flags&~Neuro::HoldState)|(arg1->getInt()?Neuro::HoldState:0); return 0;}
285PARAMGETDEF(channels) {arg1->setInt(getChannelCount());}
286PARAMSETDEF(channels) {setChannelCount(arg1->getInt()); return 0;}
287PARAMPROCDEF(p_getstate) {arg2->setDouble(getState(arg1->getInt()));}
288PARAMPROCDEF(p_setstate) {setState(arg1[0].getDouble(),arg1[1].getInt());}
289PARAMPROCDEF(p_setcstate) {setCurrentState(arg1[0].getDouble(),arg1[1].getInt());}
290PARAMGETDEF(creature);
291PARAMGETDEF(part);
292PARAMGETDEF(joint);
293PARAMGETDEF(position_x);
294PARAMGETDEF(position_y);
295PARAMGETDEF(position_z);
296PARAMGETDEF(fields);
297PARAMGETDEF(neurodef);
298PARAMGETDEF(classObject);
299#undef STATRICKCLASS
300};
301
302#endif
Note: See TracBrowser for help on using the repository browser.