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
soml.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 __SOML_H
20 #define __SOML_H
21 
22 #include <selforg/abstractcontroller.h>
23 #include <selforg/controller_misc.h>
24 #include <selforg/controllernet.h>
25 
26 #include <assert.h>
27 #include <cmath>
28 
29 #include <selforg/matrix.h>
30 
31 /// Configuration object for SoML controller
32 struct SoMLConf {
33  bool useHiddenContr; ///< use a hidden layer in the controller network?
34  /// ratio of motor units and hidden units in the controller (2 -> double amount of hidden unit)
36  bool useHiddenModel; ///< use a hiddenlayer in the model network?
37  /// ratio of motor units and hidden units in the model (2 -> double amount of hidden unit)
39  bool useS; ///< direct connection from x_t to xp_t+1
40  bool initUnitMatrix; /// if true then the network is initialized with unit matrices
41 
42  bool someInternalParams; ///< only export some internal parameters
43 };
44 
45 /**
46  * This controller implements the homeokinetic learning algorihm
47  * in sensor space with extended controller network
48  */
49 class SoML : public AbstractController {
50 
51 public:
52  SoML(const SoMLConf& conf = getDefaultConf());
53 
55  SoMLConf c;
56  c.useHiddenContr = true;
57  c.useHiddenModel = true;
58  c.hiddenContrUnitsRatio = 1.0;
59  c.hiddenModelUnitsRatio = 1.0;
60  c.someInternalParams=false;
61  c.useS = false;
62  c.initUnitMatrix = true;
63  return c;
64  }
65 
66  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
67 
68  virtual ~SoML();
69 
70  /// returns the number of sensors the controller was initialised with or 0 if not initialised
71  virtual int getSensorNumber() const { return number_sensors; }
72  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
73  virtual int getMotorNumber() const { return number_motors; }
74 
75  /// performs one step (includes learning).
76  /// Calulates motor commands from sensor inputs.
77  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
78 
79  /// performs one step without learning. Calulates motor commands from sensor inputs.
80  virtual void stepNoLearning(const sensor* , int number_sensors,
81  motor* , int number_motors);
82 
83  // motor babbling: learn the basic relations from observed sensors/motors
84  virtual void motorBabblingStep(const sensor* , int number_sensors,
85  const motor* , int number_motors);
86 
87 
88  /***** STOREABLE ****/
89  /** stores the controller values to a given file. */
90  virtual bool store(FILE* f) const;
91  /** loads the controller values from a given file. */
92  virtual bool restore(FILE* f);
93 
94  /// returns controller network (to be added to inspectables of agent)
95  virtual ControllerNet* getCNet();
96 
97 protected:
98  /// performs control step (activates network and stores results in buffer and y_)
99  void control(const matrix::Matrix& x, motor* y_, int number_motors);
100 
101  /** learn values model and controller network
102  using the current sensors x, the commands y (from last step (or earlier in case of delay))
103  */
104  virtual void learn(const matrix::Matrix& x, const matrix::Matrix& y);
105 
106  /* learns the model using backprop. It uses the current activation,
107  the current x and x_tm1 from the buffer */
108  virtual void learnModelBP(double factor);
109 
110 protected:
111  unsigned short number_sensors;
112  unsigned short number_motors;
113  static const unsigned short buffersize = 10;
114 
115  matrix::Matrix y_buffer[buffersize]; ///< buffer needed for delay
116  matrix::Matrix x_buffer[buffersize]; ///< buffer needed for delay
117  ControllerNet* cNet; ///< Controller network
118  unsigned int numControllerLayer; ///< number of controller layer
119 
120  SoMLConf conf; ///< configuration object
121 
122  matrix::Matrix x; // current sensor value vector
123  matrix::Matrix x_smooth; // time average of x values
124  matrix::Matrix eta_avg; // time average of shift (in motor space)
125  int t;
126  double E;
127 
131  paramval harmony; ///< harmony
133  paramval discountS; ///< discount for S part of the model
134  paramint s4avg; ///< # of steps the sensors are averaged (1 means no averaging)
135  paramint s4delay; ///< # of steps the motor values are delayed (1 means no delay)
137  parambool loga; ///< # use logarithmic error
138 
139 
140 
141 };
142 
143 #endif
144 
145 
Matrix type.
Definition: matrix.h:65
This controller implements the homeokinetic learning algorihm in sensor space with extended controlle...
Definition: soml.h:49
bool parambool
Definition: configurable.h:93
void control(const matrix::Matrix &x, motor *y_, int number_motors)
performs control step (activates network and stores results in buffer and y_)
Definition: soml.cpp:153
paramval dampA
Definition: soml.h:132
matrix::Matrix x_buffer[buffersize]
buffer needed for delay
Definition: soml.h:116
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
virtual ControllerNet * getCNet()
returns controller network (to be added to inspectables of agent)
Definition: soml.cpp:48
bool useHiddenContr
use a hidden layer in the controller network?
Definition: soml.h:33
paramval discountS
discount for S part of the model
Definition: soml.h:133
virtual ~SoML()
Definition: soml.cpp:44
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: soml.cpp:282
double sensor
Definition: types.h:29
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
unsigned short number_motors
Definition: soml.h:112
virtual void motorBabblingStep(const sensor *, int number_sensors, const motor *, int number_motors)
called in motor babbling phase.
Definition: soml.cpp:248
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: soml.cpp:119
bool initUnitMatrix
Definition: soml.h:40
double hiddenModelUnitsRatio
ratio of motor units and hidden units in the model (2 -> double amount of hidden unit) ...
Definition: soml.h:38
int t
Definition: soml.h:125
double paramval
Definition: configurable.h:88
static SoMLConf getDefaultConf()
Definition: soml.h:54
bool useHiddenModel
use a hiddenlayer in the model network?
Definition: soml.h:36
paramval harmony
harmony
Definition: soml.h:131
matrix::Matrix y_buffer[buffersize]
buffer needed for delay
Definition: soml.h:115
matrix::Matrix x
Definition: soml.h:122
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: soml.h:71
paramint s4avg
of steps the sensors are averaged (1 means no averaging)
Definition: soml.h:134
ControllerNet * cNet
Controller network.
Definition: soml.h:117
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: soml.h:73
paramval epsA
Definition: soml.h:130
int paramint
Definition: configurable.h:98
matrix::Matrix eta_avg
Definition: soml.h:124
double motor
Definition: types.h:30
unsigned int numControllerLayer
number of controller layer
Definition: soml.h:118
paramval creativity
Definition: soml.h:128
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: soml.cpp:139
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: soml.cpp:290
paramint s4delay
of steps the motor values are delayed (1 means no delay)
Definition: soml.h:135
paramval biasnoise
Definition: soml.h:136
bool someInternalParams
if true then the network is initialized with unit matrices
Definition: soml.h:42
paramval epsC
Definition: soml.h:129
double E
Definition: soml.h:126
parambool loga
use logarithmic error
Definition: soml.h:137
double hiddenContrUnitsRatio
ratio of motor units and hidden units in the controller (2 -> double amount of hidden unit) ...
Definition: soml.h:35
matrix::Matrix x_smooth
Definition: soml.h:123
unsigned short number_sensors
Definition: soml.h:111
SoML(const SoMLConf &conf=getDefaultConf())
Definition: soml.cpp:25
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: soml.cpp:53
SoMLConf conf
configuration object
Definition: soml.h:120
static const unsigned short buffersize
Definition: soml.h:113
virtual void learnModelBP(double factor)
Definition: soml.cpp:225
multi layer neural network with configurable activation functions and propagation and projection meth...
Definition: controllernet.h:35
int c
Definition: hexapod.cpp:56
virtual void learn(const matrix::Matrix &x, const matrix::Matrix &y)
learn values model and controller network using the current sensors x, the commands y (from last step...
Definition: soml.cpp:167
bool useS
direct connection from x_t to xp_t+1
Definition: soml.h:39
Configuration object for SoML controller.
Definition: soml.h:32