Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sox.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 by *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Ralf Der <ralfder at mis dot mpg dot de> *
5  * *
6  * ANY COMMERCIAL USE FORBIDDEN! *
7  * LICENSE: *
8  * This work is licensed under the Creative Commons *
9  * Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of *
10  * this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
11  * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, *
12  * San Francisco, California, 94105, USA. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17  * *
18  ***************************************************************************/
19 #ifndef __SOX_H
20 #define __SOX_H
21 
22 #include <selforg/abstractcontroller.h>
23 #include <selforg/controller_misc.h>
24 
25 #include <assert.h>
26 #include <cmath>
27 
28 #include <selforg/matrix.h>
29 #include <selforg/teachable.h>
30 #include <selforg/parametrizable.h>
31 
32 
33 /// configuration object for Sox controller. Use Sox::getDefaultConf().
34 struct SoxConf {
35  double initFeedbackStrength; ///< initial strength of sensor to motor connection
36  bool useExtendedModel; ///< if true, the extended model (S matrix) is used
37  /// if true the controller can be taught see teachable interface
39  /// # of steps the sensors are averaged (1 means no averaging)
41  /// # of steps the motor values are delayed (1 means no delay)
43  bool someInternalParams; ///< if true only some internal parameters are exported
44  bool onlyMainParameters; ///< if true only some configurable parameters are exported
45 
46  double factorS; ///< factor for learning rate of S
47  double factorb; ///< factor for learning rate of b
48  double factorh; ///< factor for learning rate of h
49 };
50 
51 
52 /**
53  * This controller implements the standard algorihm described the the Chapter 5 (Homeokinesis)
54  * with extensions of Chapter 15 of book "The Playful Machine"
55  */
56 class Sox : public AbstractController, public Teachable, public Parametrizable {
57 
58 public:
59  /// constructor
60  Sox(const SoxConf& conf = getDefaultConf());
61 
62  /// constructor provided for convenience, use conf object to customize more
63  Sox(double init_feedback_strength, bool useExtendedModel = true,
64  bool useTeaching = false );
65 
66  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
67 
68  virtual ~Sox();
69 
71  SoxConf conf;
72  conf.initFeedbackStrength = 1.0;
73  conf.useExtendedModel = true;
74  conf.useTeaching = false;
75  conf.steps4Averaging = 1;
76  conf.steps4Delay = 1;
77  conf.someInternalParams = false;
78  conf.onlyMainParameters = true;
79 
80  conf.factorS = 1;
81  conf.factorb = 1;
82  conf.factorh = 1;
83  return conf;
84  }
85 
86 
87  /// returns the number of sensors the controller was initialised with or 0 if not initialised
88  virtual int getSensorNumber() const { return number_sensors; }
89  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
90  virtual int getMotorNumber() const { return number_motors; }
91 
92  /// performs one step (includes learning).
93  /// Calulates motor commands from sensor inputs.
94  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
95 
96 
97  /// performs one step without learning. Calulates motor commands from sensor inputs.
98  virtual void stepNoLearning(const sensor* , int number_sensors,
99  motor* , int number_motors);
100 
101  /// called during babbling phase
102  virtual void motorBabblingStep(const sensor* , int number_sensors,
103  const motor* , int number_motors);
104 
105  /***** STOREABLE ****/
106  /** stores the controller values to a given file. */
107  virtual bool store(FILE* f) const;
108  /** loads the controller values from a given file. */
109  virtual bool restore(FILE* f);
110 
111  /* some direct access functions (unsafe!) */
112  virtual matrix::Matrix getA();
113  virtual void setA(const matrix::Matrix& A);
114  virtual matrix::Matrix getC();
115  virtual void setC(const matrix::Matrix& C);
116  virtual matrix::Matrix geth();
117  virtual void seth(const matrix::Matrix& h);
118 
119  /***** TEACHABLE ****/
120  virtual void setMotorTeaching(const matrix::Matrix& teaching);
121  virtual void setSensorTeaching(const matrix::Matrix& teaching);
124 
125  /***** PARAMETRIZABLE ****/
126  virtual std::list<matrix::Matrix> getParameters() const override;
127  virtual int setParameters(const std::list<matrix::Matrix>& params) override;
128 
129 protected:
130  unsigned short number_sensors;
131  unsigned short number_motors;
132  static const unsigned short buffersize = 10;
133 
134  matrix::Matrix A; // Model Matrix
135  matrix::Matrix C; // Controller Matrix
136  matrix::Matrix S; // Model Matrix (sensor branch)
137  matrix::Matrix h; // Controller Bias
138  matrix::Matrix b; // Model Bias
139  matrix::Matrix L; // Jacobi Matrix
141  matrix::Matrix C_native; // Controller Matrix obtained from motor babbling
142  matrix::Matrix A_native; // Model Matrix obtained from motor babbling
143  matrix::Matrix y_buffer[buffersize]; // buffer needed for delay
144  matrix::Matrix x_buffer[buffersize]; // buffer of sensor values
146  matrix::Matrix x; // current sensor value vector
147  matrix::Matrix x_smooth; // time average of x values
148  int t;
149 
150  bool loga;
151 
152  SoxConf conf; ///< configuration objects
153 
154  bool intern_isTeaching; // teaching signal available?
155  matrix::Matrix y_teaching; // motor teaching signal
156 
165  paramval gamma; // teaching strength
166 
167  void constructor();
168 
169  // calculates the pseudo inverse of L in different ways, depending on pseudo
171 
172  /// learn values model and controller (A,b,C,h)
173  virtual void learn();
174 
175  /// neuron transfer function
176  static double g(double z)
177  {
178  return tanh(z);
179  };
180 
181  /// derivative of g
182  static double g_s(double z)
183  {
184  double k=tanh(z);
185  return 1.0 - k*k;
186  };
187 
188  /// function that clips the second argument to the interval [-first,first]
189  static double clip(double r, double x){
190  return min(max(x,-r),r);
191  }
192  /// calculates the inverse the argument (useful for Matrix::map)
193  static double one_over(double x){
194  return 1/x;
195  }
196 
197 
198 };
199 
200 #endif
201 
202 
Matrix type.
Definition: matrix.h:65
paramval sense
Definition: sox.h:158
matrix::Matrix y_teaching
Definition: sox.h:155
Interface for teachable controller.
Definition: teachable.h:32
virtual matrix::Matrix getLastMotorValues()
returns the last motor values (useful for cross motor coupling)
Definition: sox.cpp:345
virtual matrix::Matrix geth()
Definition: sox.cpp:154
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
int t
Definition: sox.h:148
paramval gamma
Definition: sox.h:165
virtual void setSensorTeaching(const matrix::Matrix &teaching)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: sox.cpp:337
static double clip(double r, double x)
function that clips the second argument to the interval [-first,first]
Definition: sox.h:189
static const unsigned short buffersize
Definition: sox.h:132
matrix::Matrix R
Definition: sox.h:140
virtual void motorBabblingStep(const sensor *, int number_sensors, const motor *, int number_motors)
called during babbling phase
Definition: sox.cpp:210
Sox(const SoxConf &conf=getDefaultConf())
constructor
Definition: sox.cpp:24
matrix::Matrix x
Definition: sox.h:146
paramval creativity
Definition: sox.h:157
static double g_s(double z)
derivative of g
Definition: sox.h:182
virtual void init(int sensornumber, int motornumber, RandGen *randGen=0)
initialisation of the controller with the given sensor/ motornumber Must be called before use...
Definition: sox.cpp:96
unsigned short number_motors
Definition: sox.h:131
bool intern_isTeaching
Definition: sox.h:154
double factorh
factor for learning rate of h
Definition: sox.h:48
virtual std::list< matrix::Matrix > getParameters() const override
Returns a list of matrices that parametrize the controller.
Definition: sox.cpp:353
double sensor
Definition: types.h:29
unsigned short number_sensors
Definition: sox.h:130
virtual matrix::Matrix getC()
Definition: sox.cpp:145
static SoxConf getDefaultConf()
Definition: sox.h:70
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
int steps4Averaging
of steps the sensors are averaged (1 means no averaging)
Definition: sox.h:40
matrix::Matrix v_avg
Definition: sox.h:145
matrix::Matrix S
Definition: sox.h:136
static double one_over(double x)
calculates the inverse the argument (useful for Matrix::map)
Definition: sox.h:193
SoxConf conf
configuration objects
Definition: sox.h:152
double factorb
factor for learning rate of b
Definition: sox.h:47
matrix::Matrix x_buffer[buffersize]
Definition: sox.h:144
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: sox.cpp:164
paramint pseudo
Definition: sox.h:161
paramval epsC
Definition: sox.h:162
This controller implements the standard algorihm described the the Chapter 5 (Homeokinesis) with exte...
Definition: sox.h:56
double paramval
Definition: configurable.h:88
bool someInternalParams
if true only some internal parameters are exported
Definition: sox.h:43
virtual void setA(const matrix::Matrix &A)
Definition: sox.cpp:140
virtual matrix::Matrix getLastSensorValues()
returns the last sensor values (useful for cross sensor coupling)
Definition: sox.cpp:349
virtual void learn()
learn values model and controller (A,b,C,h)
Definition: sox.cpp:258
bool onlyMainParameters
if true only some configurable parameters are exported
Definition: sox.h:44
matrix::Matrix b
Definition: sox.h:138
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: sox.cpp:374
virtual void seth(const matrix::Matrix &h)
Definition: sox.cpp:158
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
matrix::Matrix A
Definition: sox.h:134
bool loga
Definition: sox.h:150
bool useExtendedModel
if true, the extended model (S matrix) is used
Definition: sox.h:36
void constructor()
Definition: sox.cpp:42
int steps4Delay
of steps the motor values are delayed (1 means no delay)
Definition: sox.h:42
virtual void setMotorTeaching(const matrix::Matrix &teaching)
The given motor teaching signal is used for this timestep.
Definition: sox.cpp:328
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: sox.cpp:386
int paramint
Definition: configurable.h:98
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: sox.cpp:180
double motor
Definition: types.h:30
using ParameterList = std::list<matrix::Matrix>;
Definition: parametrizable.h:39
matrix::Matrix C
Definition: sox.h:135
matrix::Matrix h
Definition: sox.h:137
paramval harmony
Definition: sox.h:159
double initFeedbackStrength
initial strength of sensor to motor connection
Definition: sox.h:35
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: sox.h:88
paramval epsA
Definition: sox.h:163
paramval causeaware
Definition: sox.h:160
virtual void setC(const matrix::Matrix &C)
Definition: sox.cpp:149
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: sox.h:90
paramval damping
Definition: sox.h:164
matrix::Matrix pseudoInvL(const matrix::Matrix &L, const matrix::Matrix &A, const matrix::Matrix &C)
Definition: sox.cpp:246
double factorS
factor for learning rate of S
Definition: sox.h:46
configuration object for Sox controller. Use Sox::getDefaultConf().
Definition: sox.h:34
virtual int setParameters(const std::list< matrix::Matrix > &params) override
sets the parameters.
Definition: sox.cpp:357
virtual matrix::Matrix getA()
Definition: sox.cpp:136
virtual ~Sox()
Definition: sox.cpp:92
static double g(double z)
neuron transfer function
Definition: sox.h:176
matrix::Matrix x_smooth
Definition: sox.h:147
matrix::Matrix L
Definition: sox.h:139
matrix::Matrix y_buffer[buffersize]
Definition: sox.h:143
bool useTeaching
if true the controller can be taught see teachable interface
Definition: sox.h:38
matrix::Matrix A_native
Definition: sox.h:142
matrix::Matrix C_native
Definition: sox.h:141