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
sos.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 __SOS_H
20 #define __SOS_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  * This controller implements the standard algorihm described the Chapter 5 (Homeokinesis)
32  * of book "The Playful Machine"
33  */
34 class Sos : public AbstractController {
35 
36 public:
37  Sos(double init_feedback_strength = 1.0);
38  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
39 
40  virtual ~Sos();
41 
42  /// returns the number of sensors the controller was initialised with or 0 if not initialised
43  virtual int getSensorNumber() const { return number_sensors; }
44  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
45  virtual int getMotorNumber() const { return number_motors; }
46 
47  /// performs one step (includes learning).
48  /// Calulates motor commands from sensor inputs.
49  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
50 
51 
52  /// performs one step without learning. Calulates motor commands from sensor inputs.
53  virtual void stepNoLearning(const sensor* , int number_sensors,
54  motor* , int number_motors);
55 
56 
57  /***** STOREABLE ****/
58  /** stores the controller values to a given file. */
59  virtual bool store(FILE* f) const;
60  /** loads the controller values from a given file. */
61  virtual bool restore(FILE* f);
62 
63  /* some direct access functions (unsafe!) */
64  virtual matrix::Matrix getA();
65  virtual void setA(const matrix::Matrix& A);
66  virtual matrix::Matrix getC();
67  virtual void setC(const matrix::Matrix& C);
68  virtual matrix::Matrix geth();
69  virtual void seth(const matrix::Matrix& h);
70 
71 protected:
72  unsigned short number_sensors;
73  unsigned short number_motors;
74  static const unsigned short buffersize = 10;
75 
76  matrix::Matrix A; // Model Matrix
77  matrix::Matrix C; // Controller Matrix
78  matrix::Matrix h; // Controller Bias
79  matrix::Matrix b; // Model Bias
80  matrix::Matrix L; // Jacobi Matrix
81  matrix::Matrix y_buffer[buffersize]; // buffer needed for delay
82  matrix::Matrix x_buffer[buffersize]; // buffer of sensor values
84  matrix::Matrix x; // current sensor value vector
85  matrix::Matrix x_smooth; // time average of x values
86  int t;
87  bool TLE;
88  bool loga;
89 
91 
95  paramint s4avg; // # of steps the sensors are averaged (1 means no averaging)
96  paramint s4delay; // # of steps the motor values are delayed (1 means no delay)
97 
98 
99  /// learn values model and controller (A,b,C,h)
100  virtual void learn();
101 
102  /// neuron transfer function
103  static double g(double z)
104  {
105  return tanh(z);
106  };
107 
108  /// derivative of g
109  static double g_s(double z)
110  {
111  double k=tanh(z);
112  return 1.0 - k*k;
113  };
114 
115  /// function that clips the second argument to the interval [-first,first]
116  static double clip(double r, double x){
117  return min(max(x,-r),r);
118  }
119  /// calculates the inverse the argument (useful for Matrix::map)
120  static double one_over(double x){
121  return 1/x;
122  }
123 
124 
125 };
126 
127 #endif
128 
129 
matrix::Matrix x_buffer[buffersize]
Definition: sos.h:82
Matrix type.
Definition: matrix.h:65
matrix::Matrix y_buffer[buffersize]
Definition: sos.h:81
virtual matrix::Matrix geth()
Definition: sos.cpp:93
int t
Definition: sos.h:86
double init_feedback_strength
Definition: sos.h:90
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
unsigned short number_motors
Definition: sos.h:73
static double clip(double r, double x)
function that clips the second argument to the interval [-first,first]
Definition: sos.h:116
bool loga
Definition: sos.h:88
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: sos.cpp:103
matrix::Matrix C
Definition: sos.h:77
double sensor
Definition: types.h:29
matrix::Matrix x
Definition: sos.h:84
static double g(double z)
neuron transfer function
Definition: sos.h:103
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
Sos(double init_feedback_strength=1.0)
Definition: sos.cpp:24
bool TLE
Definition: sos.h:87
virtual void setC(const matrix::Matrix &C)
Definition: sos.cpp:88
matrix::Matrix v_avg
Definition: sos.h:83
unsigned short number_sensors
Definition: sos.h:72
virtual matrix::Matrix getA()
Definition: sos.cpp:75
virtual void learn()
learn values model and controller (A,b,C,h)
Definition: sos.cpp:149
virtual ~Sos()
Definition: sos.cpp:46
double paramval
Definition: configurable.h:88
This controller implements the standard algorihm described the Chapter 5 (Homeokinesis) of book "The ...
Definition: sos.h:34
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: sos.cpp:118
matrix::Matrix L
Definition: sos.h:80
static double g_s(double z)
derivative of g
Definition: sos.h:109
paramint s4delay
Definition: sos.h:96
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: sos.cpp:50
paramval epsA
Definition: sos.h:94
matrix::Matrix b
Definition: sos.h:79
static const unsigned short buffersize
Definition: sos.h:74
virtual void setA(const matrix::Matrix &A)
Definition: sos.cpp:79
int paramint
Definition: configurable.h:98
double motor
Definition: types.h:30
paramval epsC
Definition: sos.h:93
virtual matrix::Matrix getC()
Definition: sos.cpp:84
matrix::Matrix A
Definition: sos.h:76
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: sos.h:45
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: sos.h:43
static double one_over(double x)
calculates the inverse the argument (useful for Matrix::map)
Definition: sos.h:120
virtual void seth(const matrix::Matrix &h)
Definition: sos.cpp:97
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: sos.cpp:211
matrix::Matrix h
Definition: sos.h:78
paramint s4avg
Definition: sos.h:95
matrix::Matrix x_smooth
Definition: sos.h:85
paramval creativity
Definition: sos.h:92
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: sos.cpp:200