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
soxexpand.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 __SOXEXPAND_H
20 #define __SOXEXPAND_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 
30 /**
31  Configuration object for SoxExpand.
32  use SoxExpand::getDefaultConf() and then modify as needed before passing it to
33  the contructor.
34  */
35 struct SoxExpandConf {
36  double initFeedbackStrength; ///< initial value of diagonals of C
37  unsigned int numberContextSensors; ///< number of sensors considered as context sensors
38  matrix::Matrix contextCoupling; ///< coupling of context senors to bias
39 };
40 
41 
42 /**
43  * This controller implements the standard algorihm described the Chapter 3 (Homeokinesis)
44  * with body expansion via context sensors
45  */
46 class SoxExpand : public AbstractController {
47 
48 public:
50  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
51 
52  virtual ~SoxExpand();
53 
58  return c;
59  }
60 
61  /// returns the number of sensors the controller was initialised with or 0 if not initialised
62  virtual int getSensorNumber() const { return number_sensors; }
63  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
64  virtual int getMotorNumber() const { return number_motors; }
65 
66  /// performs one step (includes learning).
67  /// Calulates motor commands from sensor inputs.
68  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
69 
70 
71  /// performs one step without learning. Calulates motor commands from sensor inputs.
72  virtual void stepNoLearning(const sensor* , int number_sensors,
73  motor* , int number_motors);
74 
75 
76  /***** STOREABLE ****/
77  /** stores the controller values to a given file. */
78  virtual bool store(FILE* f) const;
79  /** loads the controller values from a given file. */
80  virtual bool restore(FILE* f);
81 
82  /* some direct access functions (unsafe!) */
83  virtual matrix::Matrix getA();
84  virtual void setA(const matrix::Matrix& A);
85  virtual matrix::Matrix getC();
86  virtual void setC(const matrix::Matrix& C);
87  virtual matrix::Matrix geth();
88  virtual void seth(const matrix::Matrix& h);
89  virtual matrix::Matrix getContextC();
90  virtual void setContextC(const matrix::Matrix& CC);
91 
92 
93 protected:
94  unsigned short number_sensors;
95  unsigned short number_motors;
96  static const unsigned short buffersize = 10;
97 
98  matrix::Matrix A; // Model Matrix
99  matrix::Matrix C; // Controller Matrix
100  matrix::Matrix S; // Model Matrix (sensor branch)
101  matrix::Matrix h; // Controller Bias
102  matrix::Matrix b; // Model Bias
103  matrix::Matrix L; // Jacobi Matrix
106 
108 
109  matrix::Matrix y_buffer[buffersize]; // buffer needed for delay
110  matrix::Matrix x_buffer[buffersize]; // buffer of sensor values
112  matrix::Matrix x; // current sensor value vector
113  matrix::Matrix x_c; // current context sensor value vector
114  matrix::Matrix x_smooth; // time average of x values
115  int t;
116  bool TLE;
117  bool loga;
118 
125  paramint s4avg; // # of steps the sensors are averaged (1 means no averaging)
126  paramint s4delay; // # of steps the motor values are delayed (1 means no delay)
127 
128 
129  /// learn values model and controller (A,b,C,h)
130  virtual void learn();
131 
132  /// neuron transfer function
133  static double g(double z)
134  {
135  return tanh(z);
136  };
137 
138  /// derivative of g
139  static double g_s(double z)
140  {
141  double k=tanh(z);
142  return 1.0 - k*k;
143  };
144 
145  /// function that clips the second argument to the interval [-first,first]
146  static double clip(double r, double x){
147  return min(max(x,-r),r);
148  }
149  /// calculates the inverse the argument (useful for Matrix::map)
150  static double one_over(double x){
151  return 1/x;
152  }
153 
154 
155 };
156 
157 #endif
158 
159 
Matrix type.
Definition: matrix.h:65
matrix::Matrix AC
Definition: soxexpand.h:104
matrix::Matrix v_avg
Definition: soxexpand.h:111
matrix::Matrix h
Definition: soxexpand.h:101
matrix::Matrix x_buffer[buffersize]
Definition: soxexpand.h:110
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
virtual void seth(const matrix::Matrix &h)
Definition: soxexpand.cpp:129
double initFeedbackStrength
initial value of diagonals of C
Definition: soxexpand.h:36
static SoxExpandConf getDefaultConf()
Definition: soxexpand.h:54
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: soxexpand.cpp:242
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: soxexpand.cpp:253
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: soxexpand.cpp:151
matrix::Matrix b
Definition: soxexpand.h:102
SoxExpandConf conf
Definition: soxexpand.h:107
double sensor
Definition: types.h:29
paramval causeaware
Definition: soxexpand.h:122
paramint s4avg
Definition: soxexpand.h:125
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
static const unsigned short buffersize
Definition: soxexpand.h:96
unsigned int numberContextSensors
number of sensors considered as context sensors
Definition: soxexpand.h:37
static double g(double z)
neuron transfer function
Definition: soxexpand.h:133
matrix::Matrix A
Definition: soxexpand.h:98
matrix::Matrix x_c
Definition: soxexpand.h:113
paramval harmony
Definition: soxexpand.h:121
matrix::Matrix R
Definition: soxexpand.h:105
virtual void setContextC(const matrix::Matrix &CC)
Definition: soxexpand.cpp:119
unsigned short number_sensors
Definition: soxexpand.h:94
double paramval
Definition: configurable.h:88
paramval creativity
Definition: soxexpand.h:119
bool TLE
Definition: soxexpand.h:116
virtual ~SoxExpand()
Definition: soxexpand.cpp:57
matrix::Matrix x_smooth
Definition: soxexpand.h:114
bool loga
Definition: soxexpand.h:117
This controller implements the standard algorihm described the Chapter 3 (Homeokinesis) with body exp...
Definition: soxexpand.h:46
static double g_s(double z)
derivative of g
Definition: soxexpand.h:139
matrix::Matrix L
Definition: soxexpand.h:103
SoxExpand(const SoxExpandConf &conf=getDefaultConf())
Definition: soxexpand.cpp:24
virtual void setC(const matrix::Matrix &C)
Definition: soxexpand.cpp:111
matrix::Matrix y_buffer[buffersize]
Definition: soxexpand.h:109
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: soxexpand.cpp:61
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
virtual void learn()
learn values model and controller (A,b,C,h)
Definition: soxexpand.cpp:190
virtual void setA(const matrix::Matrix &A)
Definition: soxexpand.cpp:102
int t
Definition: soxexpand.h:115
paramint s4delay
Definition: soxexpand.h:126
virtual matrix::Matrix getC()
Definition: soxexpand.cpp:107
paramval epsA
Definition: soxexpand.h:124
paramval epsC
Definition: soxexpand.h:123
matrix::Matrix S
Definition: soxexpand.h:100
matrix::Matrix x
Definition: soxexpand.h:112
int paramint
Definition: configurable.h:98
double motor
Definition: types.h:30
Configuration object for SoxExpand.
Definition: soxexpand.h:35
matrix::Matrix contextCoupling
coupling of context senors to bias
Definition: soxexpand.h:38
static double one_over(double x)
calculates the inverse the argument (useful for Matrix::map)
Definition: soxexpand.h:150
matrix::Matrix C
Definition: soxexpand.h:99
virtual matrix::Matrix getContextC()
Definition: soxexpand.cpp:116
paramval sense
Definition: soxexpand.h:120
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
unsigned short number_motors
Definition: soxexpand.h:95
static double clip(double r, double x)
function that clips the second argument to the interval [-first,first]
Definition: soxexpand.h:146
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: soxexpand.h:64
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: soxexpand.h:62
virtual matrix::Matrix geth()
Definition: soxexpand.cpp:125
virtual matrix::Matrix getA()
Definition: soxexpand.cpp:98
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: soxexpand.cpp:135
int c
Definition: hexapod.cpp:56