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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef __ABSTRACTIAFCONTROLLER_H
00053 #define __ABSTRACTIAFCONTROLLER_H
00054
00055 #include "abstractcontroller.h"
00056
00057 #include <selforg/matrix.h>
00058 #include "controller_misc.h"
00059 #include <selforg/configurable.h>
00060
00061 typedef struct AbstractIAFControllerConf {
00062 AbstractIAFControllerConf() {
00063 thresholdI=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00064 thresholdO=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00065 leakI=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00066 leakO=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00067 restingPotential=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00068 wIInitScale=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00069 wOInitScale=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00070 numberIAFNeuronsPerInput=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00071 numberIAFNeuronsPerOutput=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00072 }
00073 ~AbstractIAFControllerConf() {
00074
00075
00076
00077
00078 }
00079 Configurable::paramval* numberIAFNeuronsPerInput;
00080 Configurable::paramval* numberIAFNeuronsPerOutput;
00081 Configurable::paramval* wIInitScale;
00082 Configurable::paramval* wOInitScale;
00083 Configurable::paramval* thresholdI;
00084 Configurable::paramval* thresholdO;
00085 Configurable::paramval* leakI;
00086 Configurable::paramval* leakO;
00087 Configurable::paramval* restingPotential;
00088 } AbstractIAFControllerConf;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 class AbstractIAFController : public AbstractController {
00100
00101 public:
00102 AbstractIAFController(const AbstractIAFControllerConf& conf = getDefaultConf());
00103
00104 virtual ~AbstractIAFController() {}
00105
00106
00107 static AbstractIAFControllerConf getDefaultConf(){
00108 AbstractIAFControllerConf c;
00109 *c.numberIAFNeuronsPerInput = 10;
00110 *c.numberIAFNeuronsPerOutput = 10;
00111 *c.wIInitScale= 0.5;
00112 *c.wOInitScale= 0.5;
00113 *c.thresholdI=0.5;
00114 *c.thresholdO=0.5;
00115 *c.leakI=0.01;
00116 *c.leakO=0.01;
00117 *c.restingPotential=0.0;
00118
00119 return c;
00120 }
00121
00122
00123
00124
00125 virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
00126
00127 virtual int getSensorNumber() const { return sensorNumber; }
00128
00129 virtual int getMotorNumber() const { return motorNumber; }
00130
00131 virtual void step(const sensor* sensors, int sensornumber, motor* motors, int motornumber);
00132
00133 virtual void stepNoLearning(const sensor* sensors, int sensornumber, motor* motors, int motornumber);
00134
00135
00136
00137 virtual bool store(FILE* f) const { return true; }
00138
00139 virtual bool restore(FILE* f) { return true; }
00140
00141
00142 virtual bool setParam(const paramkey& key, paramval val);
00143
00144 protected:
00145 AbstractIAFControllerConf conf;
00146 RandGen* randG;
00147 bool initialised;
00148 int sensorNumber;
00149 int motorNumber;
00150 double range;
00151 matrix::Matrix xI;
00152 matrix::Matrix xO;
00153 matrix::Matrix wI;
00154 matrix::Matrix wO;
00155 matrix::Matrix sumI;
00156 matrix::Matrix sumO;
00157 matrix::Matrix tI;
00158 matrix::Matrix tO;
00159
00160
00161
00162
00163 virtual void forwardStep(const sensor* sensors, int number_sensors, motor* motors, int number_motors);
00164
00165
00166
00167
00168
00169
00170
00171 void initMatrices();
00172
00173
00174
00175
00176
00177 static double toTristateWithProbability(void* r,double x) {
00178 RandGen* g = (RandGen*) r;
00179 if (!g) return 0.;
00180 double rand = g->rand();
00181 return x < -rand ? -1. : (x < rand ? 0. : 1.);
00182 }
00183
00184
00185
00186 static double toTristateWithThreshold(double x, double threshold){
00187 return x < -threshold ? -1. : (x < threshold ? 0. : 1.);
00188 }
00189
00190
00191
00192
00193 static double dampToZero(void* r, double x){
00194 double damp = *(double*)r;
00195 return x < -damp ? x+damp : (x > damp ? x-damp : 0.);
00196 }
00197
00198
00199 static double toZeroIfFired(double x, double fired) {
00200 return (fired==1 || fired==-1) ? 0 : x ;
00201 }
00202
00203
00204 static double toValueIfFired(void* r,double x, double fired) {
00205 double value = *(double*)r;
00206 return (fired==1 || fired==-1) ? value : x ;
00207 }
00208
00209
00210
00211 static double toDualStateWithProbability(void* r,double x) {
00212 RandGen* g = (RandGen*) r;
00213 if (!g) return 0.;
00214 double rand = g->rand();
00215 return x < rand ? 0. : 1.;
00216 }
00217
00218
00219 static double toDualStateWithThreshold(double x, double threshold){
00220 return x < threshold ? 0. : 1.;
00221 }
00222
00223
00224
00225
00226 };
00227
00228
00229 #endif